diff js.t @ 1625:a140cab489e8

Tests: added js buffer tests.
author Dmitry Volyntsev <xeioex@nginx.com>
date Wed, 25 Nov 2020 12:25:24 +0000
parents bc0990ea2e5b
children a35445ae8de7
line wrap: on
line diff
--- a/js.t
+++ b/js.t
@@ -44,6 +44,7 @@ http {
     js_set $test_arg      test_arg;
     js_set $test_iarg     test_iarg;
     js_set $test_var      test_var;
+    js_set $test_type     test_type;
     js_set $test_global   test_global;
     js_set $test_log      test_log;
     js_set $test_except   test_except;
@@ -107,6 +108,10 @@ http {
             js_content request_body;
         }
 
+        location /request_body_cache {
+            js_content request_body_cache;
+        }
+
         location /send {
             js_content send;
         }
@@ -119,6 +124,10 @@ http {
             js_content arg_keys;
         }
 
+        location /type {
+            js_content test_type;
+        }
+
         location /log {
             return 200 $test_log;
         }
@@ -200,6 +209,12 @@ EOF
         }
     }
 
+    function request_body_cache(r) {
+        function t(v) {return Buffer.isBuffer(v) ? 'buffer' : (typeof v);}
+        r.return(200,
+                 `requestBody:\${t(r.requestBody)} reqBody:\${t(r.reqBody)}`);
+    }
+
     function send(r) {
         var a, s;
         r.status = 200;
@@ -221,6 +236,13 @@ EOF
         r.return(200, Object.keys(r.args).sort());
     }
 
+    function test_type(r) {
+        var p = r.args.path.split('.').reduce((a, v) => a[v], r);
+
+        var type = Buffer.isBuffer(p) ? 'buffer' : (typeof p);
+        r.return(200, `type: \${type}`);
+    }
+
     function test_log(r) {
         r.log('SEE-LOG');
     }
@@ -240,7 +262,7 @@ EOF
 
 EOF
 
-$t->try_run('no njs available')->plan(27);
+$t->try_run('no njs available')->plan(32);
 
 ###############################################################################
 
@@ -279,6 +301,24 @@ like(http_get('/return_method?c=inv'), q
 
 like(http_get('/arg_keys?b=1&c=2&a=5'), qr/a,b,c/m, 'r.args sorted keys');
 
+TODO: {
+local $TODO = 'not yet'
+	unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0';
+
+like(http_get('/type?path=variables.host'), qr/200 OK.*type: string$/s,
+	'variables type');
+like(http_get('/type?path=vars.host'), qr/200 OK.*type: buffer$/s,
+	'vars type');
+
+like(http_post('/type?path=requestBody'), qr/200 OK.*type: string$/s,
+	'requestBody type');
+like(http_post('/type?path=reqBody'), qr/200 OK.*type: buffer$/s,
+	'reqBody type');
+like(http_post('/request_body_cache'), qr/requestBody:string reqBody:buffer$/s,
+	'reqBody type');
+
+}
+
 like(http_get('/var'), qr/variable=127.0.0.1/, 'r.variables');
 like(http_get('/global'), qr/global=njs/, 'global code');
 like(http_get('/log'), qr/200 OK/, 'r.log');