Mercurial > hg > nginx-tests
comparison js_promise.t @ 1537:beb549bce15f
Tests: added njs Promise tests.
author | Alexander Borisov <alexander.borisov@nginx.com> |
---|---|
date | Thu, 26 Dec 2019 15:35:42 +0300 |
parents | |
children | 5e2b8621aa5d |
comparison
equal
deleted
inserted
replaced
1536:0ce1c9516764 | 1537:beb549bce15f |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Nginx, Inc. | |
4 | |
5 # Promise tests for http njs module. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 | |
14 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
15 | |
16 use lib 'lib'; | |
17 use Test::Nginx; | |
18 | |
19 ############################################################################### | |
20 | |
21 select STDERR; $| = 1; | |
22 select STDOUT; $| = 1; | |
23 | |
24 my $t = Test::Nginx->new()->has(qw/http/) | |
25 ->write_file_expand('nginx.conf', <<'EOF'); | |
26 | |
27 %%TEST_GLOBALS%% | |
28 | |
29 daemon off; | |
30 | |
31 events { | |
32 } | |
33 | |
34 http { | |
35 %%TEST_GLOBALS_HTTP%% | |
36 | |
37 js_include test.js; | |
38 | |
39 server { | |
40 listen 127.0.0.1:8080; | |
41 server_name localhost; | |
42 | |
43 location /promise { | |
44 js_content promise; | |
45 } | |
46 | |
47 location /promise_throw { | |
48 js_content promise_throw; | |
49 } | |
50 | |
51 location /timeout { | |
52 js_content timeout; | |
53 } | |
54 | |
55 location /sub_token { | |
56 js_content sub_token; | |
57 } | |
58 } | |
59 } | |
60 | |
61 EOF | |
62 | |
63 $t->write_file('test.js', <<EOF); | |
64 var global_token = ''; | |
65 | |
66 function promise(r) { | |
67 promisified_subrequest(r, '/sub_token', 'code=200&token=a') | |
68 .then(reply => { | |
69 var data = JSON.parse(reply.responseBody); | |
70 | |
71 if (data['token'] !== "a") { | |
72 throw new Error('token is not "a"'); | |
73 } | |
74 | |
75 return data['token']; | |
76 }) | |
77 .then(token => { | |
78 promisified_subrequest(r, '/sub_token', 'code=200&token=b') | |
79 .then(reply => { | |
80 var data = JSON.parse(reply.responseBody); | |
81 | |
82 r.return(200, '{"token": "' + data['token'] + '"}'); | |
83 }) | |
84 .catch(() => { | |
85 throw new Error("failed promise() test"); | |
86 }); | |
87 }) | |
88 .catch(() => { | |
89 r.return(500); | |
90 }); | |
91 } | |
92 | |
93 function promise_throw(r) { | |
94 promisified_subrequest(r, '/sub_token', 'code=200&token=x') | |
95 .then(reply => { | |
96 var data = JSON.parse(reply.responseBody); | |
97 | |
98 if (data['token'] !== "a") { | |
99 throw data['token']; | |
100 } | |
101 | |
102 return data['token']; | |
103 }) | |
104 .then(() => { | |
105 r.return(500); | |
106 }) | |
107 .catch(token => { | |
108 r.return(200, '{"token": "' + token + '"}'); | |
109 }); | |
110 } | |
111 | |
112 function timeout(r) { | |
113 promisified_subrequest(r, '/sub_token', 'code=200&token=R') | |
114 .then(reply => JSON.parse(reply.responseBody)) | |
115 .then(data => { | |
116 setTimeout(timeout_cb, 50, r, '/sub_token', 'code=200&token=T'); | |
117 return data; | |
118 }) | |
119 .then(data => { | |
120 setTimeout(timeout_cb, 1, r, '/sub_token', 'code=200&token=' | |
121 + data['token']); | |
122 }) | |
123 .catch(() => { | |
124 r.return(500); | |
125 }); | |
126 } | |
127 | |
128 function timeout_cb(r, url, args) { | |
129 promisified_subrequest(r, url, args) | |
130 .then(reply => { | |
131 if (global_token == '') { | |
132 var data = JSON.parse(reply.responseBody); | |
133 | |
134 global_token = data['token']; | |
135 | |
136 r.return(200, '{"token": "' + data['token'] + '"}'); | |
137 } | |
138 }) | |
139 .catch(() => { | |
140 r.return(500); | |
141 }); | |
142 } | |
143 | |
144 function promisified_subrequest(r, uri, args) { | |
145 return new Promise((resolve, reject) => { | |
146 r.subrequest(uri, args, (reply) => { | |
147 if (reply.status < 400) { | |
148 resolve(reply); | |
149 } else { | |
150 reject(reply); | |
151 } | |
152 }); | |
153 }) | |
154 } | |
155 | |
156 function sub_token(r) { | |
157 var code = r.variables.arg_code; | |
158 var token = r.variables.arg_token; | |
159 | |
160 r.return(parseInt(code), '{"token": "'+ token +'"}'); | |
161 } | |
162 | |
163 EOF | |
164 | |
165 $t->try_run('no njs available')->plan(3); | |
166 | |
167 ############################################################################### | |
168 | |
169 like(http_get('/promise'), qr/{"token": "b"}/, "Promise"); | |
170 like(http_get('/promise_throw'), qr/{"token": "x"}/, "Promise throw and catch"); | |
171 like(http_get('/timeout'), qr/{"token": "R"}/, "Promise with timeout"); | |
172 | |
173 ############################################################################### |