Mercurial > hg > nginx-tests
comparison js_buffer.t @ 1625:a140cab489e8
Tests: added js buffer tests.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Wed, 25 Nov 2020 12:25:24 +0000 |
parents | |
children | a35445ae8de7 |
comparison
equal
deleted
inserted
replaced
1624:81fd6615358e | 1625:a140cab489e8 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Dmitry Volyntsev | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for http njs module, buffer properties. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
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 eval { require JSON::PP; }; | |
28 plan(skip_all => "JSON::PP not installed") if $@; | |
29 | |
30 my $t = Test::Nginx->new()->has(qw/http rewrite proxy/) | |
31 ->write_file_expand('nginx.conf', <<'EOF'); | |
32 | |
33 %%TEST_GLOBALS%% | |
34 | |
35 daemon off; | |
36 | |
37 events { | |
38 } | |
39 | |
40 http { | |
41 %%TEST_GLOBALS_HTTP%% | |
42 | |
43 js_import test.js; | |
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 /return { | |
54 js_content test.return; | |
55 } | |
56 | |
57 location /req_body { | |
58 js_content test.req_body; | |
59 } | |
60 | |
61 location /res_body { | |
62 js_content test.res_body; | |
63 } | |
64 | |
65 location /binary_var { | |
66 js_content test.binary_var; | |
67 } | |
68 | |
69 location /p/ { | |
70 proxy_pass http://127.0.0.1:8081/; | |
71 } | |
72 } | |
73 | |
74 server { | |
75 listen 127.0.0.1:8081; | |
76 server_name localhost; | |
77 | |
78 location /sub1 { | |
79 return 200 '{"a": {"b": 1}}'; | |
80 } | |
81 } | |
82 } | |
83 | |
84 EOF | |
85 | |
86 $t->write_file('test.js', <<EOF); | |
87 function test_njs(r) { | |
88 r.return(200, njs.version); | |
89 } | |
90 | |
91 function test_return(r) { | |
92 var body = Buffer.from("body: "); | |
93 body = Buffer.concat([body, Buffer.from(r.args.text)]); | |
94 r.return(200, body); | |
95 } | |
96 | |
97 function req_body(r) { | |
98 var body = r.reqBody; | |
99 var view = new DataView(body.buffer); | |
100 view.setInt8(2, 'c'.charCodeAt(0)); | |
101 r.return(200, JSON.parse(body).c.b); | |
102 } | |
103 | |
104 function res_body(r) { | |
105 r.subrequest('/p/sub1') | |
106 .then(reply => { | |
107 var body = reply.resBody; | |
108 var view = new DataView(body.buffer); | |
109 view.setInt8(2, 'c'.charCodeAt(0)); | |
110 r.return(200, JSON.stringify(JSON.parse(body))); | |
111 }) | |
112 } | |
113 | |
114 function binary_var(r) { | |
115 var test = r.vars.binary_remote_addr.equals(Buffer.from([127,0,0,1])); | |
116 r.return(200, test); | |
117 } | |
118 | |
119 export default {njs: test_njs, return: test_return, req_body, res_body, | |
120 binary_var}; | |
121 | |
122 EOF | |
123 | |
124 $t->try_run('no njs buffer')->plan(4); | |
125 | |
126 ############################################################################### | |
127 | |
128 TODO: { | |
129 local $TODO = 'not yet' | |
130 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0'; | |
131 | |
132 like(http_get('/return?text=FOO'), qr/200 OK.*body: FOO$/s, | |
133 'return buffer'); | |
134 like(http_post('/req_body'), qr/200 OK.*BAR$/s, 'req body'); | |
135 is(get_json('/res_body'), '{"c":{"b":1}}', 'res body'); | |
136 like(http_get('/binary_var'), qr/200 OK.*true$/s, | |
137 'binary var'); | |
138 | |
139 } | |
140 | |
141 ############################################################################### | |
142 | |
143 sub recode { | |
144 my $json; | |
145 eval { $json = JSON::PP::decode_json(shift) }; | |
146 | |
147 if ($@) { | |
148 return "<failed to parse JSON>"; | |
149 } | |
150 | |
151 JSON::PP->new()->canonical()->encode($json); | |
152 } | |
153 | |
154 sub get_json { | |
155 http_get(shift) =~ /\x0d\x0a?\x0d\x0a?(.*)/ms; | |
156 recode($1); | |
157 } | |
158 | |
159 sub http_post { | |
160 my ($url, %extra) = @_; | |
161 | |
162 my $p = "POST $url HTTP/1.0" . CRLF . | |
163 "Host: localhost" . CRLF . | |
164 "Content-Length: 17" . CRLF . | |
165 CRLF . | |
166 "{\"a\":{\"b\":\"BAR\"}}"; | |
167 | |
168 return http($p, %extra); | |
169 } | |
170 | |
171 ############################################################################### |