Andrey Zelenkov | 82aff46 | 2016-03-23 17:23:08 +0300 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # (C) Sergey Kandaurov |
| 4 | # (C) Nginx, Inc. |
| 5 | |
| 6 | # Tests for HTTP/2 protocol with proxy_protocol. |
| 7 | |
| 8 | ############################################################################### |
| 9 | |
| 10 | use warnings; |
| 11 | use strict; |
| 12 | |
| 13 | use Test::More; |
| 14 | |
| 15 | use Socket qw/ CRLF /; |
| 16 | |
| 17 | BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 18 | |
| 19 | use lib 'lib'; |
| 20 | use Test::Nginx; |
| 21 | use Test::Nginx::HTTP2; |
| 22 | |
| 23 | ############################################################################### |
| 24 | |
| 25 | select STDERR; $| = 1; |
| 26 | select STDOUT; $| = 1; |
| 27 | |
| 28 | my $t = Test::Nginx->new()->has(qw/http http_v2 realip/)->plan(4) |
| 29 | ->write_file_expand('nginx.conf', <<'EOF'); |
| 30 | |
| 31 | %%TEST_GLOBALS%% |
| 32 | |
| 33 | daemon off; |
| 34 | |
| 35 | events { |
| 36 | } |
| 37 | |
| 38 | http { |
| 39 | %%TEST_GLOBALS_HTTP%% |
| 40 | |
| 41 | server { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 42 | listen 127.0.0.1:8080 proxy_protocol http2; |
Andrey Zelenkov | 82aff46 | 2016-03-23 17:23:08 +0300 | [diff] [blame] | 43 | server_name localhost; |
| 44 | |
| 45 | location /pp { |
| 46 | set_real_ip_from 127.0.0.1/32; |
| 47 | real_ip_header proxy_protocol; |
| 48 | alias %%TESTDIR%%/t.html; |
| 49 | add_header X-PP $remote_addr; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | EOF |
| 55 | |
| 56 | $t->write_file('t.html', 'SEE-THIS'); |
| 57 | $t->run(); |
| 58 | |
| 59 | ############################################################################### |
| 60 | |
| 61 | my $proxy = 'PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678' . CRLF; |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 62 | my $s = Test::Nginx::HTTP2->new(port(8080), proxy => $proxy); |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 63 | my $sid = $s->new_stream({ path => '/pp' }); |
| 64 | my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); |
Andrey Zelenkov | 82aff46 | 2016-03-23 17:23:08 +0300 | [diff] [blame] | 65 | |
| 66 | my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; |
| 67 | ok($frame, 'PROXY HEADERS frame'); |
| 68 | is($frame->{headers}->{'x-pp'}, '192.0.2.1', 'PROXY remote addr'); |
| 69 | |
| 70 | # invalid PROXY protocol string |
| 71 | |
Sergey Kandaurov | 6e674de | 2016-06-15 19:00:45 +0300 | [diff] [blame] | 72 | $proxy = 'BOGUS TCP4 192.0.2.1 192.0.2.2 1234 5678' . CRLF; |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 73 | $s = Test::Nginx::HTTP2->new(port(8080), preface => $proxy, pure => 1); |
Sergey Kandaurov | 67599f4 | 2016-06-17 11:36:33 +0300 | [diff] [blame] | 74 | $frames = $s->read(all => [{ type => 'GOAWAY' }]); |
Andrey Zelenkov | 82aff46 | 2016-03-23 17:23:08 +0300 | [diff] [blame] | 75 | |
| 76 | ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; |
| 77 | ok($frame, 'invalid PROXY - GOAWAY frame'); |
| 78 | is($frame->{code}, 1, 'invalid PROXY - error code'); |
| 79 | |
| 80 | ############################################################################### |