Skip to content

Commit 9a9ef8e

Browse files
committed
Tests: added extended request arguments tests.
1 parent 8011211 commit 9a9ef8e

File tree

2 files changed

+154
-45
lines changed

2 files changed

+154
-45
lines changed

js.t

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ http {
4141
js_set $test_version test.version;
4242
js_set $test_addr test.addr;
4343
js_set $test_uri test.uri;
44-
js_set $test_arg test.arg;
45-
js_set $test_iarg test.iarg;
4644
js_set $test_var test.variable;
4745
js_set $test_type test.type;
4846
js_set $test_global test.global_obj;
@@ -75,14 +73,6 @@ http {
7573
return 200 $test_uri;
7674
}
7775
78-
location /arg {
79-
return 200 $test_arg;
80-
}
81-
82-
location /iarg {
83-
return 200 $test_iarg;
84-
}
85-
8676
location /var {
8777
return 200 $test_var;
8878
}
@@ -120,10 +110,6 @@ http {
120110
js_content test.return_method;
121111
}
122112
123-
location /arg_keys {
124-
js_content test.arg_keys;
125-
}
126-
127113
location /type {
128114
js_content test.type;
129115
}
@@ -171,20 +157,6 @@ $t->write_file('test.js', <<EOF);
171157
return 'uri=' + r.uri;
172158
}
173159
174-
function arg(r) {
175-
return 'arg=' + r.args.foo;
176-
}
177-
178-
function iarg(r) {
179-
var s = '', a;
180-
for (a in r.args) {
181-
if (a.substr(0, 3) == 'foo') {
182-
s += r.args[a];
183-
}
184-
}
185-
return s;
186-
}
187-
188160
function variable(r) {
189161
return 'variable=' + r.variables.remote_addr;
190162
}
@@ -232,10 +204,6 @@ $t->write_file('test.js', <<EOF);
232204
r.return(Number(r.args.c), r.args.t);
233205
}
234206
235-
function arg_keys(r) {
236-
r.return(200, Object.keys(r.args).sort());
237-
}
238-
239207
function type(r) {
240208
var p = r.args.path.split('.').reduce((a, v) => a[v], r);
241209
@@ -260,29 +228,21 @@ $t->write_file('test.js', <<EOF);
260228
function content_empty(r) {
261229
}
262230
263-
export default {njs:test_njs, method, version, addr, uri, arg, iarg,
231+
export default {njs:test_njs, method, version, addr, uri,
264232
variable, global_obj, status, request_body,
265-
request_body_cache, send, return_method, arg_keys,
233+
request_body_cache, send, return_method,
266234
type, log, except, content_except, content_empty};
267235
268236
EOF
269237

270-
$t->try_run('no njs available')->plan(33);
238+
$t->try_run('no njs available')->plan(27);
271239

272240
###############################################################################
273241

274242
like(http_get('/method'), qr/method=GET/, 'r.method');
275243
like(http_get('/version'), qr/version=1.0/, 'r.httpVersion');
276244
like(http_get('/addr'), qr/addr=127.0.0.1/, 'r.remoteAddress');
277245
like(http_get('/uri'), qr/uri=\/uri/, 'r.uri');
278-
like(http_get('/arg?foo=12345'), qr/arg=12345/, 'r.args');
279-
like(http_get('/iarg?foo=12345&foo2=bar&nn=22&foo-3=z'), qr/12345barz/,
280-
'r.args iteration');
281-
282-
like(http_get('/iarg?foo=123&foo2=&foo3&foo4=456'), qr/123undefined456/,
283-
'r.args iteration 2');
284-
like(http_get('/iarg?foo=123&foo2=&foo3'), qr/123/, 'r.args iteration 3');
285-
like(http_get('/iarg?foo=123&foo2='), qr/123/, 'r.args iteration 4');
286246

287247
like(http_get('/status'), qr/204 No Content/, 'r.status');
288248

@@ -304,8 +264,6 @@ like(http_get('/return_method?c=301&t=path'), qr/ 301 .*Location: path/s,
304264
like(http_get('/return_method?c=404'), qr/404 Not.*html/s, 'return error page');
305265
like(http_get('/return_method?c=inv'), qr/ 500 /, 'return invalid');
306266

307-
like(http_get('/arg_keys?b=1&c=2&a=5'), qr/a,b,c/m, 'r.args sorted keys');
308-
309267
TODO: {
310268
local $TODO = 'not yet'
311269
unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0';

js_args.t

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Dmitry Volyntsev
4+
# (C) Nginx, Inc.
5+
6+
# Tests for http njs module, arguments tests.
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+
eval { require JSON::PP; };
26+
plan(skip_all => "JSON::PP not installed") if $@;
27+
28+
my $t = Test::Nginx->new()->has(qw/http/)
29+
->write_file_expand('nginx.conf', <<'EOF');
30+
31+
%%TEST_GLOBALS%%
32+
33+
daemon off;
34+
35+
events {
36+
}
37+
38+
http {
39+
%%TEST_GLOBALS_HTTP%%
40+
41+
js_import test.js;
42+
43+
js_set $test_iter test.iter;
44+
45+
server {
46+
listen 127.0.0.1:8080;
47+
server_name localhost;
48+
49+
location /njs {
50+
js_content test.njs;
51+
}
52+
53+
location /iter {
54+
return 200 $test_iter;
55+
}
56+
57+
location /keys {
58+
js_content test.keys;
59+
}
60+
61+
location /object {
62+
js_content test.object;
63+
}
64+
}
65+
}
66+
67+
EOF
68+
69+
$t->write_file('test.js', <<EOF);
70+
function test_njs(r) {
71+
r.return(200, njs.version);
72+
}
73+
74+
function iter(r) {
75+
var s = '', a;
76+
for (a in r.args) {
77+
if (a.substr(0, 3) == 'foo') {
78+
s += r.args[a];
79+
}
80+
}
81+
82+
return s;
83+
}
84+
85+
function keys(r) {
86+
r.return(200, Object.keys(r.args).sort());
87+
}
88+
89+
function object(r) {
90+
r.return(200, JSON.stringify(r.args));
91+
}
92+
93+
export default {njs: test_njs, iter, keys, object};
94+
95+
EOF
96+
97+
$t->try_run('no njs')->plan(15);
98+
99+
###############################################################################
100+
101+
sub recode {
102+
my $json;
103+
eval { $json = JSON::PP::decode_json(shift) };
104+
105+
if ($@) {
106+
return "<failed to parse JSON>";
107+
}
108+
109+
JSON::PP->new()->canonical()->encode($json);
110+
}
111+
112+
sub get_json {
113+
http_get(shift) =~ /\x0d\x0a?\x0d\x0a?(.*)/ms;
114+
recode($1);
115+
}
116+
117+
###############################################################################
118+
119+
TODO: {
120+
local $TODO = 'not yet'
121+
unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.7.6';
122+
123+
like(http_get('/iter?foo=12345&foo2=bar&nn=22&foo-3=z'), qr/12345barz/,
124+
'r.args iteration');
125+
like(http_get('/iter?foo=123&foo2=&foo3&foo4=456'), qr/123456/,
126+
'r.args iteration 2');
127+
like(http_get('/iter?foo=123&foo2=&foo3'), qr/123/, 'r.args iteration 3');
128+
like(http_get('/iter?foo=123&foo2='), qr/123/, 'r.args iteration 4');
129+
like(http_get('/iter?foo=1&foo=2'), qr/1,2/m, 'r.args iteration 5');
130+
131+
like(http_get('/keys?b=1&c=2&a=5'), qr/a,b,c/m, 'r.args sorted keys');
132+
like(http_get('/keys?b=1&b=2'), qr/b/m, 'r.args duplicate keys');
133+
like(http_get('/keys?b=1&a&c='), qr/a,b,c/m, 'r.args empty value');
134+
135+
is(get_json('/object'), '{}', 'empty object');
136+
is(get_json('/object?a=1&b=2&c=3'), '{"a":"1","b":"2","c":"3"}',
137+
'ordinary object');
138+
is(get_json('/object?a=1&A=2'), '{"A":"2","a":"1"}',
139+
'case sensitive object');
140+
is(get_json('/object?a=1&A=2&a=3'), '{"A":"2","a":["1","3"]}',
141+
'duplicate keys object');
142+
is(get_json('/object?%61=1&a=2'), '{"a":["1","2"]}',
143+
'keys percent-encoded object');
144+
is(get_json('/object?a=%62%63&b=%63%64'), '{"a":"bc","b":"cd"}',
145+
'values percent-encoded object');
146+
is(get_json('/object?a=%6&b=%&c=%%&d=%zz'),
147+
'{"a":"%6","b":"%","c":"%%","d":"%zz"}',
148+
'values percent-encoded broken object');
149+
}
150+
151+
###############################################################################

0 commit comments

Comments
 (0)