Mercurial > hg > nginx-tests
comparison js_header_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, header 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 / { | |
49 if ($arg_name ~ "add") { | |
50 js_header_filter test.add; | |
51 } | |
52 | |
53 js_header_filter test.add2; | |
54 | |
55 proxy_pass http://127.0.0.1:8081/; | |
56 } | |
57 } | |
58 | |
59 server { | |
60 listen 127.0.0.1:8081; | |
61 server_name localhost; | |
62 | |
63 location / { | |
64 return 200; | |
65 } | |
66 } | |
67 } | |
68 | |
69 EOF | |
70 | |
71 $t->write_file('test.js', <<EOF); | |
72 function test_njs(r) { | |
73 r.return(200, njs.version); | |
74 } | |
75 | |
76 function add(r) { | |
77 r.headersOut['Foo'] = 'bar'; | |
78 } | |
79 | |
80 function add2(r) { | |
81 r.headersOut['Bar'] = 'xxx'; | |
82 } | |
83 | |
84 export default {njs: test_njs, add, add2}; | |
85 | |
86 EOF | |
87 | |
88 $t->try_run('no njs header filter')->plan(2); | |
89 | |
90 ############################################################################### | |
91 | |
92 like(http_get('/?name=add'), qr/Foo: bar/, 'header filter if'); | |
93 like(http_get('/'), qr/Bar: xxx/, 'header filter'); | |
94 | |
95 ############################################################################### |