Mercurial > hg > nginx-tests
comparison stream_js_import2.t @ 1782:390cbd46c46b
Tests: added js tests for directives in additional contexts.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Tue, 02 Aug 2022 20:35:06 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1781:386748f328b1 | 1782:390cbd46c46b |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Dmitry Volyntsev | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for stream njs module, js_import directive in server context. | |
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/stream stream_return/) | |
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 server { | |
40 listen 127.0.0.1:8081; | |
41 js_import foo from ./main.js; | |
42 js_set $test foo.bar.p; | |
43 return $test; | |
44 } | |
45 | |
46 server { | |
47 listen 127.0.0.1:8082; | |
48 | |
49 js_import lib.js; | |
50 | |
51 js_access lib.access; | |
52 js_preread lib.preread; | |
53 js_filter lib.filter; | |
54 proxy_pass 127.0.0.1:8083; | |
55 } | |
56 | |
57 server { | |
58 listen 127.0.0.1:8083; | |
59 return "x"; | |
60 } | |
61 } | |
62 | |
63 EOF | |
64 | |
65 $t->write_file('lib.js', <<EOF); | |
66 var res = ''; | |
67 | |
68 function access(s) { | |
69 res += '1'; | |
70 s.allow(); | |
71 } | |
72 | |
73 function preread(s) { | |
74 s.on('upload', function (data) { | |
75 res += '2'; | |
76 if (res.length >= 3) { | |
77 s.done(); | |
78 } | |
79 }); | |
80 } | |
81 | |
82 function filter(s) { | |
83 s.on('upload', function(data, flags) { | |
84 s.send(data); | |
85 res += '3'; | |
86 }); | |
87 | |
88 s.on('download', function(data, flags) { | |
89 if (!flags.last) { | |
90 res += '4'; | |
91 s.send(data); | |
92 | |
93 } else { | |
94 res += '5'; | |
95 s.send(res, {last:1}); | |
96 s.off('download'); | |
97 } | |
98 }); | |
99 } | |
100 | |
101 export default {access, preread, filter}; | |
102 | |
103 EOF | |
104 | |
105 $t->write_file('main.js', <<EOF); | |
106 export default {bar: {p(s) {return "P-TEST"}}}; | |
107 | |
108 EOF | |
109 | |
110 $t->try_run('no njs available')->plan(2); | |
111 | |
112 ############################################################################### | |
113 | |
114 is(stream('127.0.0.1:' . port(8081))->read(), 'P-TEST', 'foo.bar.p'); | |
115 is(stream('127.0.0.1:' . port(8082))->io('0'), 'x122345', 'lib.access'); | |
116 | |
117 ############################################################################### |