comparison stream_js_buffer.t @ 1625:a140cab489e8

Tests: added js buffer tests.
author Dmitry Volyntsev <xeioex@nginx.com>
date Wed, 25 Nov 2020 12:25:24 +0000
parents
children a35445ae8de7
comparison
equal deleted inserted replaced
1624:81fd6615358e 1625:a140cab489e8
1 #!/usr/bin/perl
2
3 # (C) Dmitry Volyntsev
4 # (C) Nginx, Inc.
5
6 # Tests for stream njs module, buffer properties.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19 use Test::Nginx::Stream qw/ stream /;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 my $t = Test::Nginx->new()->has(qw/http proxy rewrite stream stream_return/)
27 ->write_file_expand('nginx.conf', <<'EOF');
28
29 %%TEST_GLOBALS%%
30
31 daemon off;
32
33 events {
34 }
35
36 http {
37 %%TEST_GLOBALS_HTTP%%
38
39 js_import test.js;
40
41 server {
42 listen 127.0.0.1:8080;
43 server_name localhost;
44
45 location /njs {
46 js_content test.njs;
47 }
48
49 location /p/ {
50 proxy_pass http://127.0.0.1:8085/;
51 }
52
53 location /return {
54 return 200 'RETURN:$http_foo';
55 }
56 }
57 }
58
59 stream {
60 js_import test.js;
61
62 js_set $type test.type;
63 js_set $binary_var test.binary_var;
64
65 server {
66 listen 127.0.0.1:8081;
67 return $type;
68 }
69
70 server {
71 listen 127.0.0.1:8082;
72 return $binary_var;
73 }
74
75 server {
76 listen 127.0.0.1:8083;
77 js_preread test.cb_mismatch;
78 proxy_pass 127.0.0.1:8090;
79 }
80
81 server {
82 listen 127.0.0.1:8084;
83 js_preread test.cb_mismatch2;
84 proxy_pass 127.0.0.1:8090;
85 }
86
87 server {
88 listen 127.0.0.1:8085;
89 js_filter test.header_inject;
90 proxy_pass 127.0.0.1:8080;
91 }
92 }
93
94 EOF
95
96 $t->write_file('test.js', <<EOF);
97 function test_njs(r) {
98 r.return(200, njs.version);
99 }
100
101 function type(s) {
102 var v = s.vars.remote_addr;
103 var type = Buffer.isBuffer(v) ? 'buffer' : (typeof v);
104 return type;
105 }
106
107 function binary_var(s) {
108 var test = s.vars.binary_remote_addr.equals(Buffer.from([127,0,0,1]));
109 return test;
110 }
111
112 function cb_mismatch(s) {
113 try {
114 s.on('upload', () => {});
115 s.on('downstream', () => {});
116 } catch (e) {
117 throw new Error(`cb_mismatch:\${e.message}`)
118 }
119 }
120
121 function cb_mismatch2(s) {
122 try {
123 s.on('upstream', () => {});
124 s.on('download', () => {});
125 } catch (e) {
126 throw new Error(`cb_mismatch2:\${e.message}`)
127 }
128 }
129
130 function header_inject(s) {
131 var req = Buffer.from([]);
132
133 s.on('upstream', function(data, flags) {
134 req = Buffer.concat([req, data]);
135
136 var n = req.indexOf('\\n');
137 if (n != -1) {
138 var rest = req.slice(n + 1);
139 req = req.slice(0, n + 1);
140
141 s.send(req, flags);
142 s.send('Foo: foo\\r\\n', flags);
143 s.send(rest, flags);
144
145 s.off('upstream');
146 }
147 });
148 }
149
150 export default {njs: test_njs, type, binary_var, cb_mismatch, cb_mismatch2,
151 header_inject};
152
153 EOF
154
155 $t->try_run('no njs ngx')->plan(5);
156
157 ###############################################################################
158
159 TODO: {
160 local $TODO = 'not yet'
161 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.0';
162
163 is(stream('127.0.0.1:' . port(8081))->read(), 'buffer', 'var type');
164 is(stream('127.0.0.1:' . port(8082))->read(), 'true', 'binary var');
165
166 stream('127.0.0.1:' . port(8083))->io('x');
167 stream('127.0.0.1:' . port(8084))->io('x');
168
169 like(http_get('/p/return'), qr/RETURN:foo/, 'injected header');
170
171 $t->stop();
172
173 ok(index($t->read_file('error.log'), 'cb_mismatch:mixing string and buffer')
174 > 0, 'cb mismatch');
175 ok(index($t->read_file('error.log'), 'cb_mismatch2:mixing string and buffer')
176 > 0, 'cb mismatch');
177 }
178
179 ###############################################################################