comparison js_headers.t @ 1521:b6699ffd9ddd

Tests: added Cookie and X-Forwarded-For r.headersIn tests.
author Dmitry Volyntsev <xeioex@nginx.com>
date Tue, 15 Oct 2019 19:36:27 +0300
parents 9521130f6f22
children ca0858ef7d3d
comparison
equal deleted inserted replaced
1520:9521130f6f22 1521:b6699ffd9ddd
10 use warnings; 10 use warnings;
11 use strict; 11 use strict;
12 12
13 use Test::More; 13 use Test::More;
14 14
15 use Socket qw/ CRLF /;
16
15 BEGIN { use FindBin; chdir($FindBin::Bin); } 17 BEGIN { use FindBin; chdir($FindBin::Bin); }
16 18
17 use lib 'lib'; 19 use lib 'lib';
18 use Test::Nginx; 20 use Test::Nginx;
19 21
33 } 35 }
34 36
35 http { 37 http {
36 %%TEST_GLOBALS_HTTP%% 38 %%TEST_GLOBALS_HTTP%%
37 39
38 js_set $test_hdr_in test_hdr_in; 40 js_set $test_foo_in test_foo_in;
39 js_set $test_ihdr_in test_ihdr_in; 41 js_set $test_ifoo_in test_ifoo_in;
40 42
41 js_include test.js; 43 js_include test.js;
42 44
43 server { 45 server {
44 listen 127.0.0.1:8080; 46 listen 127.0.0.1:8080;
65 67
66 location /headers_list { 68 location /headers_list {
67 js_content headers_list; 69 js_content headers_list;
68 } 70 }
69 71
72 location /foo_in {
73 return 200 $test_foo_in;
74 }
75
76 location /ifoo_in {
77 return 200 $test_ifoo_in;
78 }
79
70 location /hdr_in { 80 location /hdr_in {
71 return 200 $test_hdr_in; 81 js_content hdr_in;
72 }
73
74 location /ihdr_in {
75 return 200 $test_ihdr_in;
76 } 82 }
77 83
78 location /hdr_out { 84 location /hdr_out {
79 js_content hdr_out; 85 js_content hdr_out;
80 } 86 }
134 } 140 }
135 141
136 r.return(200, out); 142 r.return(200, out);
137 } 143 }
138 144
139 function test_hdr_in(r) { 145 function hdr_in(r) {
146 var s = '', h;
147 for (h in r.headersIn) {
148 s += `\${h.toLowerCase()}: \${r.headersIn[h]}\n`;
149 }
150
151 r.return(200, s);
152 }
153
154 function test_foo_in(r) {
140 return 'hdr=' + r.headersIn.foo; 155 return 'hdr=' + r.headersIn.foo;
141 } 156 }
142 157
143 function test_ihdr_in(r) { 158 function test_ifoo_in(r) {
144 var s = '', h; 159 var s = '', h;
145 for (h in r.headersIn) { 160 for (h in r.headersIn) {
146 if (h.substr(0, 3) == 'foo') { 161 if (h.substr(0, 3) == 'foo') {
147 s += r.headersIn[h]; 162 s += r.headersIn[h];
148 } 163 }
179 } 194 }
180 195
181 196
182 EOF 197 EOF
183 198
184 $t->try_run('no njs')->plan(12); 199 $t->try_run('no njs')->plan(16);
185 200
186 ############################################################################### 201 ###############################################################################
187 202
188 like(http_get('/content_length'), qr/Content-Length: 3/, 203 like(http_get('/content_length'), qr/Content-Length: 3/,
189 'set Content-Length'); 204 'set Content-Length');
201 like(http_get('/hdr_out?foo=123&bar=copy'), qr/Bar: 123/, 'r.headersOut get'); 216 like(http_get('/hdr_out?foo=123&bar=copy'), qr/Bar: 123/, 'r.headersOut get');
202 unlike(http_get('/hdr_out?bar=empty'), qr/Bar:/, 'r.headersOut empty'); 217 unlike(http_get('/hdr_out?bar=empty'), qr/Bar:/, 'r.headersOut empty');
203 unlike(http_get('/hdr_out?foo='), qr/Foo:/, 'r.headersOut no value'); 218 unlike(http_get('/hdr_out?foo='), qr/Foo:/, 'r.headersOut no value');
204 unlike(http_get('/hdr_out?foo'), qr/Foo:/, 'r.headersOut no value 2'); 219 unlike(http_get('/hdr_out?foo'), qr/Foo:/, 'r.headersOut no value 2');
205 220
206 ############################################################################### 221 like(http(
222 'GET /hdr_in HTTP/1.0' . CRLF
223 . 'Cookie: foo' . CRLF
224 . 'Host: localhost' . CRLF . CRLF
225 ), qr/cookie: foo/, 'r.headersIn cookie');
226
227 like(http(
228 'GET /hdr_in HTTP/1.0' . CRLF
229 . 'X-Forwarded-For: foo' . CRLF
230 . 'Host: localhost' . CRLF . CRLF
231 ), qr/x-forwarded-for: foo/, 'r.headersIn xff');
232
233
234 TODO: {
235 local $TODO = 'not yet'
236 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.3.6';
237
238 like(http(
239 'GET /hdr_in HTTP/1.0' . CRLF
240 . 'Cookie: foo1' . CRLF
241 . 'Cookie: foo2' . CRLF
242 . 'Host: localhost' . CRLF . CRLF
243 ), qr/cookie: foo1; foo2/, 'r.headersIn cookie2');
244
245 like(http(
246 'GET /hdr_in HTTP/1.0' . CRLF
247 . 'X-Forwarded-For: foo1' . CRLF
248 . 'X-Forwarded-For: foo2' . CRLF
249 . 'Host: localhost' . CRLF . CRLF
250 ), qr/x-forwarded-for: foo1, foo2/, 'r.headersIn xff2');
251
252 }
253
254 ###############################################################################