Mercurial > hg > nginx-tests
comparison js_promise.t @ 1628:54f867e69cb5
Tests: added js promise test with standalone promises.
author | Alexander Borisov <alexander.borisov@nginx.com> |
---|---|
date | Thu, 26 Nov 2020 18:58:23 +0300 |
parents | bc0990ea2e5b |
children | 18ac4d9e5a2a |
comparison
equal
deleted
inserted
replaced
1627:456bf219d768 | 1628:54f867e69cb5 |
---|---|
38 | 38 |
39 server { | 39 server { |
40 listen 127.0.0.1:8080; | 40 listen 127.0.0.1:8080; |
41 server_name localhost; | 41 server_name localhost; |
42 | 42 |
43 location /njs { | |
44 js_content test_njs; | |
45 } | |
46 | |
43 location /promise { | 47 location /promise { |
44 js_content promise; | 48 js_content promise; |
45 } | 49 } |
46 | 50 |
47 location /promise_throw { | 51 location /promise_throw { |
48 js_content promise_throw; | 52 js_content promise_throw; |
49 } | 53 } |
50 | 54 |
55 location /promise_pure { | |
56 js_content promise_pure; | |
57 } | |
58 | |
51 location /timeout { | 59 location /timeout { |
52 js_content timeout; | 60 js_content timeout; |
53 } | 61 } |
54 | 62 |
55 location /sub_token { | 63 location /sub_token { |
60 | 68 |
61 EOF | 69 EOF |
62 | 70 |
63 $t->write_file('test.js', <<EOF); | 71 $t->write_file('test.js', <<EOF); |
64 var global_token = ''; | 72 var global_token = ''; |
73 | |
74 function test_njs(r) { | |
75 r.return(200, njs.version); | |
76 } | |
65 | 77 |
66 function promise(r) { | 78 function promise(r) { |
67 promisified_subrequest(r, '/sub_token', 'code=200&token=a') | 79 promisified_subrequest(r, '/sub_token', 'code=200&token=a') |
68 .then(reply => { | 80 .then(reply => { |
69 var data = JSON.parse(reply.responseBody); | 81 var data = JSON.parse(reply.responseBody); |
104 .then(() => { | 116 .then(() => { |
105 r.return(500); | 117 r.return(500); |
106 }) | 118 }) |
107 .catch(token => { | 119 .catch(token => { |
108 r.return(200, '{"token": "' + token + '"}'); | 120 r.return(200, '{"token": "' + token + '"}'); |
121 }); | |
122 } | |
123 | |
124 function promise_pure(r) { | |
125 var count = 0; | |
126 | |
127 Promise.resolve(true) | |
128 .then(() => count++) | |
129 .then(() => not_exist_ref) | |
130 .finally(() => count++) | |
131 .catch(() => { | |
132 r.return((count != 2) ? 500 : 200); | |
109 }); | 133 }); |
110 } | 134 } |
111 | 135 |
112 function timeout(r) { | 136 function timeout(r) { |
113 promisified_subrequest(r, '/sub_token', 'code=200&token=R') | 137 promisified_subrequest(r, '/sub_token', 'code=200&token=R') |
160 r.return(parseInt(code), '{"token": "'+ token +'"}'); | 184 r.return(parseInt(code), '{"token": "'+ token +'"}'); |
161 } | 185 } |
162 | 186 |
163 EOF | 187 EOF |
164 | 188 |
165 $t->try_run('no njs available')->plan(3); | 189 $t->try_run('no njs available')->plan(4); |
166 | 190 |
167 ############################################################################### | 191 ############################################################################### |
168 | 192 |
169 like(http_get('/promise'), qr/{"token": "b"}/, "Promise"); | 193 like(http_get('/promise'), qr/{"token": "b"}/, "Promise"); |
170 like(http_get('/promise_throw'), qr/{"token": "x"}/, "Promise throw and catch"); | 194 like(http_get('/promise_throw'), qr/{"token": "x"}/, "Promise throw and catch"); |
171 like(http_get('/timeout'), qr/{"token": "R"}/, "Promise with timeout"); | 195 like(http_get('/timeout'), qr/{"token": "R"}/, "Promise with timeout"); |
172 | 196 |
173 ############################################################################### | 197 TODO: { |
198 local $TODO = 'not yet' | |
199 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0'; | |
200 | |
201 like(http_get('/promise_pure'), qr/200 OK/, "events handling"); | |
202 } | |
203 | |
204 $t->todo_alerts() unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0'; | |
205 | |
206 ############################################################################### |