blob: 9a597d4348e85cb3de13075b96e1b664f059b3c6 [file] [log] [blame]
Andrey Zelenkov82aff462016-03-23 17:23:08 +03001#!/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
10use warnings;
11use strict;
12
13use Test::More;
14
15use Socket qw/ CRLF /;
16
17BEGIN { use FindBin; chdir($FindBin::Bin); }
18
19use lib 'lib';
20use Test::Nginx;
21use Test::Nginx::HTTP2;
22
23###############################################################################
24
25select STDERR; $| = 1;
26select STDOUT; $| = 1;
27
28my $t = Test::Nginx->new()->has(qw/http http_v2 realip/)->plan(4)
29 ->write_file_expand('nginx.conf', <<'EOF');
30
31%%TEST_GLOBALS%%
32
33daemon off;
34
35events {
36}
37
38http {
39 %%TEST_GLOBALS_HTTP%%
40
41 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030042 listen 127.0.0.1:8080 proxy_protocol http2;
Andrey Zelenkov82aff462016-03-23 17:23:08 +030043 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
54EOF
55
56$t->write_file('t.html', 'SEE-THIS');
57$t->run();
58
59###############################################################################
60
61my $proxy = 'PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678' . CRLF;
Andrey Zelenkove59bf362016-07-12 17:39:03 +030062my $s = Test::Nginx::HTTP2->new(port(8080), proxy => $proxy);
Sergey Kandaurov67599f42016-06-17 11:36:33 +030063my $sid = $s->new_stream({ path => '/pp' });
64my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
Andrey Zelenkov82aff462016-03-23 17:23:08 +030065
66my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
67ok($frame, 'PROXY HEADERS frame');
68is($frame->{headers}->{'x-pp'}, '192.0.2.1', 'PROXY remote addr');
69
70# invalid PROXY protocol string
71
Sergey Kandaurov6e674de2016-06-15 19:00:45 +030072$proxy = 'BOGUS TCP4 192.0.2.1 192.0.2.2 1234 5678' . CRLF;
Andrey Zelenkove59bf362016-07-12 17:39:03 +030073$s = Test::Nginx::HTTP2->new(port(8080), preface => $proxy, pure => 1);
Sergey Kandaurov67599f42016-06-17 11:36:33 +030074$frames = $s->read(all => [{ type => 'GOAWAY' }]);
Andrey Zelenkov82aff462016-03-23 17:23:08 +030075
76($frame) = grep { $_->{type} eq "GOAWAY" } @$frames;
77ok($frame, 'invalid PROXY - GOAWAY frame');
78is($frame->{code}, 1, 'invalid PROXY - error code');
79
80###############################################################################