comparison stream_js_fetch.t @ 1640:67adc5fd0548

Tests: added js tests for ngx.fetch() method.
author Dmitry Volyntsev <xeioex@nginx.com>
date Thu, 21 Jan 2021 18:19:37 +0000
parents
children 6b6a496ac984
comparison
equal deleted inserted replaced
1639:6c323c672a86 1640:67adc5fd0548
1 #!/usr/bin/perl
2
3 # (C) Dmitry Volyntsev
4 # (C) Nginx, Inc.
5
6 # Tests for stream njs module, fetch method.
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 stream/)
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
50 server {
51 listen 127.0.0.1:8080;
52 server_name aaa;
53
54 location /validate {
55 js_content test.validate;
56 }
57 }
58 }
59
60 stream {
61 %%TEST_GLOBALS_STREAM%%
62
63 js_import test.js;
64
65 server {
66 listen 127.0.0.1:8081;
67 js_preread test.preread_verify;
68 proxy_pass 127.0.0.1:8090;
69 }
70 }
71
72 EOF
73
74 $t->write_file('test.js', <<EOF);
75 function test_njs(r) {
76 r.return(200, njs.version);
77 }
78
79 function validate(r) {
80 r.return((r.requestText == 'QZ') ? 200 : 403);
81 }
82
83 function preread_verify(s) {
84 var collect = Buffer.from([]);
85
86 s.on('upstream', function (data, flags) {
87 collect = Buffer.concat([collect, data]);
88
89 if (collect.length >= 4 && collect.readUInt16BE(0) == 0xabcd) {
90 s.off('upstream');
91 ngx.fetch('http://127.0.0.1:8080/validate',
92 {body: collect.slice(2,4), headers: {Host:'aaa'}})
93 .then(reply => (reply.status == 200) ? s.done(): s.deny())
94
95 } else if (collect.length) {
96 s.deny();
97 }
98 });
99 }
100
101 export default {njs: test_njs, validate, preread_verify}
102 EOF
103
104 $t->try_run('no stream njs available')->plan(4);
105
106 $t->run_daemon(\&stream_daemon, port(8090));
107 $t->waitforsocket('127.0.0.1:' . port(8090));
108
109 ###############################################################################
110
111 local $TODO = 'not yet'
112 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.1';
113
114 is(stream('127.0.0.1:' . port(8081))->io('###'), '', 'preread not enough');
115 is(stream('127.0.0.1:' . port(8081))->io("\xAB\xCDQZ##"), "\xAB\xCDQZ##",
116 'preread validated');
117 is(stream('127.0.0.1:' . port(8081))->io("\xAC\xCDQZ##"), '',
118 'preread invalid magic');
119 is(stream('127.0.0.1:' . port(8081))->io("\xAB\xCDQQ##"), '',
120 'preread validation failed');
121
122 ###############################################################################
123
124 sub stream_daemon {
125 my $server = IO::Socket::INET->new(
126 Proto => 'tcp',
127 LocalAddr => '127.0.0.1:' . port(8090),
128 Listen => 5,
129 Reuse => 1
130 )
131 or die "Can't create listening socket: $!\n";
132
133 local $SIG{PIPE} = 'IGNORE';
134
135 while (my $client = $server->accept()) {
136 $client->autoflush(1);
137
138 log2c("(new connection $client)");
139
140 $client->sysread(my $buffer, 65536) or next;
141
142 log2i("$client $buffer");
143
144 log2o("$client $buffer");
145
146 $client->syswrite($buffer);
147
148 close $client;
149 }
150 }
151
152 sub log2i { Test::Nginx::log_core('|| <<', @_); }
153 sub log2o { Test::Nginx::log_core('|| >>', @_); }
154 sub log2c { Test::Nginx::log_core('||', @_); }
155
156 ###############################################################################