Mercurial > hg > nginx-tests
comparison proxy_unfinished.t @ 299:44c42894fdfd
Tests: move unfinished tests to a separate file.
Add more tests to catch unfinished chunked responses, as well as
proxy-only aspect of the problem (we shouldn't send final chunk if
we know the response isn't complete).
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 11 Jun 2013 17:14:37 +0400 |
parents | |
children | 81c98592661f |
comparison
equal
deleted
inserted
replaced
298:e491290fe83a | 299:44c42894fdfd |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Tests for http proxy and prematurely closed connections. Incomplete | |
6 # responses shouldn't loose information about their incompleteness. | |
7 | |
8 # In particular, incomplete responses: | |
9 # | |
10 # - shouldn't be cached | |
11 # | |
12 # - if a response is sent using chunked transfer encoding, | |
13 # final chunk shouldn't be sent | |
14 | |
15 ############################################################################### | |
16 | |
17 use warnings; | |
18 use strict; | |
19 | |
20 use Test::More; | |
21 | |
22 use Socket qw/ CRLF /; | |
23 | |
24 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
25 | |
26 use lib 'lib'; | |
27 use Test::Nginx; | |
28 | |
29 ############################################################################### | |
30 | |
31 select STDERR; $| = 1; | |
32 select STDOUT; $| = 1; | |
33 | |
34 plan(skip_all => 'win32') if $^O eq 'MSWin32'; | |
35 | |
36 my $t = Test::Nginx->new()->has(qw/http proxy cache sub/)->plan(4) | |
37 ->write_file_expand('nginx.conf', <<'EOF'); | |
38 | |
39 %%TEST_GLOBALS%% | |
40 | |
41 daemon off; | |
42 | |
43 events { | |
44 } | |
45 | |
46 http { | |
47 %%TEST_GLOBALS_HTTP%% | |
48 | |
49 proxy_cache_path %%TESTDIR%%/cache levels=1:2 | |
50 keys_zone=one:1m; | |
51 | |
52 server { | |
53 listen 127.0.0.1:8080; | |
54 server_name localhost; | |
55 | |
56 location / { | |
57 sub_filter foo bar; | |
58 sub_filter_types *; | |
59 proxy_pass http://127.0.0.1:8081; | |
60 } | |
61 | |
62 location /cache/ { | |
63 proxy_pass http://127.0.0.1:8081/; | |
64 proxy_cache one; | |
65 } | |
66 } | |
67 } | |
68 | |
69 EOF | |
70 | |
71 $t->run_daemon(\&http_daemon); | |
72 $t->run()->waitforsocket('127.0.0.1:8081'); | |
73 | |
74 ############################################################################### | |
75 | |
76 my ($r, $n); | |
77 | |
78 $r = http_get('/cache/length'); | |
79 $r =~ m/unfinished (\d+)/; $n = $1 + 1; | |
80 like(http_get('/cache/length'), qr/unfinished $n/, 'unfinished not cached'); | |
81 | |
82 TODO: { | |
83 local $TODO = 'not yet'; | |
84 | |
85 # chunked encoding has enough information to don't cache a response, | |
86 # much like with Content-Length available | |
87 | |
88 $r = http_get('/cache/chunked'); | |
89 $r =~ m/unfinished (\d+)/; $n = $1 + 1; | |
90 like(http_get('/cache/chunked'), qr/unfinished $n/, 'unfinished chunked'); | |
91 | |
92 } | |
93 | |
94 TODO: { | |
95 local $TODO = 'not yet'; | |
96 | |
97 # make sure there is no final chunk in normal responses | |
98 | |
99 like(http_get_11('/length'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s, | |
100 'length no final chunk'); | |
101 like(http_get_11('/chunked'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s, | |
102 'chunked no final chunk'); | |
103 | |
104 } | |
105 | |
106 ############################################################################### | |
107 | |
108 sub http_get_11 { | |
109 my ($uri) = @_; | |
110 | |
111 return http( | |
112 "GET $uri HTTP/1.1" . CRLF . | |
113 "Connection: close" . CRLF . | |
114 "Host: localhost" . CRLF . CRLF | |
115 ); | |
116 } | |
117 | |
118 ############################################################################### | |
119 | |
120 sub http_daemon { | |
121 my $server = IO::Socket::INET->new( | |
122 Proto => 'tcp', | |
123 LocalAddr => '127.0.0.1:8081', | |
124 Listen => 5, | |
125 Reuse => 1 | |
126 ) | |
127 or die "Can't create listening socket: $!\n"; | |
128 | |
129 local $SIG{PIPE} = 'IGNORE'; | |
130 | |
131 my $num = 0; | |
132 | |
133 while (my $client = $server->accept()) { | |
134 $client->autoflush(1); | |
135 | |
136 my $headers = ''; | |
137 my $uri = ''; | |
138 | |
139 while (<$client>) { | |
140 $headers .= $_; | |
141 last if (/^\x0d?\x0a?$/); | |
142 } | |
143 | |
144 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; | |
145 $num++; | |
146 | |
147 if ($uri eq '/length') { | |
148 print $client | |
149 "HTTP/1.1 200 OK" . CRLF . | |
150 "Content-Length: 100" . CRLF . | |
151 "Cache-Control: max-age=300" . CRLF . | |
152 "Connection: close" . CRLF . | |
153 CRLF . | |
154 "unfinished $num" . CRLF; | |
155 | |
156 } elsif ($uri eq '/chunked') { | |
157 print $client | |
158 "HTTP/1.1 200 OK" . CRLF . | |
159 "Transfer-Encoding: chunked" . CRLF . | |
160 "Cache-Control: max-age=300" . CRLF . | |
161 "Connection: close" . CRLF . | |
162 CRLF . | |
163 "ff" . CRLF . | |
164 "unfinished $num" . CRLF; | |
165 } | |
166 } | |
167 } | |
168 | |
169 ############################################################################### |