Mercurial > hg > nginx-tests
comparison stream_js_fetch_init.t @ 1807:01fcc82a435a
Tests: added stream js test for Fetch object reinitialization.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Tue, 15 Nov 2022 17:39:18 -0800 |
parents | |
children | 520fb74cce4c |
comparison
equal
deleted
inserted
replaced
1806:564f74bf6e4d | 1807:01fcc82a435a |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Dmitry Volyntsev | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for stream njs module, Response prototype reinitialization. | |
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 rewrite stream/) | |
27 ->write_file_expand('nginx.conf', <<'EOF'); | |
28 | |
29 %%TEST_GLOBALS%% | |
30 | |
31 daemon off; | |
32 | |
33 events { | |
34 } | |
35 | |
36 stream { | |
37 %%TEST_GLOBALS_STREAM%% | |
38 | |
39 js_import test.js; | |
40 | |
41 server { | |
42 listen 127.0.0.1:8081; | |
43 js_access test.access_ok; | |
44 proxy_pass 127.0.0.1:8090; | |
45 } | |
46 } | |
47 | |
48 http { | |
49 %%TEST_GLOBALS_HTTP%% | |
50 | |
51 js_import test.js; | |
52 | |
53 server { | |
54 listen 127.0.0.1:8080; | |
55 server_name localhost; | |
56 | |
57 location /njs { | |
58 js_content test.njs; | |
59 } | |
60 } | |
61 | |
62 server { | |
63 listen 127.0.0.1:8080; | |
64 server_name aaa; | |
65 | |
66 location /success { | |
67 return 200; | |
68 } | |
69 } | |
70 } | |
71 | |
72 EOF | |
73 | |
74 my $p = port(8080); | |
75 | |
76 $t->write_file('test.js', <<EOF); | |
77 function test_njs(r) { | |
78 r.return(200, njs.version); | |
79 } | |
80 | |
81 async function access_ok(s) { | |
82 let reply = await ngx.fetch('http://127.0.0.1:$p/success', | |
83 {headers: {Host:'aaa'}}); | |
84 | |
85 (reply.status == 200) ? s.allow(): s.deny(); | |
86 } | |
87 | |
88 export default {njs: test_njs, access_ok}; | |
89 EOF | |
90 | |
91 $t->try_run('no stream njs available')->plan(1); | |
92 | |
93 $t->run_daemon(\&stream_daemon, port(8090)); | |
94 $t->waitforsocket('127.0.0.1:' . port(8090)); | |
95 | |
96 ############################################################################### | |
97 | |
98 local $TODO = 'not yet' | |
99 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.7.9'; | |
100 | |
101 is(stream('127.0.0.1:' . port(8081))->io('ABC'), 'ABC', 'access fetch ok'); | |
102 | |
103 ############################################################################### | |
104 | |
105 sub stream_daemon { | |
106 my $server = IO::Socket::INET->new( | |
107 Proto => 'tcp', | |
108 LocalAddr => '127.0.0.1:' . port(8090), | |
109 Listen => 5, | |
110 Reuse => 1 | |
111 ) | |
112 or die "Can't create listening socket: $!\n"; | |
113 | |
114 local $SIG{PIPE} = 'IGNORE'; | |
115 | |
116 while (my $client = $server->accept()) { | |
117 $client->autoflush(1); | |
118 | |
119 log2c("(new connection $client)"); | |
120 | |
121 $client->sysread(my $buffer, 65536) or next; | |
122 | |
123 log2i("$client $buffer"); | |
124 | |
125 log2o("$client $buffer"); | |
126 | |
127 $client->syswrite($buffer); | |
128 | |
129 close $client; | |
130 } | |
131 } | |
132 | |
133 sub log2i { Test::Nginx::log_core('|| <<', @_); } | |
134 sub log2o { Test::Nginx::log_core('|| >>', @_); } | |
135 sub log2c { Test::Nginx::log_core('||', @_); } | |
136 | |
137 ############################################################################### |