Mercurial > hg > nginx-tests
comparison js_args.t @ 1778:0979f22e3a5b
Tests: added extended request arguments tests.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Thu, 14 Jul 2022 20:05:07 -0700 |
parents | |
children | 520fb74cce4c |
comparison
equal
deleted
inserted
replaced
1777:410072f42157 | 1778:0979f22e3a5b |
---|---|
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 ############################################################################### |