Mercurial > hg > nginx-tests
comparison js_variables.t @ 1442:4281225b0c52
Tests: njs tests for setting nginx variables.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Thu, 21 Feb 2019 20:42:20 +0300 |
parents | |
children | 9521130f6f22 |
comparison
equal
deleted
inserted
replaced
1441:2e81f7788cf7 | 1442:4281225b0c52 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Dmitry Volyntsev | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for http njs module, setting nginx variables. | |
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_set $test_var test_var; | |
39 | |
40 js_include test.js; | |
41 | |
42 server { | |
43 listen 127.0.0.1:8080; | |
44 server_name localhost; | |
45 | |
46 set $foo foo_orig; | |
47 | |
48 location /njs { | |
49 js_content test_njs; | |
50 } | |
51 | |
52 location /var_set { | |
53 return 200 $test_var$foo; | |
54 } | |
55 | |
56 location /content_set { | |
57 js_content content_set; | |
58 } | |
59 | |
60 location /not_found_set { | |
61 js_content not_found_set; | |
62 } | |
63 } | |
64 } | |
65 | |
66 EOF | |
67 | |
68 $t->write_file('test.js', <<EOF); | |
69 function test_njs(r) { | |
70 r.return(200, njs.version); | |
71 } | |
72 | |
73 function test_var(r) { | |
74 r.variables.foo = r.variables.arg_a; | |
75 return 'test_var'; | |
76 } | |
77 | |
78 function content_set(r) { | |
79 r.variables.foo = r.variables.arg_a; | |
80 r.return(200, r.variables.foo); | |
81 } | |
82 | |
83 function not_found_set(r) { | |
84 try { | |
85 r.variables.unknown = 1; | |
86 } catch (e) { | |
87 r.return(500, e); | |
88 } | |
89 } | |
90 | |
91 EOF | |
92 | |
93 $t->try_run('no njs')->plan(3); | |
94 | |
95 ############################################################################### | |
96 | |
97 TODO: { | |
98 local $TODO = 'not yet' | |
99 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.2.8'; | |
100 | |
101 like(http_get('/var_set?a=bar'), qr/test_varbar/, 'var set'); | |
102 like(http_get('/content_set?a=bar'), qr/bar/, 'content set'); | |
103 like(http_get('/not_found_set'), qr/variable not found/, 'not found exception'); | |
104 | |
105 } | |
106 | |
107 ############################################################################### |