comparison 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 bc0990ea2e5b
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 http njs module, request object.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14 use Socket qw/ CRLF /;
15
16 BEGIN { use FindBin; chdir($FindBin::Bin); }
17
18 use lib 'lib';
19 use Test::Nginx;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 my $t = Test::Nginx->new()->has(qw/http/)
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 location /to_string {
50 js_content to_string;
51 }
52
53 location /define_prop {
54 js_content define_prop;
55 }
56
57 location /in_operator {
58 js_content in_operator;
59 }
60
61 location /redefine_bind {
62 js_content redefine_bind;
63 }
64
65 location /redefine_proxy {
66 js_content redefine_proxy;
67 }
68
69 location /redefine_proto {
70 js_content redefine_proto;
71 }
72
73 location /get_own_prop_descs {
74 js_content get_own_prop_descs;
75 }
76 }
77 }
78
79 EOF
80
81 $t->write_file('test.js', <<EOF);
82 function test_njs(r) {
83 r.return(200, njs.version);
84 }
85
86 function to_string(r) {
87 r.return(200, r.toString());
88 }
89
90 function define_prop(r) {
91 Object.defineProperty(r.headersOut, 'Foo', {value:'bar'});
92 r.return(200);
93 }
94
95 function in_operator(r) {
96 r.return(200, ['Foo', 'Bar'].map(v=>v in r.headersIn)
97 .toString() === 'true,false');
98 }
99
100 function redefine_bind(r) {
101 r.return = r.return.bind(r, 200);
102 r.return('redefine_bind');
103 }
104
105 function redefine_proxy(r) {
106 r.return_orig = r.return;
107 r.return = function (body) { this.return_orig(200, body);}
108 r.return('redefine_proxy');
109 }
110
111 function redefine_proto(r) {
112 r[0] = 'a';
113 r[1] = 'b';
114 r.length = 2;
115 Object.setPrototypeOf(r, Array.prototype);
116 r.return(200, r.join('|'));
117 }
118
119 function get_own_prop_descs(r) {
120 r.return(200,
121 Object.getOwnPropertyDescriptors(r)['log'].value === r.log);
122 }
123
124 EOF
125
126 $t->try_run('no njs request object')->plan(7);
127
128 ###############################################################################
129
130 TODO: {
131 local $TODO = 'not yet'
132 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.4.0';
133
134
135 like(http_get('/to_string'), qr/\[object Request\]/, 'toString');
136 like(http_get('/define_prop'), qr/Foo: bar/, 'define_prop');
137 like(http(
138 'GET /in_operator HTTP/1.0' . CRLF
139 . 'Foo: foo' . CRLF
140 . 'Host: localhost' . CRLF . CRLF
141 ), qr/true/, 'in_operator');
142 like(http_get('/redefine_bind'), qr/redefine_bind/, 'redefine_bind');
143 like(http_get('/redefine_proxy'), qr/redefine_proxy/, 'redefine_proxy');
144 like(http_get('/redefine_proto'), qr/a|b/, 'redefine_proto');
145 like(http_get('/get_own_prop_descs'), qr/true/, 'get_own_prop_descs');
146
147 }
148
149 ###############################################################################