changeset 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 456bf219d768
children a1874249496d
files js_promise.t
diffstat 1 files changed, 34 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/js_promise.t
+++ b/js_promise.t
@@ -40,6 +40,10 @@ http {
         listen       127.0.0.1:8080;
         server_name  localhost;
 
+        location /njs {
+            js_content test_njs;
+        }
+
         location /promise {
             js_content promise;
         }
@@ -48,6 +52,10 @@ http {
             js_content promise_throw;
         }
 
+        location /promise_pure {
+            js_content promise_pure;
+        }
+
         location /timeout {
             js_content timeout;
         }
@@ -63,6 +71,10 @@ EOF
 $t->write_file('test.js', <<EOF);
     var global_token = '';
 
+    function test_njs(r) {
+        r.return(200, njs.version);
+    }
+
     function promise(r) {
         promisified_subrequest(r, '/sub_token', 'code=200&token=a')
         .then(reply => {
@@ -109,6 +121,18 @@ EOF
         });
     }
 
+    function promise_pure(r) {
+        var count = 0;
+
+        Promise.resolve(true)
+        .then(() => count++)
+        .then(() => not_exist_ref)
+        .finally(() => count++)
+        .catch(() => {
+            r.return((count != 2) ? 500 : 200);
+        });
+    }
+
     function timeout(r) {
         promisified_subrequest(r, '/sub_token', 'code=200&token=R')
         .then(reply => JSON.parse(reply.responseBody))
@@ -162,7 +186,7 @@ EOF
 
 EOF
 
-$t->try_run('no njs available')->plan(3);
+$t->try_run('no njs available')->plan(4);
 
 ###############################################################################
 
@@ -170,4 +194,13 @@ like(http_get('/promise'), qr/{"token": 
 like(http_get('/promise_throw'), qr/{"token": "x"}/, "Promise throw and catch");
 like(http_get('/timeout'), qr/{"token": "R"}/, "Promise with timeout");
 
+TODO: {
+local $TODO = 'not yet'
+	unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0';
+
+like(http_get('/promise_pure'), qr/200 OK/, "events handling");
+}
+
+$t->todo_alerts() unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0';
+
 ###############################################################################