[PATCH] Tests: added tests for proxy_protocol_server_{addr, port} variables
Chris Branch
cbranch at cloudflare.com
Tue Jan 16 14:27:20 UTC 2018
# HG changeset patch
# User Chris Branch <cbranch at cloudflare.com>
# Date 1516111868 0
# Tue Jan 16 14:11:08 2018 +0000
# Node ID 1c5977148c79eba0f14e60df134a7cace77d3207
# Parent 6ca8b38f63b61c6c00ac4c9e355a7909dbb6aeb2
Tests: added tests for proxy_protocol_server_{addr,port} variables.
diff -r 6ca8b38f63b6 -r 1c5977148c79 proxy_protocol_server.t
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/proxy_protocol_server.t Tue Jan 16 14:11:08 2018 +0000
@@ -0,0 +1,108 @@
+#!/usr/bin/perl
+
+# Tests for proxy_protocol_server_{addr,port} variables.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+use Socket qw/ CRLF /;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+my $t = Test::Nginx->new()->has(qw/http/)
+ ->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+daemon off;
+
+events {
+}
+
+http {
+ %%TEST_GLOBALS_HTTP%%
+
+ log_format port $proxy_protocol_server_port;
+
+ server {
+ listen 127.0.0.1:8080 proxy_protocol;
+ server_name localhost;
+
+ add_header X-PP-Server-Addr $proxy_protocol_server_addr;
+ add_header X-PP-Server-Port $proxy_protocol_server_port;
+ add_header X-Remote-Addr $server_addr;
+ add_header X-Remote-Port $server_port;
+
+ location /log {
+ access_log %%TESTDIR%%/port.log port;
+ }
+ }
+}
+
+EOF
+
+$t->write_file('t1', 'SEE-THIS');
+$t->run()->plan(13);
+
+###############################################################################
+
+my $tcp4 = 'PROXY TCP4 192.0.2.1 192.0.2.2 123 5678' . CRLF;
+my $tcp6 = 'PROXY TCP6 2001:Db8::1 2001:Db8::2 123 5678' . CRLF;
+my $unk = 'PROXY UNKNOWN 1 2 3 4 5 6' . CRLF;
+my $r;
+
+# PROXY header parsing
+
+$r = pp_get('/t1', $tcp4);
+like($r, qr/SEE-THIS/, 'tcp4 request');
+like($r, qr/X-PP-Server-Addr: 192.0.2.2/, 'tcp4 pp server addr');
+like($r, qr/X-PP-Server-Port: 5678/, 'tcp4 pp server port');
+like($r, qr/X-Remote-Port: 8080/, 'tcp4 real server port');
+
+$r = pp_get('/t1', $tcp6);
+like($r, qr/SEE-THIS/, 'tcp6 request');
+like($r, qr/X-PP-Server-Addr: 2001:DB8::2/i, 'tcp6 pp server addr');
+like($r, qr/X-PP-Server-Port: 5678/, 'tcp6 pp server port');
+like($r, qr/X-Remote-Port: 8080/, 'tcp6 real server port');
+
+$r = pp_get('/t1', $unk);
+like($r, qr/SEE-THIS/, 'tcp6 request');
+unlike($r, qr/X-PP-Server-Addr/, 'unknown pp server addr');
+unlike($r, qr/X-PP-Server-Port/, 'unknown pp server port');
+like($r, qr/X-Remote-Port: 8080/, 'unknown real server port');
+
+# log
+
+pp_get('/log', $tcp4);
+
+$t->stop();
+
+my $log = $t->read_file('/port.log');
+chomp $log;
+
+is($log, 5678, 'pp port log');
+
+###############################################################################
+
+sub pp_get {
+ my ($url, $proxy) = @_;
+ return http($proxy . <<EOF);
+GET $url HTTP/1.0
+Host: localhost
+
+EOF
+}
+
+###############################################################################
More information about the nginx-devel
mailing list