comparison js_body_filter_if.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 http njs module, body filter, if 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
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http proxy rewrite/)
26 ->write_file_expand('nginx.conf', <<'EOF');
27
28 %%TEST_GLOBALS%%
29
30 daemon off;
31
32 events {
33 }
34
35 http {
36 %%TEST_GLOBALS_HTTP%%
37
38 js_import test.js;
39
40 server {
41 listen 127.0.0.1:8080;
42 server_name localhost;
43
44 location /njs {
45 js_content test.njs;
46 }
47
48 location /filter {
49 if ($arg_name ~ "prepend") {
50 js_body_filter test.prepend;
51 }
52
53 if ($arg_name ~ "append") {
54 js_body_filter test.append;
55 }
56
57 js_body_filter test.should_not_be_called;
58
59 proxy_pass http://127.0.0.1:8081/source;
60 }
61 }
62
63 server {
64 listen 127.0.0.1:8081;
65 server_name localhost;
66
67 location /source {
68 postpone_output 1;
69 js_content test.source;
70 }
71 }
72 }
73
74 EOF
75
76 $t->write_file('test.js', <<EOF);
77 function test_njs(r) {
78 r.return(200, njs.version);
79 }
80
81 function append(r, data, flags) {
82 r.sendBuffer(data, {last:false});
83
84 if (flags.last) {
85 r.sendBuffer("XXX", flags);
86 }
87 }
88
89 function chain(chunks, i) {
90 if (i < chunks.length) {
91 chunks.r.send(chunks[i++]);
92 setTimeout(chunks.chain, chunks.delay, chunks, i);
93
94 } else {
95 chunks.r.finish();
96 }
97 }
98
99 function source(r) {
100 var chunks = ['AAA', 'BB', 'C', 'DDDD'];
101 chunks.delay = 5;
102 chunks.r = r;
103 chunks.chain = chain;
104
105 r.status = 200;
106 r.sendHeader();
107 chain(chunks, 0);
108 }
109
110 function prepend(r, data, flags) {
111 r.sendBuffer("XXX");
112 r.sendBuffer(data, flags);
113 r.done();
114 }
115
116 export default {njs: test_njs, append, prepend, source};
117
118 EOF
119
120 $t->try_run('no njs body filter')->plan(2);
121
122 ###############################################################################
123
124 like(http_get('/filter?name=append'), qr/AAABBCDDDDXXX/, 'append');
125 like(http_get('/filter?name=prepend'), qr/XXXAAABBCDDDD/, 'prepend');
126
127 ###############################################################################