comparison js_dump.t @ 1534:96fb3513345b

Tests: added js dump tests.
author Dmitry Volyntsev <xeioex@nginx.com>
date Tue, 26 Nov 2019 18:25:18 +0300
parents
children bc0990ea2e5b
comparison
equal deleted inserted replaced
1533:41dcbcf3381a 1534:96fb3513345b
1 #!/usr/bin/perl
2
3 # (C) Dmitry Volyntsev
4 # (C) Nginx, Inc.
5
6 # Tests for http njs module, request object dump.
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 rewrite/)
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 /dump {
50 js_content test_dump;
51 }
52
53 location /stringify {
54 js_content test_stringify;
55 }
56
57 location /stringify_subrequest {
58 js_content test_stringify_subrequest;
59 }
60
61 location /js_sub {
62 return 201 '{$request_method}';
63 }
64 }
65 }
66
67 EOF
68
69 $t->write_file('test.js', <<EOF);
70 function test_njs(r) {
71 r.return(200, njs.version);
72 }
73
74 function test_dump(r) {
75 r.headersOut.baz = 'bar';
76 r.return(200, njs.dump(r));
77 }
78
79 function test_stringify(r) {
80 r.headersOut.baz = 'bar';
81 var obj = JSON.parse(JSON.stringify(r));
82 r.return(200, JSON.stringify(obj));
83 }
84
85 function test_stringify_subrequest(r) {
86 r.subrequest('/js_sub', reply => {
87 r.return(200, JSON.stringify(reply))
88 });
89 }
90
91 EOF
92
93 $t->try_run('no njs dump')->plan(3);
94
95 ###############################################################################
96
97 TODO: {
98 local $TODO = 'not yet'
99 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.3.8';
100
101 like(http(
102 'GET /dump?v=1&t=x HTTP/1.0' . CRLF
103 . 'Foo: bar' . CRLF
104 . 'Foo2: bar2' . CRLF
105 . 'Host: localhost' . CRLF . CRLF
106 ), qr/method:'GET'/, 'njs.dump(r)');
107
108 like(http(
109 'GET /stringify?v=1&t=x HTTP/1.0' . CRLF
110 . 'Foo: bar' . CRLF
111 . 'Foo2: bar2' . CRLF
112 . 'Host: localhost' . CRLF . CRLF
113 ), qr/headersOut":\{"baz":"bar"}/, 'JSON.stringify(r)');
114
115 like(http(
116 'GET /stringify_subrequest HTTP/1.0' . CRLF
117 . 'Host: localhost' . CRLF . CRLF
118 ), qr/responseBody":"\{GET}"/, 'JSON.stringify(reply)');
119
120 }
121
122 ###############################################################################