Mercurial > hg > nginx-tests
comparison js2.t @ 1341:15f0d9412b22
Tests: for http njs module using the single req object API.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Wed, 13 Jun 2018 13:57:30 +0300 |
parents | js.t@7a0d626ecd94 |
children | 32156faf018e |
comparison
equal
deleted
inserted
replaced
1340:da711694bbd7 | 1341:15f0d9412b22 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Roman Arutyunyan | |
4 # (C) Dmitry Volyntsev | |
5 # (C) Nginx, Inc. | |
6 | |
7 # Tests for http njs module. | |
8 | |
9 ############################################################################### | |
10 | |
11 use warnings; | |
12 use strict; | |
13 | |
14 use Test::More; | |
15 use Socket qw/ CRLF /; | |
16 | |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
18 | |
19 use lib 'lib'; | |
20 use Test::Nginx; | |
21 | |
22 ############################################################################### | |
23 | |
24 select STDERR; $| = 1; | |
25 select STDOUT; $| = 1; | |
26 | |
27 my $t = Test::Nginx->new()->has(qw/http rewrite/) | |
28 ->write_file_expand('nginx.conf', <<'EOF'); | |
29 | |
30 %%TEST_GLOBALS%% | |
31 | |
32 daemon off; | |
33 | |
34 events { | |
35 } | |
36 | |
37 http { | |
38 %%TEST_GLOBALS_HTTP%% | |
39 | |
40 js_set $test_method test_method; | |
41 js_set $test_version test_version; | |
42 js_set $test_addr test_addr; | |
43 js_set $test_uri test_uri; | |
44 js_set $test_hdr test_hdr; | |
45 js_set $test_ihdr test_ihdr; | |
46 js_set $test_arg test_arg; | |
47 js_set $test_iarg test_iarg; | |
48 js_set $test_var test_var; | |
49 js_set $test_log test_log; | |
50 js_set $test_except test_except; | |
51 | |
52 js_include test.js; | |
53 | |
54 server { | |
55 listen 127.0.0.1:8080; | |
56 server_name localhost; | |
57 | |
58 location /njs { | |
59 js_content test_njs; | |
60 } | |
61 | |
62 location /method { | |
63 return 200 $test_method; | |
64 } | |
65 | |
66 location /version { | |
67 return 200 $test_version; | |
68 } | |
69 | |
70 location /addr { | |
71 return 200 $test_addr; | |
72 } | |
73 | |
74 location /uri { | |
75 return 200 $test_uri; | |
76 } | |
77 | |
78 location /hdr_in { | |
79 return 200 $test_hdr; | |
80 } | |
81 | |
82 location /ihdr_in { | |
83 return 200 $test_ihdr; | |
84 } | |
85 | |
86 location /arg { | |
87 return 200 $test_arg; | |
88 } | |
89 | |
90 location /iarg { | |
91 return 200 $test_iarg; | |
92 } | |
93 | |
94 location /var { | |
95 return 200 $test_var; | |
96 } | |
97 | |
98 location /body { | |
99 js_content request_body; | |
100 } | |
101 | |
102 location /in_file { | |
103 client_body_in_file_only on; | |
104 js_content request_body; | |
105 } | |
106 | |
107 location /status { | |
108 js_content status; | |
109 } | |
110 | |
111 location /ctype { | |
112 js_content ctype; | |
113 } | |
114 | |
115 location /clen { | |
116 js_content clen; | |
117 } | |
118 | |
119 location /hdr_out { | |
120 js_content hdr_out; | |
121 } | |
122 | |
123 location /ihdr_out { | |
124 js_content ihdr_out; | |
125 } | |
126 | |
127 location /request_body { | |
128 js_content request_body; | |
129 } | |
130 | |
131 location /send { | |
132 js_content send; | |
133 } | |
134 | |
135 location /return { | |
136 js_content return_method; | |
137 } | |
138 | |
139 location /return_headers { | |
140 js_content return_headers; | |
141 } | |
142 | |
143 location /log { | |
144 return 200 $test_log; | |
145 } | |
146 | |
147 location /var_except { | |
148 return 200 $test_except; | |
149 } | |
150 | |
151 location /content_except { | |
152 js_content content_except; | |
153 } | |
154 | |
155 location /content_empty { | |
156 js_content content_empty; | |
157 } | |
158 } | |
159 } | |
160 | |
161 EOF | |
162 | |
163 $t->write_file('test.js', <<EOF); | |
164 function test_njs(r) { | |
165 r.return(200, njs.version); | |
166 } | |
167 | |
168 function test_method(r) { | |
169 return 'method=' + r.method; | |
170 } | |
171 | |
172 function test_version(r) { | |
173 return 'version=' + r.httpVersion; | |
174 } | |
175 | |
176 function test_addr(r) { | |
177 return 'addr=' + r.remoteAddress; | |
178 } | |
179 | |
180 function test_uri(r) { | |
181 return 'uri=' + r.uri; | |
182 } | |
183 | |
184 function test_hdr(r) { | |
185 return 'hdr=' + r.headersIn.foo; | |
186 } | |
187 | |
188 function test_ihdr(r) { | |
189 var s = '', h; | |
190 for (h in r.headersIn) { | |
191 if (h.substr(0, 3) == 'foo') { | |
192 s += r.headersIn[h]; | |
193 } | |
194 } | |
195 return s; | |
196 } | |
197 | |
198 function test_arg(r) { | |
199 return 'arg=' + r.args.foo; | |
200 } | |
201 | |
202 function test_iarg(r) { | |
203 var s = '', a; | |
204 for (a in r.args) { | |
205 if (a.substr(0, 3) == 'foo') { | |
206 s += r.args[a]; | |
207 } | |
208 } | |
209 return s; | |
210 } | |
211 | |
212 function test_var(r) { | |
213 return 'variable=' + r.variables.remote_addr; | |
214 } | |
215 | |
216 function status(r) { | |
217 r.status = 204; | |
218 if (r.status != 204) | |
219 r.status = 404; | |
220 r.sendHeader(); | |
221 r.finish(); | |
222 } | |
223 | |
224 function ctype(r) { | |
225 r.status = 200; | |
226 r.headersOut['Content-Type'] = 'application/foo'; | |
227 r.sendHeader(); | |
228 r.finish(); | |
229 } | |
230 | |
231 function clen(r) { | |
232 r.status = 200; | |
233 r.headersOut['Content-Length'] = 5; | |
234 if (r.headersOut['Content-Length'] != 5) | |
235 r.headersOut['Content-Length'] = 6; | |
236 r.sendHeader(); | |
237 r.send('foo12'); | |
238 r.finish(); | |
239 } | |
240 | |
241 function hdr_out(r) { | |
242 r.status = 200; | |
243 r.headersOut['Foo'] = r.args.fOO; | |
244 | |
245 if (r.args.bar) { | |
246 r.headersOut['Bar'] = r.headersOut['Foo']; | |
247 } | |
248 | |
249 if (r.args.bar == 'empty') { | |
250 r.headersOut['Bar'] = r.headersOut['Baz']; | |
251 } | |
252 | |
253 r.sendHeader(); | |
254 r.finish(); | |
255 } | |
256 | |
257 function ihdr_out(r) { | |
258 r.status = 200; | |
259 r.headersOut['a'] = r.args.a; | |
260 r.headersOut['b'] = r.args.b; | |
261 | |
262 var s = '', h; | |
263 for (h in r.headersOut) { | |
264 s += r.headersOut[h]; | |
265 } | |
266 | |
267 r.sendHeader(); | |
268 r.send(s); | |
269 r.finish(); | |
270 } | |
271 | |
272 function request_body(r) { | |
273 try { | |
274 var body = r.requestBody; | |
275 r.return(200, body); | |
276 | |
277 } catch (e) { | |
278 r.return(500, e.message); | |
279 } | |
280 } | |
281 | |
282 function send(r) { | |
283 var a, s; | |
284 r.status = 200; | |
285 r.sendHeader(); | |
286 for (a in r.args) { | |
287 if (a.substr(0, 3) == 'foo') { | |
288 s = r.args[a]; | |
289 r.send('n=' + a + ', v=' + s.substr(0, 2) + ' '); | |
290 } | |
291 } | |
292 r.finish(); | |
293 } | |
294 | |
295 function return_method(r) { | |
296 r.return(Number(r.args.c), r.args.t); | |
297 } | |
298 | |
299 function return_headers(r) { | |
300 r.headersOut.Foo = 'bar'; | |
301 r.return(200); | |
302 } | |
303 | |
304 function test_log(r) { | |
305 r.log("SEE-THIS"); | |
306 } | |
307 | |
308 function test_except(r) { | |
309 var fs = require('fs'); | |
310 fs.readFileSync(); | |
311 } | |
312 | |
313 | |
314 function content_except(r) { | |
315 JSON.parse({}.a.a); | |
316 } | |
317 | |
318 function content_empty(r) { | |
319 } | |
320 | |
321 EOF | |
322 | |
323 $t->try_run('no njs available')->plan(32); | |
324 | |
325 ############################################################################### | |
326 | |
327 TODO: { | |
328 local $TODO = 'not yet' | |
329 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.2.2'; | |
330 | |
331 like(http_get('/method'), qr/method=GET/, 'r.method'); | |
332 like(http_get('/version'), qr/version=1.0/, 'r.httpVersion'); | |
333 like(http_get('/addr'), qr/addr=127.0.0.1/, 'r.remoteAddress'); | |
334 like(http_get('/uri'), qr/uri=\/uri/, 'r.uri'); | |
335 like(http_get_hdr('/hdr_in'), qr/hdr=12345/, 'r.headersIn'); | |
336 like(http_get_ihdr('/ihdr_in'), qr/12345barz/, 'r.headersIn iteration'); | |
337 like(http_get('/arg?foO=12345'), qr/arg=12345/, 'r.args'); | |
338 like(http_get('/iarg?foo=12345&foo2=bar&nn=22&foo-3=z'), qr/12345barz/, | |
339 'r.args iteration'); | |
340 like(http_get('/status'), qr/204 No Content/, 'r.status'); | |
341 like(http_get('/ctype'), qr/Content-Type: application\/foo/, | |
342 'r.headersOut.contentType'); | |
343 like(http_get('/clen'), qr/Content-Length: 5/, 'r.headersOut.contentLength'); | |
344 like(http_get('/hdr_out?foo=12345'), qr/Foo: 12345/, 'r.headersOut'); | |
345 like(http_get('/hdr_out?foo=123&bar=copy'), qr/Bar: 123/, 'r.headersOut get'); | |
346 like(http_get('/hdr_out?bar=empty'), qr/Bar: \x0d/, 'r.headersOut empty'); | |
347 like(http_get('/ihdr_out?a=12&b=34'), qr/^1234$/m, 'r.headersOut iteration'); | |
348 like(http_get('/ihdr_out'), qr/\x0d\x0a?\x0d\x0a?$/m, 'r.send zero'); | |
349 | |
350 like(http_post('/body'), qr/REQ-BODY/, 'request body'); | |
351 like(http_post('/in_file'), qr/request body is in a file/, | |
352 'request body in file'); | |
353 like(http_post_big('/body'), qr/200.*^(1234567890){1024}$/ms, | |
354 'request body big'); | |
355 | |
356 like(http_get('/send?foo=12345&n=11&foo-2=bar&ndd=&foo-3=z'), | |
357 qr/n=foo, v=12 n=foo-2, v=ba n=foo-3, v=z/, 'r.send'); | |
358 | |
359 like(http_get('/return?c=200'), qr/200 OK.*\x0d\x0a?\x0d\x0a?$/s, 'return code'); | |
360 like(http_get('/return?c=200&t=SEE-THIS'), qr/200 OK.*^SEE-THIS$/ms, 'return text'); | |
361 like(http_get('/return?c=301&t=path'), qr/ 301 .*Location: path/s, 'return redirect'); | |
362 like(http_get('/return?c=404'), qr/404 Not.*html/s, 'return error page'); | |
363 like(http_get('/return?c=inv'), qr/ 500 /, 'return invalid'); | |
364 | |
365 like(http_get('/return_headers'), qr/Foo: bar/, 'return headers'); | |
366 | |
367 like(http_get('/var'), qr/variable=127.0.0.1/, 'r.variables'); | |
368 like(http_get('/log'), qr/200 OK/, 'r.log'); | |
369 | |
370 http_get('/var_except'); | |
371 http_get('/content_except'); | |
372 | |
373 like(http_get('/content_empty'), qr/500 Internal Server Error/, 'empty handler'); | |
374 } | |
375 | |
376 $t->stop(); | |
377 | |
378 ok(index($t->read_file('error.log'), 'SEE-THIS') > 0, 'log js'); | |
379 ok(index($t->read_file('error.log'), 'at fs.readFileSync') > 0, | |
380 'js_set backtrace'); | |
381 ok(index($t->read_file('error.log'), 'at JSON.parse') > 0, | |
382 'js_content backtrace'); | |
383 | |
384 ############################################################################### | |
385 | |
386 sub http_get_hdr { | |
387 my ($url, %extra) = @_; | |
388 return http(<<EOF, %extra); | |
389 GET $url HTTP/1.0 | |
390 FoO: 12345 | |
391 | |
392 EOF | |
393 } | |
394 | |
395 sub http_get_ihdr { | |
396 my ($url, %extra) = @_; | |
397 return http(<<EOF, %extra); | |
398 GET $url HTTP/1.0 | |
399 foo: 12345 | |
400 Host: localhost | |
401 foo2: bar | |
402 X-xxx: more | |
403 foo-3: z | |
404 | |
405 EOF | |
406 } | |
407 | |
408 sub http_post { | |
409 my ($url, %extra) = @_; | |
410 | |
411 my $p = "POST $url HTTP/1.0" . CRLF . | |
412 "Host: localhost" . CRLF . | |
413 "Content-Length: 8" . CRLF . | |
414 CRLF . | |
415 "REQ-BODY"; | |
416 | |
417 return http($p, %extra); | |
418 } | |
419 | |
420 sub http_post_big { | |
421 my ($url, %extra) = @_; | |
422 | |
423 my $p = "POST $url HTTP/1.0" . CRLF . | |
424 "Host: localhost" . CRLF . | |
425 "Content-Length: 10240" . CRLF . | |
426 CRLF . | |
427 ("1234567890" x 1024); | |
428 | |
429 return http($p, %extra); | |
430 } | |
431 | |
432 ############################################################################### |