comparison proxy_pass_request.t @ 893:4fad3232ad56

Tests: proxy_pass_request_headers, proxy_pass_request_body tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 06 Apr 2016 17:23:51 +0300
parents
children e9064d691790
comparison
equal deleted inserted replaced
892:0c64f87aa689 893:4fad3232ad56
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for proxy_pass_request_headers, proxy_pass_request_body directives.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(3);
26
27 $t->write_file_expand('nginx.conf', <<'EOF');
28
29 %%TEST_GLOBALS%%
30
31 daemon off;
32
33 events {
34 }
35
36 http {
37 %%TEST_GLOBALS_HTTP%%
38
39 server {
40 listen 127.0.0.1:8080;
41 server_name localhost;
42
43 proxy_pass_request_headers off;
44
45 location / {
46 proxy_pass http://127.0.0.1:8081;
47 }
48
49 location /body {
50 proxy_pass http://127.0.0.1:8081;
51 proxy_pass_request_headers on;
52 proxy_pass_request_body off;
53 }
54
55 location /both {
56 proxy_pass http://127.0.0.1:8081;
57 proxy_pass_request_headers off;
58 proxy_pass_request_body off;
59 }
60 }
61 }
62
63 EOF
64
65 $t->run_daemon(\&http_daemon);
66 $t->run()->waitforsocket('127.0.0.1:8081');
67
68 ###############################################################################
69
70 like(get('/', 'foo', 'bar'), qr/Header: none.*Body: bar/s, 'no headers');
71 like(get('/body', 'foo', 'bar'), qr/Header: foo.*Body: none/s, 'no body');
72 like(get('/both', 'foo', 'bar'), qr/Header: none.*Body: none/s, 'both');
73
74 ###############################################################################
75
76 sub get {
77 my ($uri, $header, $body) = @_;
78 my $cl = length("$body\n");
79
80 http(<<EOF);
81 GET $uri HTTP/1.0
82 Host: localhost
83 X-Header: $header
84 Content-Length: $cl
85
86 $body
87 EOF
88 }
89
90 sub http_daemon {
91 my $server = IO::Socket::INET->new(
92 Proto => 'tcp',
93 LocalHost => '127.0.0.1:8081',
94 Listen => 5,
95 Reuse => 1
96 )
97 or die "Can't create listening socket: $!\n";
98
99 local $SIG{PIPE} = 'IGNORE';
100
101 while (my $client = $server->accept()) {
102 $client->autoflush(1);
103
104 my $r = '';
105
106 eval {
107 local $SIG{ALRM} = sub { die "timeout\n" };
108 local $SIG{PIPE} = sub { die "sigpipe\n" };
109 alarm(2);
110 $client->sysread($r, 4096);
111 alarm(0);
112 };
113 alarm(0);
114 if ($@) {
115 log_in("died: $@");
116 next;
117 }
118
119 next if $r eq '';
120
121 Test::Nginx::log_core('|| <<', $r);
122
123 my $header = $r =~ /x-header: (\S+)/i && $1 || 'none';
124 my $body = $r =~ /\x0d\x0a?\x0d\x0a?(.+)/ && $1 || 'none';
125
126 print $client <<"EOF";
127 HTTP/1.1 200 OK
128 Connection: close
129 X-Header: $header
130 X-Body: $body
131
132 EOF
133
134 close $client;
135 }
136 }
137
138 ###############################################################################