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