comparison proxy_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 # Tests for http 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 my $t = Test::Nginx->new()
26 ->has(qw/http proxy cache rewrite addition/)->plan(20)
27 ->write_file_expand('nginx.conf', <<'EOF');
28
29 %%TEST_GLOBALS%%
30
31 daemon off;
32
33 events {
34 }
35
36 http {
37 %%TEST_GLOBALS_HTTP%%
38
39 proxy_cache_path cache keys_zone=one:1m;
40 proxy_cache_key $request_uri;
41 proxy_cache_valid any 1m;
42
43 server {
44 listen 127.0.0.1:8080;
45 server_name localhost;
46
47 location / {
48 proxy_pass http://127.0.0.1:8081;
49 add_after_body /after;
50 }
51
52 location /unbuf/ {
53 proxy_pass http://127.0.0.1:8081;
54 proxy_buffering off;
55 add_after_body /after;
56 }
57
58 location /head/ {
59 proxy_pass http://127.0.0.1:8081;
60 proxy_cache one;
61 add_after_body /after;
62 }
63
64 location /after {
65 return 200 ":after\n";
66 }
67 }
68 }
69
70 EOF
71
72 $t->run_daemon(\&http_daemon);
73 $t->run()->waitforsocket('127.0.0.1:' . port(8081));
74
75 ###############################################################################
76
77 TODO: {
78 local $TODO = 'not yet' unless $t->has_version('1.19.1');
79
80 like(http_get('/'), qr/SEE-THIS(?!-BUT-NOT-THIS)/, 'response with extra data');
81
82 }
83
84 like(http_get('/short'), qr/SEE-THIS(?!.*:after)/s, 'too short response');
85 like(http_get('/empty'), qr/200 OK(?!.*:after)/s, 'empty too short response');
86
87 like(http_head('/'), qr/200 OK(?!.*SEE-THIS)/s, 'no data in HEAD');
88 like(http_head('/short'), qr/200 OK(?!.*SEE-THIS)/s, 'too short to HEAD');
89 like(http_head('/empty'), qr/200 OK/, 'empty response to HEAD');
90
91 # unbuffered responses
92
93 TODO: {
94 local $TODO = 'not yet' unless $t->has_version('1.19.1');
95
96 like(http_get('/unbuf/'), qr/SEE-THIS(?!-BUT-NOT-THIS)/,
97 'unbuffered with extra data');
98
99 }
100
101 like(http_get('/unbuf/short'), qr/SEE-THIS(?!.*:after)/s,
102 'unbuffered too short response');
103 like(http_get('/unbuf/empty'), qr/200 OK(?!.*:after)/s,
104 'unbuffered empty too short response');
105
106 like(http_head('/unbuf/'), qr/200 OK(?!.*SEE-THIS)/s,
107 'unbuffered no data in HEAD');
108 like(http_head('/unbuf/short'), qr/200 OK(?!.*SEE-THIS)/s,
109 'unbuffered too short response to HEAD');
110 like(http_head('/unbuf/empty'), qr/200 OK/,
111 'unbuffered empty response to HEAD');
112
113 # caching of responsses to HEAD requests
114
115 like(http_head('/head/empty'), qr/200 OK(?!.*SEE-THIS)/s, 'head no body');
116 like(http_head('/head/matching'), qr/200 OK(?!.*SEE-THIS)/s, 'head matching');
117 like(http_head('/head/extra'), qr/200 OK(?!.*SEE-THIS)/s, 'head extra');
118 like(http_head('/head/short'), qr/200 OK(?!.*SEE-THIS)/s, 'head too short');
119
120 like(http_get('/head/empty'), qr/SEE-THIS/, 'head no body cached');
121 like(http_get('/head/matching'), qr/SEE-THIS/, 'head matching cached');
122
123 TODO: {
124 local $TODO = 'not yet' unless $t->has_version('1.19.1');
125
126 like(http_get('/head/extra'), qr/SEE-THIS(?!-BUT-NOT-THIS)/s,
127 'head extra cached');
128
129 }
130
131 like(http_get('/head/short'), qr/SEE-THIS(?!.*:after)/s,
132 'head too short cached');
133
134 ###############################################################################
135
136 sub http_daemon {
137 my $server = IO::Socket::INET->new(
138 Proto => 'tcp',
139 LocalHost => '127.0.0.1:' . port(8081),
140 Listen => 5,
141 Reuse => 1
142 )
143 or die "Can't create listening socket: $!\n";
144
145 local $SIG{PIPE} = 'IGNORE';
146
147 my ($uri, $head);
148
149 while (my $c = $server->accept()) {
150 $c->autoflush(1);
151
152 my $headers = '';
153 my $uri = '';
154
155 while (<$c>) {
156 $headers .= $_;
157 last if (/^\x0d?\x0a?$/);
158 }
159
160 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
161 $uri =~ s!^/unbuf!!;
162
163 $head = ($headers =~ /^HEAD/);
164
165 if ($uri eq '/') {
166 $c->print("HTTP/1.1 200 OK\n");
167 $c->print("Content-Type: text/html\n");
168 $c->print("Content-Length: 8\n\n");
169 $c->print("SEE-THIS-BUT-NOT-THIS\n");
170
171 } elsif ($uri eq '/short') {
172 $c->print("HTTP/1.1 200 OK\n");
173 $c->print("Content-Type: text/html\n");
174 $c->print("Content-Length: 100\n\n");
175 $c->print("SEE-THIS-TOO-SHORT-RESPONSE\n");
176
177 } elsif ($uri eq '/empty') {
178 $c->print("HTTP/1.1 200 OK\n");
179 $c->print("Content-Type: text/html\n");
180 $c->print("Content-Length: 100\n\n");
181
182 } elsif ($uri eq '/head/empty') {
183 $c->print("HTTP/1.1 200 OK\n");
184 $c->print("Content-Type: text/html\n");
185 $c->print("Content-Length: 8\n\n");
186 $c->print("SEE-THIS") unless $head;
187
188 } elsif ($uri eq '/head/matching') {
189 $c->print("HTTP/1.1 200 OK\n");
190 $c->print("Content-Type: text/html\n");
191 $c->print("Content-Length: 8\n\n");
192 $c->print("SEE-THIS");
193
194 } elsif ($uri eq '/head/extra') {
195 $c->print("HTTP/1.1 200 OK\n");
196 $c->print("Content-Type: text/html\n");
197 $c->print("Content-Length: 8\n\n");
198 $c->print("SEE-THIS-BUT-NOT-THIS\n");
199
200 } elsif ($uri eq '/head/short') {
201 $c->print("HTTP/1.1 200 OK\n");
202 $c->print("Content-Type: text/html\n");
203 $c->print("Content-Length: 100\n\n");
204 $c->print("SEE-THIS\n");
205 }
206
207 close $c;
208 }
209 }
210
211 ###############################################################################