Mercurial > hg > nginx-tests
comparison scgi_extra_data.t @ 1581:463d6863d360
Tests: tests for extra data and short responses.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 06 Jul 2020 18:37:20 +0300 |
parents | |
children | 6128590b0d46 |
comparison
equal
deleted
inserted
replaced
1580:9e142c0e34b2 | 1581:463d6863d360 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Test for scgi backend with extra data. | |
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 eval { require SCGI; }; | |
26 plan(skip_all => 'SCGI not installed') if $@; | |
27 | |
28 my $t = Test::Nginx->new() | |
29 ->has(qw/http scgi cache rewrite addition/)->plan(20) | |
30 ->write_file_expand('nginx.conf', <<'EOF'); | |
31 | |
32 %%TEST_GLOBALS%% | |
33 | |
34 daemon off; | |
35 | |
36 events { | |
37 } | |
38 | |
39 http { | |
40 %%TEST_GLOBALS_HTTP%% | |
41 | |
42 scgi_param SCGI 1; | |
43 scgi_param REQUEST_URI $request_uri; | |
44 scgi_param REQUEST_METHOD $request_method; | |
45 | |
46 scgi_cache_path cache keys_zone=one:1m; | |
47 scgi_cache_key $request_uri; | |
48 scgi_cache_valid any 1m; | |
49 | |
50 server { | |
51 listen 127.0.0.1:8080; | |
52 server_name localhost; | |
53 | |
54 location / { | |
55 scgi_pass 127.0.0.1:8081; | |
56 add_after_body /after; | |
57 } | |
58 | |
59 location /unbuf/ { | |
60 scgi_pass 127.0.0.1:8081; | |
61 scgi_buffering off; | |
62 add_after_body /after; | |
63 } | |
64 | |
65 location /head/ { | |
66 scgi_pass 127.0.0.1:8081; | |
67 scgi_cache one; | |
68 add_after_body /after; | |
69 } | |
70 | |
71 location /after { | |
72 return 200 ":after\n"; | |
73 } | |
74 } | |
75 } | |
76 | |
77 EOF | |
78 | |
79 $t->run_daemon(\&scgi_daemon); | |
80 $t->run()->waitforsocket('127.0.0.1:' . port(8081)); | |
81 | |
82 ############################################################################### | |
83 | |
84 TODO: { | |
85 local $TODO = 'not yet' unless $t->has_version('1.19.1'); | |
86 | |
87 like(http_get('/'), qr/SEE-THIS(?!-BUT-NOT-THIS)/, 'response with extra data'); | |
88 like(http_get('/short'), qr/SEE-THIS(?!.*:after)/s, 'too short response'); | |
89 like(http_get('/empty'), qr/200 OK(?!.*:after)/s, 'empty too short response'); | |
90 | |
91 } | |
92 | |
93 like(http_head('/'), qr/200 OK(?!.*SEE-THIS)/s, 'no data in HEAD'); | |
94 like(http_head('/short'), qr/200 OK(?!.*SEE-THIS)/s, 'too short to HEAD'); | |
95 like(http_head('/empty'), qr/200 OK/, 'empty response to HEAD'); | |
96 | |
97 # unbuffered responses | |
98 | |
99 TODO: { | |
100 local $TODO = 'not yet' unless $t->has_version('1.19.1'); | |
101 | |
102 like(http_get('/unbuf/'), qr/SEE-THIS(?!-BUT-NOT-THIS)/, | |
103 'unbuffered with extra data'); | |
104 like(http_get('/unbuf/short'), qr/SEE-THIS(?!.*:after)/s, | |
105 'unbuffered too short response'); | |
106 like(http_get('/unbuf/empty'), qr/200 OK(?!.*:after)/s, | |
107 'unbuffered empty too short response'); | |
108 | |
109 } | |
110 | |
111 like(http_head('/unbuf/'), qr/200 OK(?!.*SEE-THIS)/s, | |
112 'unbuffered no data in HEAD'); | |
113 like(http_head('/unbuf/short'), qr/200 OK(?!.*SEE-THIS)/s, | |
114 'unbuffered too short response to HEAD'); | |
115 like(http_head('/unbuf/empty'), qr/200 OK/, | |
116 'unbuffered empty response to HEAD'); | |
117 | |
118 # caching of responsses to HEAD requests | |
119 | |
120 like(http_head('/head/empty'), qr/200 OK(?!.*SEE-THIS)/s, 'head no body'); | |
121 like(http_head('/head/matching'), qr/200 OK(?!.*SEE-THIS)/s, 'head matching'); | |
122 like(http_head('/head/extra'), qr/200 OK(?!.*SEE-THIS)/s, 'head extra'); | |
123 like(http_head('/head/short'), qr/200 OK(?!.*SEE-THIS)/s, 'head too short'); | |
124 | |
125 like(http_get('/head/empty'), qr/SEE-THIS/, 'head no body cached'); | |
126 like(http_get('/head/matching'), qr/SEE-THIS/, 'head matching cached'); | |
127 | |
128 TODO: { | |
129 local $TODO = 'not yet' unless $t->has_version('1.19.1'); | |
130 | |
131 like(http_get('/head/extra'), qr/SEE-THIS(?!-BUT-NOT-THIS)/s, | |
132 'head extra cached'); | |
133 like(http_get('/head/short'), qr/SEE-THIS(?!.*:after)/s, | |
134 'head too short cached'); | |
135 | |
136 } | |
137 | |
138 ############################################################################### | |
139 | |
140 sub scgi_daemon { | |
141 my $server = IO::Socket::INET->new( | |
142 Proto => 'tcp', | |
143 LocalHost => '127.0.0.1:' . port(8081), | |
144 Listen => 5, | |
145 Reuse => 1 | |
146 ) | |
147 or die "Can't create listening socket: $!\n"; | |
148 | |
149 my $scgi = SCGI->new($server, blocking => 1); | |
150 my ($c, $uri, $head); | |
151 | |
152 while (my $request = $scgi->accept()) { | |
153 eval { $request->read_env(); }; | |
154 next if $@; | |
155 | |
156 $uri = $request->env->{REQUEST_URI}; | |
157 $uri =~ s!^/unbuf!!; | |
158 | |
159 $head = $request->env->{REQUEST_METHOD} eq 'HEAD'; | |
160 | |
161 $c = $request->connection(); | |
162 | |
163 if ($uri eq '/') { | |
164 $c->print("Content-Type: text/html\n"); | |
165 $c->print("Content-Length: 8\n\n"); | |
166 $c->print("SEE-THIS-BUT-NOT-THIS\n"); | |
167 | |
168 } elsif ($uri eq '/short') { | |
169 $c->print("Content-Type: text/html\n"); | |
170 $c->print("Content-Length: 100\n\n"); | |
171 $c->print("SEE-THIS-TOO-SHORT-RESPONSE\n"); | |
172 | |
173 } elsif ($uri eq '/empty') { | |
174 $c->print("Content-Type: text/html\n"); | |
175 $c->print("Content-Length: 100\n\n"); | |
176 | |
177 } elsif ($uri eq '/head/empty') { | |
178 $c->print("Content-Type: text/html\n"); | |
179 $c->print("Content-Length: 8\n\n"); | |
180 $c->print("SEE-THIS") unless $head; | |
181 | |
182 } elsif ($uri eq '/head/matching') { | |
183 $c->print("Content-Type: text/html\n"); | |
184 $c->print("Content-Length: 8\n\n"); | |
185 $c->print("SEE-THIS"); | |
186 | |
187 } elsif ($uri eq '/head/extra') { | |
188 $c->print("Content-Type: text/html\n"); | |
189 $c->print("Content-Length: 8\n\n"); | |
190 $c->print("SEE-THIS-BUT-NOT-THIS\n"); | |
191 | |
192 } elsif ($uri eq '/head/short') { | |
193 $c->print("Content-Type: text/html\n"); | |
194 $c->print("Content-Length: 100\n\n"); | |
195 $c->print("SEE-THIS\n"); | |
196 } | |
197 } | |
198 } | |
199 | |
200 ############################################################################### |