comparison js.t @ 708:71a2d58c0a7f

Tests: JavaScript tests.
author Roman Arutyunyan <arut@nginx.com>
date Wed, 15 Jul 2015 20:29:01 -0700
parents
children ad8134cd6f45
comparison
equal deleted inserted replaced
707:01e5d5717815 708:71a2d58c0a7f
1 #!/usr/bin/perl
2
3 # (C) Roman Arutyunyan
4
5 # Tests for http JavaScript module.
6
7 ###############################################################################
8
9 use warnings;
10 use strict;
11
12 use Test::More;
13
14 BEGIN { use FindBin; chdir($FindBin::Bin); }
15
16 use lib 'lib';
17 use Test::Nginx;
18
19 ###############################################################################
20
21 select STDERR; $| = 1;
22 select STDOUT; $| = 1;
23
24 my $t = Test::Nginx->new()->has(qw/http rewrite js/)->plan(13)
25 ->write_file_expand('nginx.conf', <<'EOF');
26
27 %%TEST_GLOBALS%%
28
29 daemon off;
30
31 events {
32 }
33
34 http {
35 %%TEST_GLOBALS_HTTP%%
36
37 js_set $test_method "'method=' + $r.method";
38 js_set $test_version "'version=' + $r.httpVersion";
39 js_set $test_addr "'addr=' + $r.remoteAddress";
40 js_set $test_uri "'uri=' + $r.uri";
41 js_set $test_hdr "'hdr=' + $r.headers.foo";
42 js_set $test_ihdr "var s;
43 s = '';
44 for (h in $r.headers) {
45 if (h.substr(0, 3) == 'foo') {
46 s += $r.headers[h];
47 }
48 }
49 s;";
50 js_set $test_arg "'arg=' + $r.args.foo";
51 js_set $test_iarg "var s;
52 s = '';
53 for (a in $r.args) {
54 if (a.substr(0, 3) == 'foo') {
55 s += $r.args[a];
56 }
57 }
58 s;";
59
60 server {
61 listen 127.0.0.1:8080;
62 server_name localhost;
63
64 location /req_method {
65 return 200 $test_method;
66 }
67
68 location /req_version {
69 return 200 $test_version;
70 }
71
72 location /req_addr {
73 return 200 $test_addr;
74 }
75
76 location /req_uri {
77 return 200 $test_uri;
78 }
79
80 location /req_hdr {
81 return 200 $test_hdr;
82 }
83
84 location /req_ihdr {
85 return 200 $test_ihdr;
86 }
87
88 location /req_arg {
89 return 200 $test_arg;
90 }
91
92 location /req_iarg {
93 return 200 $test_iarg;
94 }
95
96 location /res_status {
97 js_run "
98 var res;
99 res = $r.response;
100 res.status = 204;
101 res.sendHeader();
102 res.finish();
103 ";
104 }
105
106 location /res_ctype {
107 js_run "
108 var res;
109 res = $r.response;
110 res.status = 200;
111 res.contentType = 'application/foo';
112 res.sendHeader();
113 res.finish();
114 ";
115 }
116
117 location /res_clen {
118 js_run "
119 var res;
120 res = $r.response;
121 res.status = 200;
122 res.contentLength = 5;
123 res.sendHeader();
124 res.send('foo12');
125 res.finish();
126 ";
127 }
128
129 location /res_send {
130 js_run "
131 var res, a, s;
132 res = $r.response;
133 res.status = 200;
134 res.sendHeader();
135 for (a in $r.args) {
136 if (a.substr(0, 3) == 'foo') {
137 s = $r.args[a];
138 res.send('n=' + a + ', v=' + s.substr(0, 2) + ' ');
139 }
140 }
141 res.finish();
142 ";
143 }
144
145 location /res_hdr {
146 js_run "
147 var res;
148 res = $r.response;
149 res.status = 200;
150 res.headers['Foo'] = $r.args.fOO;
151 res.sendHeader();
152 res.finish();
153 ";
154 }
155 }
156 }
157
158 EOF
159
160 $t->run();
161
162 ###############################################################################
163
164 like(http_get('/req_method'), qr/method=GET/, 'r.method');
165 like(http_get('/req_version'), qr/version=1.0/, 'r.httpVersion');
166 like(http_get('/req_addr'), qr/addr=127.0.0.1/, 'r.remoteAddress');
167 like(http_get('/req_uri'), qr/uri=\/req_uri/, 'r.uri');
168 like(http_get_hdr('/req_hdr'), qr/hdr=12345/, 'r.headers');
169 like(http_get_ihdr('/req_ihdr'), qr/12345barz/, 'r.headers iteration');
170 like(http_get('/req_arg?foO=12345'), qr/arg=12345/, 'r.args');
171 like(http_get('/req_iarg?foo=12345&foo2=bar&nn=22&foo-3=z'), qr/12345barz/,
172 'r.args iteration');
173
174 like(http_get('/res_status'), qr/204 No Content/, 'r.response.status');
175 like(http_get('/res_ctype'), qr/Content-Type: application\/foo/,
176 'r.response.contentType');
177 like(http_get('/res_clen'), qr/Content-Length: 5/, 'r.response.contentLength');
178 like(http_get('/res_send?foo=12345&n=11&foo-2=bar&ndd=&foo-3=z'),
179 qr/n=foo, v=12 n=foo-2, v=ba n=foo-3, v=z/, 'r.response.send');
180 like(http_get('/res_hdr?foo=12345'), qr/Foo: 12345/, 'r.response.headers');
181
182 ###############################################################################
183
184 sub http_get_hdr {
185 my ($url, %extra) = @_;
186 return http(<<EOF, %extra);
187 GET $url HTTP/1.0
188 FoO: 12345
189
190 EOF
191 }
192
193 sub http_get_ihdr {
194 my ($url, %extra) = @_;
195 return http(<<EOF, %extra);
196 GET $url HTTP/1.0
197 foo: 12345
198 Host: localhost
199 foo2: bar
200 X-xxx: more
201 foo-3: z
202
203 EOF
204 }
205
206 ###############################################################################