comparison stream_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 53ed2231403b
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 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 ###############################################################################