1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
#!/usr/bin/perl
# (C) Dmitry Volyntsev
# (C) Nginx, Inc.
# Tests for http njs module, working with headers.
###############################################################################
use warnings;
use strict;
use Test::More;
use Socket qw/ CRLF /;
BEGIN { use FindBin; chdir($FindBin::Bin); }
use lib 'lib';
use Test::Nginx;
###############################################################################
select STDERR; $| = 1;
select STDOUT; $| = 1;
my $t = Test::Nginx->new()->has(qw/http charset/)
->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
js_set $test_foo_in test.foo_in;
js_set $test_ifoo_in test.ifoo_in;
js_import test.js;
server {
listen 127.0.0.1:8080;
server_name localhost;
location /njs {
js_content test.njs;
}
location /content_length {
js_content test.content_length;
}
location /content_length_arr {
js_content test.content_length_arr;
}
location /content_length_keys {
js_content test.content_length_keys;
}
location /content_type {
charset windows-1251;
default_type text/plain;
js_content test.content_type;
}
location /content_type_arr {
charset windows-1251;
default_type text/plain;
js_content test.content_type_arr;
}
location /content_encoding {
js_content test.content_encoding;
}
location /content_encoding_arr {
js_content test.content_encoding_arr;
}
location /date {
js_content test.date;
}
location /last_modified {
js_content test.last_modified;
}
location /location {
js_content test.location;
}
location /location_sr {
js_content test.location_sr;
}
location /_redirect {
return 307 $request_uri;
}
location /headers_list {
js_content test.headers_list;
}
location /foo_in {
return 200 $test_foo_in;
}
location /ifoo_in {
return 200 $test_ifoo_in;
}
location /in_lowkey {
js_content test.in_lowkey;
}
location /hdr_in {
js_content test.hdr_in;
}
location /raw_hdr_in {
js_content test.raw_hdr_in;
}
location /hdr_out {
js_content test.hdr_out;
}
location /raw_hdr_out {
js_content test.raw_hdr_out;
}
location /hdr_out_array {
js_content test.hdr_out_array;
}
location /hdr_out_set_cookie {
js_content test.hdr_out_set_cookie;
}
location /hdr_out_single {
js_content test.hdr_out_single;
}
location /ihdr_out {
js_content test.ihdr_out;
}
location /hdr_sorted_keys {
js_content test.hdr_sorted_keys;
}
location /hdr_out_special_set {
js_content test.hdr_out_special_set;
}
location /copy_subrequest_hdrs {
js_content test.copy_subrequest_hdrs;
}
location /server {
js_content test.server;
}
location = /subrequest {
internal;
js_content test.subrequest;
}
}
}
EOF
$t->write_file('test.js', <<EOF);
function test_njs(r) {
r.return(200, njs.version);
}
function content_length(r) {
if (njs.version_number >= 0x000705) {
var clength = r.headersOut['Content-Length'];
if (clength !== undefined) {
r.return(500, `Content-Length "\${clength}" is not empty`);
return;
}
}
delete r.headersOut['Content-Length'];
r.headersOut['Content-Length'] = '';
r.headersOut['Content-Length'] = 3;
delete r.headersOut['Content-Length'];
r.headersOut['Content-Length'] = 3;
r.sendHeader();
r.send('XXX');
r.finish();
}
function content_length_arr(r) {
r.headersOut['Content-Length'] = [5];
r.headersOut['Content-Length'] = [];
r.headersOut['Content-Length'] = [4,3];
r.sendHeader();
r.send('XXX');
r.finish();
}
function content_length_keys(r) {
r.headersOut['Content-Length'] = 3;
var in_keys = Object.keys(r.headersOut).some(v=>v=='Content-Length');
r.return(200, `B:\${in_keys}`);
}
function content_type(r) {
if (njs.version_number >= 0x000705) {
var ctype = r.headersOut['Content-Type'];
if (ctype !== undefined) {
r.return(500, `Content-Type "\${ctype}" is not empty`);
return;
}
}
delete r.headersOut['Content-Type'];
r.headersOut['Content-Type'] = 'text/xml';
r.headersOut['Content-Type'] = '';
r.headersOut['Content-Type'] = 'text/xml; charset=';
delete r.headersOut['Content-Type'];
r.headersOut['Content-Type'] = 'text/xml; charset=utf-8';
r.headersOut['Content-Type'] = 'text/xml; charset="utf-8"';
var in_keys = Object.keys(r.headersOut).some(v=>v=='Content-Type');
r.return(200, `B:\${in_keys}`);
}
function content_type_arr(r) {
r.headersOut['Content-Type'] = ['text/html'];
r.headersOut['Content-Type'] = [];
r.headersOut['Content-Type'] = [ 'text/xml', 'text/html'];
r.return(200);
}
function content_encoding(r) {
if (njs.version_number >= 0x000705) {
var ce = r.headersOut['Content-Encoding'];
if (ce !== undefined) {
r.return(500, `Content-Encoding "\${ce}" is not empty`);
return;
}
}
delete r.headersOut['Content-Encoding'];
r.headersOut['Content-Encoding'] = '';
r.headersOut['Content-Encoding'] = 'test';
delete r.headersOut['Content-Encoding'];
r.headersOut['Content-Encoding'] = 'gzip';
r.return(200);
}
function date(r) {
r.headersOut['Date'] = 'Sun, 09 Sep 2001 01:46:40 GMT';
r.return(200);
}
function last_modified(r) {
r.headersOut['Last-Modified'] = 'Sun, 09 Sep 2001 01:46:40 GMT';
r.return(200);
}
function location(r) {
if (njs.version_number >= 0x000705) {
var lc = r.headersOut['Location'];
if (lc !== undefined) {
r.return(500, `Location "\${lc}" is not empty`);
return;
}
}
delete r.headersOut['Location'];
r.headersOut['Location'] = '';
r.headersOut['Location'] = 'test';
delete r.headersOut['Location'];
r.headersOut['Location'] = 'loc';
r.return(200);
}
async function location_sr(r) {
let resp = await r.subrequest('/_redirect');
r.headersOut['Location'] = resp.headersOut['Location'];
r.return(resp.status, 'loc');
}
function content_encoding_arr(r) {
r.headersOut['Content-Encoding'] = 'test';
r.headersOut['Content-Encoding'] = [];
r.headersOut['Content-Encoding'] = ['test', 'gzip'];
r.return(200);
}
function headers_list(r) {
for (var h in {a:1, b:2, c:3}) {
r.headersOut[h] = h;
}
delete r.headersOut.b;
r.headersOut.d = 'd';
var out = "";
for (var h in r.headersOut) {
out += h + ":";
}
r.return(200, out);
}
function hdr_in(r) {
var s = '', h;
for (h in r.headersIn) {
s += `\${h.toLowerCase()}: \${r.headersIn[h]}\n`;
}
r.return(200, s);
}
function raw_hdr_in(r) {
var filtered = r.rawHeadersIn
.filter(v=>v[0].toLowerCase() == r.args.filter);
r.return(200, 'raw:' + filtered.map(v=>v[1]).join('|'));
}
function hdr_sorted_keys(r) {
var s = '';
var hdr = r.args.in ? r.headersIn : r.headersOut;
if (!r.args.in) {
r.headersOut.b = 'b';
r.headersOut.c = 'c';
r.headersOut.a = 'a';
}
r.return(200, Object.keys(hdr).sort());
}
function foo_in(r) {
return 'hdr=' + r.headersIn.foo;
}
function ifoo_in(r) {
var s = '', h;
for (h in r.headersIn) {
if (h.substr(0, 3) == 'foo') {
s += r.headersIn[h];
}
}
return s;
}
function in_lowkey(r) {
const name = 'X'.repeat(16);
let v = r.headersIn[name];
r.return(200, name);
}
function hdr_out(r) {
r.status = 200;
r.headersOut['Foo'] = r.args.foo;
if (r.args.bar) {
r.headersOut['Bar'] =
r.headersOut[(r.args.bar == 'empty' ? 'Baz' :'Foo')]
}
r.sendHeader();
r.finish();
}
function raw_hdr_out(r) {
r.headersOut.a = ['foo', 'bar'];
r.headersOut.b = 'b';
var filtered = r.rawHeadersOut
.filter(v=>v[0].toLowerCase() == r.args.filter);
r.return(200, 'raw:' + filtered.map(v=>v[1]).join('|'));
}
function hdr_out_array(r) {
if (!r.args.hidden) {
r.headersOut['Foo'] = [r.args.foo];
r.headersOut['Foo'] = [];
r.headersOut['Foo'] = ['bar', r.args.foo];
}
if (r.args.scalar_set) {
r.headersOut['Foo'] = 'xxx';
}
r.return(200, `B:\${r.headersOut.foo}`);
}
function hdr_out_single(r) {
r.headersOut.ETag = ['a', 'b'];
r.return(200, `B:\${r.headersOut.etag}`);
}
function hdr_out_set_cookie(r) {
r.headersOut['Set-Cookie'] = [];
r.headersOut['Set-Cookie'] = ['a', 'b'];
delete r.headersOut['Set-Cookie'];
r.headersOut['Set-Cookie'] = 'e';
r.headersOut['Set-Cookie'] = ['c', '', null, 'd', 'f'];
var cookies = r.headersOut['Set-Cookie'];
r.return(200, `B:\${cookies} \${Array.isArray(cookies)}`);
}
function ihdr_out(r) {
r.status = 200;
r.headersOut['a'] = r.args.a;
r.headersOut['b'] = r.args.b;
var s = '', h;
for (h in r.headersOut) {
s += r.headersOut[h];
}
r.sendHeader();
r.send(s);
r.finish();
}
function hdr_out_special_set(r) {
r.headersOut['Foo'] = "xxx";
r.headersOut['Content-Encoding'] = 'abc';
let ce = r.headersOut['Content-Encoding'];
r.return(200, `CE: \${ce}`);
}
async function copy_subrequest_hdrs(r) {
let resp = await r.subrequest("/subrequest");
for (const h in resp.headersOut) {
r.headersOut[h] = resp.headersOut[h];
}
r.return(200, resp.responseText);
}
function server(r) {
r.headersOut['Server'] = 'Foo';
r.return(200);
}
function subrequest(r) {
r.headersOut['A'] = 'a';
r.headersOut['Content-Encoding'] = 'ce';
r.headersOut['B'] = 'b';
r.headersOut['C'] = 'c';
r.headersOut['D'] = 'd';
r.headersOut['Set-Cookie'] = ['A', 'BB'];
r.headersOut['Content-Length'] = 3;
r.headersOut['Content-Type'] = 'ct';
r.sendHeader();
r.send('XXX');
r.finish();
}
export default {njs:test_njs, content_length, content_length_arr,
content_length_keys, content_type, content_type_arr,
content_encoding, content_encoding_arr, headers_list,
hdr_in, raw_hdr_in, hdr_sorted_keys, foo_in, ifoo_in,
hdr_out, raw_hdr_out, hdr_out_array, hdr_out_single,
hdr_out_set_cookie, ihdr_out, hdr_out_special_set,
copy_subrequest_hdrs, subrequest, date, last_modified,
location, location_sr, server, in_lowkey};
EOF
$t->try_run('no njs')->plan(50);
###############################################################################
like(http_get('/content_length'), qr/Content-Length: 3/,
'set Content-Length');
like(http_get('/content_type'), qr/Content-Type: text\/xml; charset="utf-8"\r/,
'set Content-Type');
unlike(http_get('/content_type'), qr/Content-Type: text\/plain/,
'set Content-Type 2');
like(http_get('/content_encoding'), qr/Content-Encoding: gzip/,
'set Content-Encoding');
like(http_get('/headers_list'), qr/a:c:d/, 'headers list');
like(http_get('/ihdr_out?a=12&b=34'), qr/^1234$/m, 'r.headersOut iteration');
like(http_get('/ihdr_out'), qr/\x0d\x0a?\x0d\x0a?$/m, 'r.send zero');
like(http_get('/hdr_out?foo=12345'), qr/Foo: 12345/, 'r.headersOut');
like(http_get('/hdr_out?foo=123&bar=copy'), qr/Bar: 123/, 'r.headersOut get');
unlike(http_get('/hdr_out?bar=empty'), qr/Bar:/, 'r.headersOut empty');
unlike(http_get('/hdr_out?foo='), qr/Foo:/, 'r.headersOut no value');
unlike(http_get('/hdr_out?foo'), qr/Foo:/, 'r.headersOut no value 2');
like(http_get('/content_length_keys'), qr/B:true/, 'Content-Length in keys');
like(http_get('/content_length_arr'), qr/Content-Length: 3/,
'set Content-Length arr');
like(http_get('/content_type'), qr/B:true/, 'Content-Type in keys');
like(http_get('/content_type_arr'), qr/Content-Type: text\/html/,
'set Content-Type arr');
like(http_get('/content_encoding_arr'), qr/Content-Encoding: gzip/,
'set Content-Encoding arr');
like(http_get('/hdr_out_array?foo=12345'), qr/Foo: bar\r\nFoo: 12345/,
'r.headersOut arr');
like(http_get('/hdr_out_array'), qr/Foo: bar/,
'r.headersOut arr last is empty');
like(http_get('/hdr_out_array?foo=abc'), qr/B:bar,\s?abc/,
'r.headersOut get');
like(http_get('/hdr_out_array'), qr/B:bar/, 'r.headersOut get2');
like(http_get('/hdr_out_array?hidden=1'), qr/B:undefined/,
'r.headersOut get3');
like(http_get('/hdr_out_array?scalar_set=1'), qr/B:xxx/,
'r.headersOut scalar set');
like(http_get('/hdr_out_single'), qr/ETag: a\r\nETag: b/,
'r.headersOut single');
like(http_get('/hdr_out_single'), qr/B:a/,
'r.headersOut single get');
like(http_get('/hdr_out_set_cookie'), qr/Set-Cookie: c\r\nSet-Cookie: d/,
'set_cookie');
like(http_get('/hdr_out_set_cookie'), qr/B:c,d,f true/,
'set_cookie2');
unlike(http_get('/hdr_out_set_cookie'), qr/Set-Cookie: [abe]/,
'set_cookie3');
like(http(
'GET /hdr_in HTTP/1.0' . CRLF
. 'Cookie: foo' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/cookie: foo/, 'r.headersIn cookie');
like(http(
'GET /hdr_in HTTP/1.0' . CRLF
. 'X-Forwarded-For: foo' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/x-forwarded-for: foo/, 'r.headersIn xff');
like(http(
'GET /hdr_in HTTP/1.0' . CRLF
. 'Cookie: foo1' . CRLF
. 'Cookie: foo2' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/cookie: foo1;\s?foo2/, 'r.headersIn cookie2');
like(http(
'GET /hdr_in HTTP/1.0' . CRLF
. 'X-Forwarded-For: foo1' . CRLF
. 'X-Forwarded-For: foo2' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/x-forwarded-for: foo1,\s?foo2/, 'r.headersIn xff2');
like(http(
'GET /hdr_in HTTP/1.0' . CRLF
. 'ETag: bar1' . CRLF
. 'ETag: bar2' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/etag: bar1(?!,\s?bar2)/, 'r.headersIn duplicate single');
like(http(
'GET /hdr_in HTTP/1.0' . CRLF
. 'Content-Type: bar1' . CRLF
. 'Content-Type: bar2' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/content-type: bar1(?!,\s?bar2)/, 'r.headersIn duplicate single 2');
like(http(
'GET /hdr_in HTTP/1.0' . CRLF
. 'Foo: bar1' . CRLF
. 'Foo: bar2' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/foo: bar1,\s?bar2/, 'r.headersIn duplicate generic');
like(http_get('/in_lowkey'), qr/X{16}/, 'r.headersIn name is not overwritten');
like(http(
'GET /raw_hdr_in?filter=foo HTTP/1.0' . CRLF
. 'foo: bar1' . CRLF
. 'Foo: bar2' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/raw: bar1|bar2/, 'r.rawHeadersIn');
like(http_get('/raw_hdr_out?filter=a'), qr/raw: foo|bar/, 'r.rawHeadersOut');
like(http(
'GET /hdr_sorted_keys?in=1 HTTP/1.0' . CRLF
. 'Cookie: foo1' . CRLF
. 'Accept: */*' . CRLF
. 'Cookie: foo2' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/Accept,Cookie,Host/, 'r.headersIn sorted keys');
like(http(
'GET /hdr_sorted_keys HTTP/1.0' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/a,b,c/, 'r.headersOut sorted keys');
TODO: {
local $TODO = 'not yet' unless has_version('0.7.6');
like(http_get('/hdr_out_special_set'), qr/CE: abc/,
'r.headerOut special set');
like(http_get('/copy_subrequest_hdrs'),
qr/A: a.*B: b.*C: c.*D: d.*Set-Cookie: A.*Set-Cookie: BB/s,
'subrequest copy');
like(http_get('/copy_subrequest_hdrs'),
qr/Content-Type: ct.*Content-Encoding: ce.*Content-Length: 3/s,
'subrequest copy special');
}
TODO: {
local $TODO = 'not yet' unless has_version('0.8.0');
like(http_get('/location'), qr/Location: loc/, 'set location');
unlike(http_get('/location_sr'), qr/Location: \/location_sr/,
'location redirect');
}
TODO: {
local $TODO = 'not yet' unless has_version('0.8.1');
like(http_get('/date'), qr/Date: Sun, 09 Sep 2001 01:46:40 GMT/,
'set date');
like(http_get('/last_modified'),
qr/Last-Modified: Sun, 09 Sep 2001 01:46:40 GMT/,
'set Last-Modified');
}
TODO: {
local $TODO = 'not yet' unless has_version('0.8.4');
like(http_get('/date'), qr/Server: nginx/, 'normal server');
like(http_get('/server'), qr/Server: Foo/, 'set server');
unlike(http_get('/server'), qr/Server: nginx/, 'set server 2');
}
###############################################################################
sub has_version {
my $need = shift;
http_get('/njs') =~ /^([.0-9]+)$/m;
my @v = split(/\./, $1);
my ($n, $v);
for $n (split(/\./, $need)) {
$v = shift @v || 0;
return 0 if $n > $v;
return 1 if $v > $n;
}
return 1;
}
###############################################################################
|