-
Notifications
You must be signed in to change notification settings - Fork 75
/
fastcgi_extra_data.t
187 lines (137 loc) · 5.06 KB
/
fastcgi_extra_data.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/perl
# (C) Maxim Dounin
# (C) Nginx, Inc.
# Test for fastcgi backend, responses with extra data or premature close.
###############################################################################
use warnings;
use strict;
use Test::More;
BEGIN { use FindBin; chdir($FindBin::Bin); }
use lib 'lib';
use Test::Nginx;
###############################################################################
select STDERR; $| = 1;
select STDOUT; $| = 1;
eval { require FCGI; };
plan(skip_all => 'FCGI not installed') if $@;
plan(skip_all => 'win32') if $^O eq 'MSWin32';
my $t = Test::Nginx->new()
->has(qw/http fastcgi cache rewrite addition/)->plan(22)
->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_cache_path cache keys_zone=one:1m;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid any 1m;
server {
listen 127.0.0.1:8080;
server_name localhost;
location / {
fastcgi_pass 127.0.0.1:8081;
add_after_body /after;
}
location /unbuf/ {
fastcgi_pass 127.0.0.1:8081;
fastcgi_buffering off;
add_after_body /after;
}
location /head/ {
fastcgi_pass 127.0.0.1:8081;
fastcgi_cache one;
add_after_body /after;
}
location /after {
return 200 ":after\n";
}
}
}
EOF
$t->run_daemon(\&fastcgi_daemon);
$t->run()->waitforsocket('127.0.0.1:' . port(8081));
###############################################################################
like(http_get('/'), qr/SEE-THIS(?!-BUT-NOT-THIS)/, 'response with extra data');
like(http_get('/short'), qr/SEE-THIS(?!.*:after)/s, 'too short response');
like(http_get('/empty'), qr/200 OK(?!.*:after)/s, 'empty too short response');
like(http_head('/'), qr/200 OK(?!.*SEE-THIS)/s, 'no data in HEAD');
like(http_head('/short'), qr/200 OK(?!.*SEE-THIS)/s, 'too short to HEAD');
like(http_head('/empty'), qr/200 OK/, 'empty response to HEAD');
# unbuffered responses
like(http_get('/unbuf/'), qr/SEE-THIS(?!-BUT-NOT-THIS)/,
'unbuffered with extra data');
like(http_get('/unbuf/short'), qr/SEE-THIS(?!.*:after)/s,
'unbuffered too short response');
like(http_get('/unbuf/empty'), qr/200 OK(?!.*:after)/s,
'unbuffered empty too short response');
like(http_head('/unbuf/'), qr/200 OK(?!.*SEE-THIS)/s,
'unbuffered no data in HEAD');
like(http_head('/unbuf/short'), qr/200 OK(?!.*SEE-THIS)/s,
'unbuffered too short response to HEAD');
like(http_head('/unbuf/empty'), qr/200 OK/,
'unbuffered empty response to HEAD');
# caching of responsses to HEAD requests
like(http_head('/head/empty'), qr/200 OK(?!.*SEE-THIS)/s, 'head no body');
like(http_head('/head/matching'), qr/200 OK(?!.*SEE-THIS)/s, 'head matching');
like(http_head('/head/extra'), qr/200 OK(?!.*SEE-THIS)/s, 'head extra');
like(http_head('/head/short'), qr/200 OK(?!.*SEE-THIS)/s, 'head too short');
like(http_get('/head/empty'), qr/200 OK/, 'head no body cached');
like(http_get('/head/matching'), qr/SEE-THIS/, 'head matching cached');
like(http_get('/head/extra'), qr/SEE-THIS(?!-BUT-NOT-THIS)/s,
'head extra cached');
like(http_get('/head/short'), qr/SEE-THIS(?!.*:after)/s,
'head too short cached');
# "zero size buf" alerts (ticket #2018)
like(http_get('/zero'), qr/200 OK(?!.*NOT-THIS)/s, 'zero size');
like(http_get('/unbuf/zero'), qr/200 OK(?!.*NOT-THIS)/s,
'unbuffered zero size');
###############################################################################
sub fastcgi_daemon {
my $socket = FCGI::OpenSocket('127.0.0.1:' . port(8081), 5);
my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV,
$socket);
my ($uri, $head);
while( $request->Accept() >= 0 ) {
$uri = $ENV{REQUEST_URI};
$uri =~ s!^/unbuf!!;
$head = $ENV{REQUEST_METHOD} eq 'HEAD';
if ($uri eq '/') {
print "Content-Type: text/html\n";
print "Content-Length: 8\n\n";
print "SEE-THIS-BUT-NOT-THIS\n";
} elsif ($uri eq '/zero') {
print "Content-Type: text/html\n";
print "Content-Length: 0\n\n";
print "NOT-THIS\n";
} elsif ($uri eq '/short') {
print "Content-Type: text/html\n";
print "Content-Length: 100\n\n";
print "SEE-THIS-TOO-SHORT-RESPONSE\n";
} elsif ($uri eq '/empty') {
print "Content-Type: text/html\n";
print "Content-Length: 100\n\n";
} elsif ($uri eq '/head/empty') {
print "Content-Type: text/html\n";
print "Content-Length: 8\n\n";
print "SEE-THIS" unless $head;
} elsif ($uri eq '/head/matching') {
print "Content-Type: text/html\n";
print "Content-Length: 8\n\n";
print "SEE-THIS";
} elsif ($uri eq '/head/extra') {
print "Content-Type: text/html\n";
print "Content-Length: 8\n\n";
print "SEE-THIS-BUT-NOT-THIS\n";
} elsif ($uri eq '/head/short') {
print "Content-Type: text/html\n";
print "Content-Length: 100\n\n";
print "SEE-THIS\n";
}
}
FCGI::CloseSocket($socket);
}
###############################################################################