Mercurial > hg > nginx-tests
comparison js2.t @ 1352:32156faf018e
Tests: fine-tuning of js2.t.
author | Andrey Zelenkov <zelenkov@nginx.com> |
---|---|
date | Thu, 28 Jun 2018 18:33:59 +0300 |
parents | 15f0d9412b22 |
children | 215f3357034b |
comparison
equal
deleted
inserted
replaced
1351:81579df2ba8c | 1352:32156faf018e |
---|---|
35 } | 35 } |
36 | 36 |
37 http { | 37 http { |
38 %%TEST_GLOBALS_HTTP%% | 38 %%TEST_GLOBALS_HTTP%% |
39 | 39 |
40 js_set $test_method test_method; | 40 js_set $test_method test_method; |
41 js_set $test_version test_version; | 41 js_set $test_version test_version; |
42 js_set $test_addr test_addr; | 42 js_set $test_addr test_addr; |
43 js_set $test_uri test_uri; | 43 js_set $test_uri test_uri; |
44 js_set $test_hdr test_hdr; | 44 js_set $test_hdr_in test_hdr_in; |
45 js_set $test_ihdr test_ihdr; | 45 js_set $test_ihdr_in test_ihdr_in; |
46 js_set $test_arg test_arg; | 46 js_set $test_arg test_arg; |
47 js_set $test_iarg test_iarg; | 47 js_set $test_iarg test_iarg; |
48 js_set $test_var test_var; | 48 js_set $test_var test_var; |
49 js_set $test_log test_log; | 49 js_set $test_log test_log; |
50 js_set $test_except test_except; | 50 js_set $test_except test_except; |
51 | 51 |
52 js_include test.js; | 52 js_include test.js; |
53 | 53 |
54 server { | 54 server { |
55 listen 127.0.0.1:8080; | 55 listen 127.0.0.1:8080; |
74 location /uri { | 74 location /uri { |
75 return 200 $test_uri; | 75 return 200 $test_uri; |
76 } | 76 } |
77 | 77 |
78 location /hdr_in { | 78 location /hdr_in { |
79 return 200 $test_hdr; | 79 return 200 $test_hdr_in; |
80 } | 80 } |
81 | 81 |
82 location /ihdr_in { | 82 location /ihdr_in { |
83 return 200 $test_ihdr; | 83 return 200 $test_ihdr_in; |
84 } | 84 } |
85 | 85 |
86 location /arg { | 86 location /arg { |
87 return 200 $test_arg; | 87 return 200 $test_arg; |
88 } | 88 } |
130 | 130 |
131 location /send { | 131 location /send { |
132 js_content send; | 132 js_content send; |
133 } | 133 } |
134 | 134 |
135 location /return { | 135 location /return_method { |
136 js_content return_method; | 136 js_content return_method; |
137 } | 137 } |
138 | 138 |
139 location /return_headers { | 139 location /return_headers { |
140 js_content return_headers; | 140 js_content return_headers; |
142 | 142 |
143 location /log { | 143 location /log { |
144 return 200 $test_log; | 144 return 200 $test_log; |
145 } | 145 } |
146 | 146 |
147 location /var_except { | 147 location /except { |
148 return 200 $test_except; | 148 return 200 $test_except; |
149 } | 149 } |
150 | 150 |
151 location /content_except { | 151 location /content_except { |
152 js_content content_except; | 152 js_content content_except; |
179 | 179 |
180 function test_uri(r) { | 180 function test_uri(r) { |
181 return 'uri=' + r.uri; | 181 return 'uri=' + r.uri; |
182 } | 182 } |
183 | 183 |
184 function test_hdr(r) { | 184 function test_hdr_in(r) { |
185 return 'hdr=' + r.headersIn.foo; | 185 return 'hdr=' + r.headersIn.foo; |
186 } | 186 } |
187 | 187 |
188 function test_ihdr(r) { | 188 function test_ihdr_in(r) { |
189 var s = '', h; | 189 var s = '', h; |
190 for (h in r.headersIn) { | 190 for (h in r.headersIn) { |
191 if (h.substr(0, 3) == 'foo') { | 191 if (h.substr(0, 3) == 'foo') { |
192 s += r.headersIn[h]; | 192 s += r.headersIn[h]; |
193 } | 193 } |
213 return 'variable=' + r.variables.remote_addr; | 213 return 'variable=' + r.variables.remote_addr; |
214 } | 214 } |
215 | 215 |
216 function status(r) { | 216 function status(r) { |
217 r.status = 204; | 217 r.status = 204; |
218 if (r.status != 204) | |
219 r.status = 404; | |
220 r.sendHeader(); | 218 r.sendHeader(); |
221 r.finish(); | 219 r.finish(); |
222 } | 220 } |
223 | 221 |
224 function ctype(r) { | 222 function ctype(r) { |
229 } | 227 } |
230 | 228 |
231 function clen(r) { | 229 function clen(r) { |
232 r.status = 200; | 230 r.status = 200; |
233 r.headersOut['Content-Length'] = 5; | 231 r.headersOut['Content-Length'] = 5; |
234 if (r.headersOut['Content-Length'] != 5) | |
235 r.headersOut['Content-Length'] = 6; | |
236 r.sendHeader(); | 232 r.sendHeader(); |
237 r.send('foo12'); | 233 r.send('foo12'); |
238 r.finish(); | 234 r.finish(); |
239 } | 235 } |
240 | 236 |
241 function hdr_out(r) { | 237 function hdr_out(r) { |
242 r.status = 200; | 238 r.status = 200; |
243 r.headersOut['Foo'] = r.args.fOO; | 239 r.headersOut['Foo'] = r.args.fOO; |
244 | 240 |
245 if (r.args.bar) { | 241 if (r.args.bar) { |
246 r.headersOut['Bar'] = r.headersOut['Foo']; | 242 r.headersOut['Bar'] = |
247 } | 243 r.headersOut[(r.args.bar == 'empty' ? 'Baz' :'Foo')] |
248 | |
249 if (r.args.bar == 'empty') { | |
250 r.headersOut['Bar'] = r.headersOut['Baz']; | |
251 } | 244 } |
252 | 245 |
253 r.sendHeader(); | 246 r.sendHeader(); |
254 r.finish(); | 247 r.finish(); |
255 } | 248 } |
300 r.headersOut.Foo = 'bar'; | 293 r.headersOut.Foo = 'bar'; |
301 r.return(200); | 294 r.return(200); |
302 } | 295 } |
303 | 296 |
304 function test_log(r) { | 297 function test_log(r) { |
305 r.log("SEE-THIS"); | 298 r.log('SEE-THIS'); |
306 } | 299 } |
307 | 300 |
308 function test_except(r) { | 301 function test_except(r) { |
309 var fs = require('fs'); | 302 var fs = require('fs'); |
310 fs.readFileSync(); | 303 fs.readFileSync(); |
318 function content_empty(r) { | 311 function content_empty(r) { |
319 } | 312 } |
320 | 313 |
321 EOF | 314 EOF |
322 | 315 |
323 $t->try_run('no njs available')->plan(32); | 316 $t->try_run('no njs available')->plan(34); |
324 | 317 |
325 ############################################################################### | 318 ############################################################################### |
326 | 319 |
327 TODO: { | 320 TODO: { |
328 local $TODO = 'not yet' | 321 local $TODO = 'not yet' |
342 'r.headersOut.contentType'); | 335 'r.headersOut.contentType'); |
343 like(http_get('/clen'), qr/Content-Length: 5/, 'r.headersOut.contentLength'); | 336 like(http_get('/clen'), qr/Content-Length: 5/, 'r.headersOut.contentLength'); |
344 like(http_get('/hdr_out?foo=12345'), qr/Foo: 12345/, 'r.headersOut'); | 337 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'); | 338 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'); | 339 like(http_get('/hdr_out?bar=empty'), qr/Bar: \x0d/, 'r.headersOut empty'); |
340 like(http_get('/hdr_out?foo='), qr/Foo: \x0d/, 'r.headersOut no value'); | |
341 like(http_get('/hdr_out?foo'), qr/Foo: \x0d/, 'r.headersOut no value 2'); | |
347 like(http_get('/ihdr_out?a=12&b=34'), qr/^1234$/m, 'r.headersOut iteration'); | 342 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'); | 343 like(http_get('/ihdr_out'), qr/\x0d\x0a?\x0d\x0a?$/m, 'r.send zero'); |
349 | 344 |
350 like(http_post('/body'), qr/REQ-BODY/, 'request body'); | 345 like(http_post('/body'), qr/REQ-BODY/, 'request body'); |
351 like(http_post('/in_file'), qr/request body is in a file/, | 346 like(http_post('/in_file'), qr/request body is in a file/, |
352 'request body in file'); | 347 'request body in file'); |
353 like(http_post_big('/body'), qr/200.*^(1234567890){1024}$/ms, | 348 like(http_post_big('/body'), qr/200.*^(1234567890){1024}$/ms, |
354 'request body big'); | 349 'request body big'); |
355 | 350 |
356 like(http_get('/send?foo=12345&n=11&foo-2=bar&ndd=&foo-3=z'), | 351 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'); | 352 qr/n=foo, v=12 n=foo-2, v=ba n=foo-3, v=z/, 'r.send'); |
358 | 353 |
359 like(http_get('/return?c=200'), qr/200 OK.*\x0d\x0a?\x0d\x0a?$/s, 'return code'); | 354 like(http_get('/return_method?c=200'), qr/200 OK.*\x0d\x0a?\x0d\x0a?$/s, |
360 like(http_get('/return?c=200&t=SEE-THIS'), qr/200 OK.*^SEE-THIS$/ms, 'return text'); | 355 'return code'); |
361 like(http_get('/return?c=301&t=path'), qr/ 301 .*Location: path/s, 'return redirect'); | 356 like(http_get('/return_method?c=200&t=SEE-THIS'), qr/200 OK.*^SEE-THIS$/ms, |
362 like(http_get('/return?c=404'), qr/404 Not.*html/s, 'return error page'); | 357 'return text'); |
363 like(http_get('/return?c=inv'), qr/ 500 /, 'return invalid'); | 358 like(http_get('/return_method?c=301&t=path'), qr/ 301 .*Location: path/s, |
359 'return redirect'); | |
360 like(http_get('/return_method?c=404'), qr/404 Not.*html/s, 'return error page'); | |
361 like(http_get('/return_method?c=inv'), qr/ 500 /, 'return invalid'); | |
364 | 362 |
365 like(http_get('/return_headers'), qr/Foo: bar/, 'return headers'); | 363 like(http_get('/return_headers'), qr/Foo: bar/, 'return headers'); |
366 | 364 |
367 like(http_get('/var'), qr/variable=127.0.0.1/, 'r.variables'); | 365 like(http_get('/var'), qr/variable=127.0.0.1/, 'r.variables'); |
368 like(http_get('/log'), qr/200 OK/, 'r.log'); | 366 like(http_get('/log'), qr/200 OK/, 'r.log'); |
369 | 367 |
370 http_get('/var_except'); | 368 http_get('/except'); |
371 http_get('/content_except'); | 369 http_get('/content_except'); |
372 | 370 |
373 like(http_get('/content_empty'), qr/500 Internal Server Error/, 'empty handler'); | 371 like(http_get('/content_empty'), qr/500 Internal Server Error/, |
372 'empty handler'); | |
374 } | 373 } |
375 | 374 |
376 $t->stop(); | 375 $t->stop(); |
377 | 376 |
378 ok(index($t->read_file('error.log'), 'SEE-THIS') > 0, 'log js'); | 377 ok(index($t->read_file('error.log'), 'SEE-THIS') > 0, 'log js'); |
379 ok(index($t->read_file('error.log'), 'at fs.readFileSync') > 0, | 378 ok(index($t->read_file('error.log'), 'at fs.readFileSync') > 0, |
380 'js_set backtrace'); | 379 'js_set backtrace'); |
381 ok(index($t->read_file('error.log'), 'at JSON.parse') > 0, | 380 ok(index($t->read_file('error.log'), 'at JSON.parse') > 0, |
382 'js_content backtrace'); | 381 'js_content backtrace'); |
383 | 382 |
384 ############################################################################### | 383 ############################################################################### |
385 | 384 |
386 sub http_get_hdr { | 385 sub http_get_hdr { |
387 my ($url, %extra) = @_; | 386 my ($url, %extra) = @_; |