diff stream_js.t @ 1751:18ac4d9e5a2a

Tests: rewriting njs tests without deprecated "js_include".
author Dmitry Volyntsev <xeioex@nginx.com>
date Thu, 09 Dec 2021 17:36:29 +0000
parents 9e0347f4df11
children 520fb74cce4c
line wrap: on
line diff
--- a/stream_js.t
+++ b/stream_js.t
@@ -37,14 +37,14 @@ events {
 http {
     %%TEST_GLOBALS_HTTP%%
 
-    js_include test.js;
+    js_import test.js;
 
     server {
         listen       127.0.0.1:8079;
         server_name  localhost;
 
         location /njs {
-            js_content test_njs;
+            js_content test.njs;
         }
 
         location /p/ {
@@ -61,15 +61,15 @@ http {
 stream {
     %%TEST_GLOBALS_STREAM%%
 
-    js_set $js_addr      js_addr;
-    js_set $js_var       js_var;
-    js_set $js_log       js_log;
-    js_set $js_unk       js_unk;
-    js_set $js_req_line  js_req_line;
-    js_set $js_sess_unk  js_sess_unk;
-    js_set $js_async     js_async;
+    js_set $js_addr      test.addr;
+    js_set $js_var       test.variable;
+    js_set $js_log       test.log;
+    js_set $js_unk       test.unk;
+    js_set $js_req_line  test.req_line;
+    js_set $js_sess_unk  test.sess_unk;
+    js_set $js_async     test.asyncf;
 
-    js_include test.js;
+    js_import test.js;
 
     log_format status $server_port:$status;
 
@@ -105,84 +105,84 @@ stream {
 
     server {
         listen      127.0.0.1:8086;
-        js_access   js_access_step;
-        js_preread  js_preread_step;
-        js_filter   js_filter_step;
+        js_access   test.access_step;
+        js_preread  test.preread_step;
+        js_filter   test.filter_step;
         proxy_pass  127.0.0.1:8090;
     }
 
     server {
         listen      127.0.0.1:8087;
-        js_access   js_access_undecided;
+        js_access   test.access_undecided;
         return      OK;
         access_log  %%TESTDIR%%/status.log status;
     }
 
     server {
         listen      127.0.0.1:8088;
-        js_access   js_access_allow;
+        js_access   test.access_allow;
         return      OK;
         access_log  %%TESTDIR%%/status.log status;
     }
 
     server {
         listen      127.0.0.1:8089;
-        js_access   js_access_deny;
+        js_access   test.access_deny;
         return      OK;
         access_log  %%TESTDIR%%/status.log status;
     }
 
     server {
         listen      127.0.0.1:8091;
-        js_preread  js_preread_async;
+        js_preread  test.preread_async;
         proxy_pass  127.0.0.1:8090;
     }
 
     server {
         listen      127.0.0.1:8092;
-        js_preread  js_preread_data;
+        js_preread  test.preread_data;
         proxy_pass  127.0.0.1:8090;
     }
 
     server {
         listen      127.0.0.1:8093;
-        js_preread  js_preread_req_line;
+        js_preread  test.preread_req_line;
         return      $js_req_line;
     }
 
     server {
         listen      127.0.0.1:8094;
-        js_filter   js_filter_empty;
+        js_filter   test.filter_empty;
         proxy_pass  127.0.0.1:8090;
     }
 
     server {
         listen      127.0.0.1:8095;
-        js_filter   js_filter_header_inject;
+        js_filter   test.filter_header_inject;
         proxy_pass  127.0.0.1:8079;
     }
 
     server {
         listen      127.0.0.1:8096;
-        js_filter   js_filter_search;
+        js_filter   test.filter_search;
         proxy_pass  127.0.0.1:8090;
     }
 
     server {
         listen      127.0.0.1:8097;
-        js_access   js_access_except;
+        js_access   test.access_except;
         proxy_pass  127.0.0.1:8090;
     }
 
     server {
         listen      127.0.0.1:8098;
-        js_preread  js_preread_except;
+        js_preread  test.preread_except;
         proxy_pass  127.0.0.1:8090;
     }
 
     server {
         listen      127.0.0.1:8099;
-        js_filter   js_filter_except;
+        js_filter   test.filter_except;
         proxy_pass  127.0.0.1:8090;
     }
 
@@ -199,25 +199,25 @@ EOF
         r.return(200, njs.version);
     }
 
-    function js_addr(s) {
+    function addr(s) {
         return 'addr=' + s.remoteAddress;
     }
 
-    function js_var(s) {
+    function variable(s) {
         return 'variable=' + s.variables.remote_addr;
     }
 
-    function js_sess_unk(s) {
+    function sess_unk(s) {
         return 'sess_unk=' + s.unk;
     }
 
-    function js_log(s) {
+    function log(s) {
         s.log("SEE-THIS");
     }
 
     var res = '';
 
-    function js_access_step(s) {
+    function access_step(s) {
         res += '1';
 
         setTimeout(function() {
@@ -227,7 +227,7 @@ EOF
         }, 1);
     }
 
-    function js_preread_step(s) {
+    function preread_step(s) {
         s.on('upload', function (data) {
             res += '2';
             if (res.length >= 3) {
@@ -236,7 +236,7 @@ EOF
         });
     }
 
-    function js_filter_step(s) {
+    function filter_step(s) {
         s.on('upload', function(data, flags) {
             s.send(data);
             res += '3';
@@ -256,11 +256,11 @@ EOF
         });
     }
 
-    function js_access_undecided(s) {
+    function access_undecided(s) {
         s.decline();
     }
 
-    function js_access_allow(s) {
+    function access_allow(s) {
         if (s.remoteAddress.match('127.0.0.1')) {
             s.done();
             return;
@@ -269,7 +269,7 @@ EOF
         s.deny();
     }
 
-    function js_access_deny(s) {
+    function access_deny(s) {
         if (s.remoteAddress.match('127.0.0.1')) {
             s.deny();
             return;
@@ -279,13 +279,13 @@ EOF
     }
 
 
-    function js_preread_async(s) {
+    function preread_async(s) {
         setTimeout(function() {
             s.done();
         }, 1);
     }
 
-    function js_preread_data(s) {
+    function preread_data(s) {
         s.on('upload', function (data, flags) {
             if (data.indexOf('z') != -1) {
                 s.done();
@@ -295,7 +295,7 @@ EOF
 
     var line = '';
 
-    function js_preread_req_line(s) {
+    function preread_req_line(s) {
         s.on('upload', function (data, flags) {
             var n = data.indexOf('\\n');
             if (n != -1) {
@@ -305,14 +305,14 @@ EOF
         });
     }
 
-    function js_req_line(s) {
+    function req_line(s) {
         return line;
     }
 
-    function js_filter_empty(s) {
+    function filter_empty(s) {
     }
 
-    function js_filter_header_inject(s) {
+    function filter_header_inject(s) {
         var req = '';
 
         s.on('upload', function(data, flags) {
@@ -330,7 +330,7 @@ EOF
         });
     }
 
-    function js_filter_search(s) {
+    function filter_search(s) {
         s.on('download', function(data, flags) {
             var n = data.search('y');
             if (n != -1) {
@@ -346,19 +346,19 @@ EOF
         });
     }
 
-    function js_access_except(s) {
+    function access_except(s) {
         function done() {return s.a.a};
 
         setTimeout(done, 1);
         setTimeout(done, 2);
     }
 
-    function js_preread_except(s) {
+    function preread_except(s) {
         var fs = require('fs');
         fs.readFileSync();
     }
 
-    function js_filter_except(s) {
+    function filter_except(s) {
         s.on('unknown', function() {});
     }
 
@@ -366,13 +366,19 @@ EOF
         return new Promise(resolve => {resolve(x)}).then(v => v).then(v => v);
     }
 
-    async function js_async(s) {
+    async function asyncf(s) {
         const a1 = await pr(10);
         const a2 = await pr(20);
 
         s.setReturnValue(`retval: \${a1 + a2}`);
     }
 
+    export default {njs:test_njs, addr, variable, sess_unk, log, access_step,
+                    preread_step, filter_step, access_undecided, access_allow,
+                    access_deny, preread_async, preread_data, preread_req_line,
+                    req_line, filter_empty, filter_header_inject, filter_search,
+                    access_except, preread_except, filter_except, asyncf};
+
 EOF
 
 $t->run_daemon(\&stream_daemon, port(8090));
@@ -393,17 +399,17 @@ is(stream('127.0.0.1:' . port(8084))->re
 
 is(stream('127.0.0.1:' . port(8086))->io('0'), '0122345',
 	'async handlers order');
-is(stream('127.0.0.1:' . port(8087))->io('#'), 'OK', 'js_access_undecided');
-is(stream('127.0.0.1:' . port(8088))->io('#'), 'OK', 'js_access_allow');
-is(stream('127.0.0.1:' . port(8089))->io('#'), '', 'js_access_deny');
+is(stream('127.0.0.1:' . port(8087))->io('#'), 'OK', 'access_undecided');
+is(stream('127.0.0.1:' . port(8088))->io('#'), 'OK', 'access_allow');
+is(stream('127.0.0.1:' . port(8089))->io('#'), '', 'access_deny');
 
-is(stream('127.0.0.1:' . port(8091))->io('#'), '#', 'js_preread_async');
-is(stream('127.0.0.1:' . port(8092))->io('#z'), '#z', 'js_preread_async_data');
-is(stream('127.0.0.1:' . port(8093))->io("xy\na"), 'xy', 'js_preread_req_line');
+is(stream('127.0.0.1:' . port(8091))->io('#'), '#', 'preread_async');
+is(stream('127.0.0.1:' . port(8092))->io('#z'), '#z', 'preread_async_data');
+is(stream('127.0.0.1:' . port(8093))->io("xy\na"), 'xy', 'preread_req_line');
 
-is(stream('127.0.0.1:' . port(8094))->io('x'), 'x', 'js_filter_empty');
-like(get('/p/return'), qr/foo/, 'js_filter_injected_header');
-is(stream('127.0.0.1:' . port(8096))->io('x'), 'z', 'js_filter_search');
+is(stream('127.0.0.1:' . port(8094))->io('x'), 'x', 'filter_empty');
+like(get('/p/return'), qr/foo/, 'filter_injected_header');
+is(stream('127.0.0.1:' . port(8096))->io('x'), 'z', 'filter_search');
 
 stream('127.0.0.1:' . port(8097))->io('x');
 stream('127.0.0.1:' . port(8098))->io('x');
@@ -413,7 +419,7 @@ TODO: {
 local $TODO = 'not yet'
 	unless get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.7.0';
 
-is(stream('127.0.0.1:' . port(8100))->read(), 'retval: 30', 'js_async');
+is(stream('127.0.0.1:' . port(8100))->read(), 'retval: 30', 'asyncf');
 
 }
 
@@ -422,7 +428,7 @@ is(stream('127.0.0.1:' . port(8100))->re
 ok(index($t->read_file('error.log'), 'SEE-THIS') > 0, 'stream js log');
 ok(index($t->read_file('error.log'), 'at fs.readFileSync') > 0,
 	'stream js_preread backtrace');
-ok(index($t->read_file('error.log'), 'at js_filter_except') > 0,
+ok(index($t->read_file('error.log'), 'at filter_except') > 0,
 	'stream js_filter backtrace');
 
 my @p = (port(8087), port(8088), port(8089));