comparison proxy_duplicate_headers.t @ 1769:735226e4c7fe

Tests: tests for duplicate response headers.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 30 May 2022 21:34:22 +0300
parents
children
comparison
equal deleted inserted replaced
1768:fb88778d4580 1769:735226e4c7fe
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4
5 # Test for http backend returning response with invalid and duplicate
6 # headers.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14 use Socket qw/ CRLF /;
15
16 BEGIN { use FindBin; chdir($FindBin::Bin); }
17
18 use lib 'lib';
19 use Test::Nginx;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(8);
27
28 $t->write_file_expand('nginx.conf', <<'EOF');
29
30 %%TEST_GLOBALS%%
31
32 daemon off;
33
34 events {
35 }
36
37 http {
38 %%TEST_GLOBALS_HTTP%%
39
40 server {
41 listen 127.0.0.1:8080;
42 server_name localhost;
43
44 location / {
45 proxy_pass http://127.0.0.1:8081;
46 proxy_read_timeout 1s;
47 }
48 }
49 }
50
51 EOF
52
53 $t->run_daemon(\&http_daemon);
54 $t->run()->waitforsocket('127.0.0.1:' . port(8081));
55
56 ###############################################################################
57
58 like(http_get('/'), qr/200 OK/, 'normal');
59
60 TODO: {
61 local $TODO = 'not yet' unless $t->has_version('1.23.0');
62
63 like(http_get('/invalid-length'), qr/502 Bad/, 'invalid length');
64 like(http_get('/duplicate-length'), qr/502 Bad/, 'duplicate length');
65 like(http_get('/unknown-transfer-encoding'), qr/502 Bad/,
66 'unknown transfer encoding');
67 like(http_get('/duplicate-transfer-encoding'), qr/502 Bad/,
68 'duplicate transfer encoding');
69 like(http_get('/length-and-transfer-encoding'), qr/502 Bad/,
70 'length and transfer encoding');
71 like(http_get('/transfer-encoding-and-length'), qr/502 Bad/,
72 'transfer encoding and length');
73
74 like(http_get('/duplicate-expires'), qr/Expires: foo(?!.*bar)/s,
75 'duplicate expires ignored');
76
77 }
78
79 ###############################################################################
80
81 sub http_daemon {
82 my $server = IO::Socket::INET->new(
83 Proto => 'tcp',
84 LocalAddr => '127.0.0.1:' . port(8081),
85 Listen => 5,
86 Reuse => 1
87 )
88 or die "Can't create listening socket: $!\n";
89
90 local $SIG{PIPE} = 'IGNORE';
91
92 while (my $client = $server->accept()) {
93 $client->autoflush(1);
94
95 my $headers = '';
96 my $uri = '';
97
98 while (<$client>) {
99 $headers .= $_;
100 last if (/^\x0d?\x0a?$/);
101 }
102
103 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
104
105 if ($uri eq '/') {
106
107 print $client
108 'HTTP/1.1 200 OK' . CRLF .
109 'Connection: close' . CRLF .
110 'Content-Length: 0' . CRLF . CRLF;
111
112 } elsif ($uri eq '/invalid-length') {
113
114 print $client
115 'HTTP/1.1 200 OK' . CRLF .
116 'Connection: close' . CRLF .
117 'Content-Length: foo' . CRLF . CRLF;
118
119 } elsif ($uri eq '/duplicate-length') {
120
121 print $client
122 'HTTP/1.1 200 OK' . CRLF .
123 'Connection: close' . CRLF .
124 'Content-Length: 0' . CRLF .
125 'Content-Length: 0' . CRLF . CRLF;
126
127 } elsif ($uri eq '/unknown-transfer-encoding') {
128
129 print $client
130 'HTTP/1.1 200 OK' . CRLF .
131 'Connection: close' . CRLF .
132 'Transfer-Encoding: foo' . CRLF . CRLF;
133
134 } elsif ($uri eq '/duplicate-transfer-encoding') {
135
136 print $client
137 'HTTP/1.1 200 OK' . CRLF .
138 'Connection: close' . CRLF .
139 'Transfer-Encoding: chunked' . CRLF .
140 'Transfer-Encoding: chunked' . CRLF . CRLF .
141 '0' . CRLF . CRLF;
142
143 } elsif ($uri eq '/length-and-transfer-encoding') {
144
145 print $client
146 'HTTP/1.1 200 OK' . CRLF .
147 'Connection: close' . CRLF .
148 'Content-Length: 0' . CRLF .
149 'Transfer-Encoding: chunked' . CRLF . CRLF .
150 '0' . CRLF . CRLF;
151
152 } elsif ($uri eq '/transfer-encoding-and-length') {
153
154 print $client
155 'HTTP/1.1 200 OK' . CRLF .
156 'Connection: close' . CRLF .
157 'Transfer-Encoding: chunked' . CRLF .
158 'Content-Length: 0' . CRLF . CRLF .
159 '0' . CRLF . CRLF;
160
161 } elsif ($uri eq '/duplicate-expires') {
162
163 print $client
164 'HTTP/1.1 200 OK' . CRLF .
165 'Connection: close' . CRLF .
166 'Expires: foo' . CRLF .
167 'Expires: bar' . CRLF . CRLF;
168
169 }
170
171 close $client;
172 }
173 }
174
175 ###############################################################################