comparison stream_js_object.t @ 1559:9e5d38da7651

Tests: added object js tests.
author Dmitry Volyntsev <xeioex@nginx.com>
date Fri, 20 Mar 2020 16:32:06 +0300
parents
children f3ba4c74de31
comparison
equal deleted inserted replaced
1558:e9096949d5f5 1559:9e5d38da7651
1 #!/usr/bin/perl
2
3 # (C) Dmitry Volyntsev
4 # (C) Nginx, Inc.
5
6 # Tests for stream njs module, stream session object.
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 test;
53
54 js_include test.js;
55
56 server {
57 listen 127.0.0.1:8081;
58 return $test$status;
59 }
60 }
61
62 EOF
63
64 $t->write_file('test.js', <<EOF);
65 function test_njs(r) {
66 r.return(200, njs.version);
67 }
68
69 function to_string(s) {
70 return s.toString() === '[object Stream Session]';
71 }
72
73 function define_prop(s) {
74 Object.defineProperty(s.variables, 'status', {value:400});
75 return s.variables.status == 400;
76 }
77
78 function in_operator(s) {
79 return ['status', 'unknown']
80 .map(v=>v in s.variables)
81 .toString() === 'true,false';
82 }
83
84 function redefine_proto(s) {
85 s[0] = 'a';
86 s[1] = 'b';
87 s.length = 2;
88 Object.setPrototypeOf(s, Array.prototype);
89 return s.join('|') === 'a|b';
90 }
91
92 function get_own_prop_descs(s) {
93 return Object.getOwnPropertyDescriptors(s)['on'].value === s.on;
94 }
95
96 function test(s) {
97 return [ to_string,
98 define_prop,
99 in_operator,
100 redefine_proto,
101 get_own_prop_descs,
102 ].every(v=>v(s));
103 }
104
105 EOF
106
107 $t->try_run('no njs stream session object')->plan(1);
108
109 ###############################################################################
110
111 TODO: {
112 local $TODO = 'not yet'
113 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.4.0';
114
115 is(stream('127.0.0.1:' . port(8081))->read(), 'true400', 'var set');
116
117 }
118
119 ###############################################################################