# HG changeset patch # User Dmitry Volyntsev # Date 1606307124 0 # Node ID a140cab489e82b15acf212a9419bff5b0b5d284f # Parent 81fd6615358eb5519b48b4868ee31249b100e69b Tests: added js buffer tests. diff --git a/js.t b/js.t --- 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'); diff --git a/js_buffer.t b/js_buffer.t new file mode 100644 --- /dev/null +++ b/js_buffer.t @@ -0,0 +1,171 @@ +#!/usr/bin/perl + +# (C) Dmitry Volyntsev +# (C) Nginx, Inc. + +# Tests for http njs module, buffer properties. + +############################################################################### + +use warnings; +use strict; + +use Test::More; + +use Socket qw/ CRLF /; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +eval { require JSON::PP; }; +plan(skip_all => "JSON::PP not installed") if $@; + +my $t = Test::Nginx->new()->has(qw/http rewrite proxy/) + ->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + js_import test.js; + + server { + listen 127.0.0.1:8080; + server_name localhost; + + location /njs { + js_content test.njs; + } + + location /return { + js_content test.return; + } + + location /req_body { + js_content test.req_body; + } + + location /res_body { + js_content test.res_body; + } + + location /binary_var { + js_content test.binary_var; + } + + location /p/ { + proxy_pass http://127.0.0.1:8081/; + } + } + + server { + listen 127.0.0.1:8081; + server_name localhost; + + location /sub1 { + return 200 '{"a": {"b": 1}}'; + } + } +} + +EOF + +$t->write_file('test.js', < { + var body = reply.resBody; + var view = new DataView(body.buffer); + view.setInt8(2, 'c'.charCodeAt(0)); + r.return(200, JSON.stringify(JSON.parse(body))); + }) + } + + function binary_var(r) { + var test = r.vars.binary_remote_addr.equals(Buffer.from([127,0,0,1])); + r.return(200, test); + } + + export default {njs: test_njs, return: test_return, req_body, res_body, + binary_var}; + +EOF + +$t->try_run('no njs buffer')->plan(4); + +############################################################################### + +TODO: { +local $TODO = 'not yet' + unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0'; + +like(http_get('/return?text=FOO'), qr/200 OK.*body: FOO$/s, + 'return buffer'); +like(http_post('/req_body'), qr/200 OK.*BAR$/s, 'req body'); +is(get_json('/res_body'), '{"c":{"b":1}}', 'res body'); +like(http_get('/binary_var'), qr/200 OK.*true$/s, + 'binary var'); + +} + +############################################################################### + +sub recode { + my $json; + eval { $json = JSON::PP::decode_json(shift) }; + + if ($@) { + return ""; + } + + JSON::PP->new()->canonical()->encode($json); +} + +sub get_json { + http_get(shift) =~ /\x0d\x0a?\x0d\x0a?(.*)/ms; + recode($1); +} + +sub http_post { + my ($url, %extra) = @_; + + my $p = "POST $url HTTP/1.0" . CRLF . + "Host: localhost" . CRLF . + "Content-Length: 17" . CRLF . + CRLF . + "{\"a\":{\"b\":\"BAR\"}}"; + + return http($p, %extra); +} + +############################################################################### diff --git a/stream_js_buffer.t b/stream_js_buffer.t new file mode 100644 --- /dev/null +++ b/stream_js_buffer.t @@ -0,0 +1,179 @@ +#!/usr/bin/perl + +# (C) Dmitry Volyntsev +# (C) Nginx, Inc. + +# Tests for stream njs module, buffer properties. + +############################################################################### + +use warnings; +use strict; + +use Test::More; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; +use Test::Nginx::Stream qw/ stream /; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +my $t = Test::Nginx->new()->has(qw/http proxy rewrite stream stream_return/) + ->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + js_import test.js; + + server { + listen 127.0.0.1:8080; + server_name localhost; + + location /njs { + js_content test.njs; + } + + location /p/ { + proxy_pass http://127.0.0.1:8085/; + } + + location /return { + return 200 'RETURN:$http_foo'; + } + } +} + +stream { + js_import test.js; + + js_set $type test.type; + js_set $binary_var test.binary_var; + + server { + listen 127.0.0.1:8081; + return $type; + } + + server { + listen 127.0.0.1:8082; + return $binary_var; + } + + server { + listen 127.0.0.1:8083; + js_preread test.cb_mismatch; + proxy_pass 127.0.0.1:8090; + } + + server { + listen 127.0.0.1:8084; + js_preread test.cb_mismatch2; + proxy_pass 127.0.0.1:8090; + } + + server { + listen 127.0.0.1:8085; + js_filter test.header_inject; + proxy_pass 127.0.0.1:8080; + } +} + +EOF + +$t->write_file('test.js', < {}); + s.on('downstream', () => {}); + } catch (e) { + throw new Error(`cb_mismatch:\${e.message}`) + } + } + + function cb_mismatch2(s) { + try { + s.on('upstream', () => {}); + s.on('download', () => {}); + } catch (e) { + throw new Error(`cb_mismatch2:\${e.message}`) + } + } + + function header_inject(s) { + var req = Buffer.from([]); + + s.on('upstream', function(data, flags) { + req = Buffer.concat([req, data]); + + var n = req.indexOf('\\n'); + if (n != -1) { + var rest = req.slice(n + 1); + req = req.slice(0, n + 1); + + s.send(req, flags); + s.send('Foo: foo\\r\\n', flags); + s.send(rest, flags); + + s.off('upstream'); + } + }); + } + + export default {njs: test_njs, type, binary_var, cb_mismatch, cb_mismatch2, + header_inject}; + +EOF + +$t->try_run('no njs ngx')->plan(5); + +############################################################################### + +TODO: { +local $TODO = 'not yet' + unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0'; + +is(stream('127.0.0.1:' . port(8081))->read(), 'buffer', 'var type'); +is(stream('127.0.0.1:' . port(8082))->read(), 'true', 'binary var'); + +stream('127.0.0.1:' . port(8083))->io('x'); +stream('127.0.0.1:' . port(8084))->io('x'); + +like(http_get('/p/return'), qr/RETURN:foo/, 'injected header'); + +$t->stop(); + +ok(index($t->read_file('error.log'), 'cb_mismatch:mixing string and buffer') + > 0, 'cb mismatch'); +ok(index($t->read_file('error.log'), 'cb_mismatch2:mixing string and buffer') + > 0, 'cb mismatch'); +} + +###############################################################################