Mercurial > hg > nginx-tests
comparison js_var2.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, js_var directive in server | location contexts. | |
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 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 js_var $foo; | |
45 | |
46 location /test { | |
47 js_content test.test; | |
48 } | |
49 | |
50 location /sub { | |
51 return 200 DONE; | |
52 } | |
53 | |
54 location /dest { | |
55 js_var $bar a:$arg_a; | |
56 return 200 $bar; | |
57 } | |
58 } | |
59 } | |
60 | |
61 EOF | |
62 | |
63 $t->write_file('test.js', <<EOF); | |
64 function test(r) { | |
65 if (r.args.sub) { | |
66 r.subrequest('/sub') | |
67 .then(reply => { | |
68 r.variables.bar = reply.responseText; | |
69 r.internalRedirect('/dest'); | |
70 }); | |
71 | |
72 return; | |
73 } | |
74 | |
75 r.return(200, `V:\${r.variables[r.args.var]}`); | |
76 } | |
77 | |
78 export default {test}; | |
79 | |
80 EOF | |
81 | |
82 $t->try_run('no njs js_var')->plan(3); | |
83 | |
84 ############################################################################### | |
85 | |
86 like(http_get('/test?var=bar&a=qq'), qr/200 OK.*V:a:qq$/s, 'default value'); | |
87 like(http_get('/test?var=foo'), qr/200 OK.*V:$/s, 'default empty value'); | |
88 like(http_get('/test?sub=1&var=bar&a=qq'), qr/200 OK.*DONE$/s, 'value set'); | |
89 | |
90 ############################################################################### |