Mercurial > hg > nginx-tests
comparison stream_js_preload_object.t @ 1789:c67b390cb9ef
Tests: added js tests for js_preload_object directive.
author | Vadim Zhestikov <v.zhestikov@f5.com> |
---|---|
date | Thu, 15 Sep 2022 16:53:57 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1788:78fe648d54a7 | 1789:c67b390cb9ef |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Vadim Zhestikov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for stream njs module, js_preload_object directive. | |
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 js_preload_object g1 from g.json; | |
40 | |
41 js_set $test foo.bar.p; | |
42 | |
43 js_import lib.js; | |
44 js_import foo from ./main.js; | |
45 | |
46 server { | |
47 listen 127.0.0.1:8081; | |
48 return $test; | |
49 } | |
50 | |
51 server { | |
52 listen 127.0.0.1:8082; | |
53 js_access lib.access; | |
54 js_preread lib.preread; | |
55 js_filter lib.filter; | |
56 proxy_pass 127.0.0.1:8083; | |
57 } | |
58 | |
59 server { | |
60 listen 127.0.0.1:8083; | |
61 return "x"; | |
62 } | |
63 } | |
64 | |
65 EOF | |
66 | |
67 $t->write_file('lib.js', <<EOF); | |
68 var res = ''; | |
69 | |
70 function access(s) { | |
71 res += g1.a; | |
72 s.allow(); | |
73 } | |
74 | |
75 function preread(s) { | |
76 s.on('upload', function (data) { | |
77 res += g1.b[1]; | |
78 if (res.length >= 3) { | |
79 s.done(); | |
80 } | |
81 }); | |
82 } | |
83 | |
84 function filter(s) { | |
85 s.on('upload', function(data, flags) { | |
86 s.send(data); | |
87 res += g1.c.prop[0].a; | |
88 }); | |
89 | |
90 s.on('download', function(data, flags) { | |
91 if (!flags.last) { | |
92 res += g1.b[3]; | |
93 s.send(data); | |
94 | |
95 } else { | |
96 res += g1.b[4]; | |
97 s.send(res, {last:1}); | |
98 s.off('download'); | |
99 } | |
100 }); | |
101 } | |
102 | |
103 export default {access, preread, filter}; | |
104 | |
105 EOF | |
106 | |
107 $t->write_file('main.js', <<EOF); | |
108 export default {bar: {p(s) {return g1.b[2]}}}; | |
109 | |
110 EOF | |
111 | |
112 $t->write_file('g.json', | |
113 '{"a":1, "b":[1,2,"element",4,5], "c":{"prop":[{"a":3}]}}'); | |
114 | |
115 $t->try_run('no js_preload_object available')->plan(2); | |
116 | |
117 ############################################################################### | |
118 | |
119 is(stream('127.0.0.1:' . port(8081))->read(), 'element', 'foo.bar.p'); | |
120 is(stream('127.0.0.1:' . port(8082))->io('0'), 'x122345', 'lib.access'); | |
121 | |
122 ############################################################################### |