Skip to content

Commit c974f7d

Browse files
committed
Tests: njs tests for setting nginx variables.
1 parent 88621f1 commit c974f7d

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed

js_variables.t

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
###############################################################################

stream_js_variables.t

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Dmitry Volyntsev
4+
# (C) Nginx, Inc.
5+
6+
# Tests for stream 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+
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/http stream stream_return/)
27+
->write_file_expand('nginx.conf', <<'EOF');
28+
29+
%%TEST_GLOBALS%%
30+
31+
daemon off;
32+
33+
events {
34+
}
35+
36+
http {
37+
%%TEST_GLOBALS_HTTP%%
38+
39+
js_include test.js;
40+
41+
server {
42+
listen 127.0.0.1:8080;
43+
server_name localhost;
44+
45+
location /njs {
46+
js_content test_njs;
47+
}
48+
}
49+
}
50+
51+
stream {
52+
js_set $test_var test_var;
53+
js_set $test_not_found test_not_found;
54+
55+
js_include test.js;
56+
57+
server {
58+
listen 127.0.0.1:8081;
59+
return $test_var$status;
60+
}
61+
62+
server {
63+
listen 127.0.0.1:8082;
64+
return $test_not_found;
65+
}
66+
}
67+
68+
EOF
69+
70+
$t->write_file('test.js', <<EOF);
71+
function test_njs(r) {
72+
r.return(200, njs.version);
73+
}
74+
75+
function test_var(s) {
76+
s.variables.status = 400;
77+
return 'test_var';
78+
}
79+
80+
function test_not_found(s) {
81+
try {
82+
s.variables.unknown = 1;
83+
} catch (e) {
84+
return 'not_found';
85+
}
86+
}
87+
88+
EOF
89+
90+
$t->try_run('no stream njs available')->plan(2);
91+
92+
###############################################################################
93+
94+
TODO: {
95+
local $TODO = 'not yet'
96+
unless get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.2.8';
97+
98+
is(stream('127.0.0.1:' . port(8081))->read(), 'test_var400', 'var set');
99+
is(stream('127.0.0.1:' . port(8082))->read(), 'not_found', 'not found set');
100+
101+
}
102+
103+
$t->stop();
104+
105+
###############################################################################
106+
#
107+
sub get {
108+
my ($url, %extra) = @_;
109+
110+
my $s = IO::Socket::INET->new(
111+
Proto => 'tcp',
112+
PeerAddr => '127.0.0.1:' . port(8080)
113+
) or die "Can't connect to nginx: $!\n";
114+
115+
return http_get($url, socket => $s);
116+
}
117+
118+
###############################################################################

0 commit comments

Comments
 (0)