comparison fastcgi_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 e145509cc6eb
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 fastcgi backend, responses with extra data or premature close.
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 FCGI; };
26 plan(skip_all => 'FCGI not installed') if $@;
27 plan(skip_all => 'win32') if $^O eq 'MSWin32';
28
29 my $t = Test::Nginx->new()
30 ->has(qw/http fastcgi cache rewrite addition/)->plan(20)
31 ->write_file_expand('nginx.conf', <<'EOF');
32
33 %%TEST_GLOBALS%%
34
35 daemon off;
36
37 events {
38 }
39
40 http {
41 %%TEST_GLOBALS_HTTP%%
42
43 fastcgi_param REQUEST_URI $request_uri;
44 fastcgi_param REQUEST_METHOD $request_method;
45
46 fastcgi_cache_path cache keys_zone=one:1m;
47 fastcgi_cache_key $request_uri;
48 fastcgi_cache_valid any 1m;
49
50 server {
51 listen 127.0.0.1:8080;
52 server_name localhost;
53
54 location / {
55 fastcgi_pass 127.0.0.1:8081;
56 add_after_body /after;
57 }
58
59 location /unbuf/ {
60 fastcgi_pass 127.0.0.1:8081;
61 fastcgi_buffering off;
62 add_after_body /after;
63 }
64
65 location /head/ {
66 fastcgi_pass 127.0.0.1:8081;
67 fastcgi_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(\&fastcgi_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 responsde');
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/200 OK/, '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 fastcgi_daemon {
141 my $socket = FCGI::OpenSocket('127.0.0.1:' . port(8081), 5);
142 my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV,
143 $socket);
144
145 my ($uri, $head);
146
147 while( $request->Accept() >= 0 ) {
148 $uri = $ENV{REQUEST_URI};
149 $uri =~ s!^/unbuf!!;
150
151 $head = $ENV{REQUEST_METHOD} eq 'HEAD';
152
153 if ($uri eq '/') {
154 print "Content-Type: text/html\n";
155 print "Content-Length: 8\n\n";
156 print "SEE-THIS-BUT-NOT-THIS\n";
157
158 } elsif ($uri eq '/short') {
159 print "Content-Type: text/html\n";
160 print "Content-Length: 100\n\n";
161 print "SEE-THIS-TOO-SHORT-RESPONSE\n";
162
163 } elsif ($uri eq '/empty') {
164 print "Content-Type: text/html\n";
165 print "Content-Length: 100\n\n";
166
167 } elsif ($uri eq '/head/empty') {
168 print "Content-Type: text/html\n";
169 print "Content-Length: 8\n\n";
170 print "SEE-THIS" unless $head;
171
172 } elsif ($uri eq '/head/matching') {
173 print "Content-Type: text/html\n";
174 print "Content-Length: 8\n\n";
175 print "SEE-THIS";
176
177 } elsif ($uri eq '/head/extra') {
178 print "Content-Type: text/html\n";
179 print "Content-Length: 8\n\n";
180 print "SEE-THIS-BUT-NOT-THIS\n";
181
182 } elsif ($uri eq '/head/short') {
183 print "Content-Type: text/html\n";
184 print "Content-Length: 100\n\n";
185 print "SEE-THIS\n";
186 }
187 }
188
189 FCGI::CloseSocket($socket);
190 }
191
192 ###############################################################################