-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathheaders.t
182 lines (133 loc) · 4.44 KB
/
headers.t
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
#!/usr/bin/perl
# (C) Sergey Kandaurov
# (C) Maxim Dounin
# (C) Nginx, Inc.
# Tests for headers module.
###############################################################################
use warnings;
use strict;
use Test::More;
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 proxy/)->plan(28)
->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
server {
listen 127.0.0.1:8080;
server_name localhost;
add_header X-URI $uri;
add_header X-Always $uri always;
add_header ETag foo always;
add_header ETag '' always;
expires epoch;
location /t1 {
}
location /nx {
}
location /epoch {
expires epoch;
}
location /max {
expires max;
}
location /off {
expires off;
}
location /access {
expires 2048;
location /access_inner {
# inherited from outer
}
}
location /negative {
expires -2048;
}
location /daily {
expires @15h30m33s;
}
location /modified {
expires modified 2048;
location /modified/proxy {
proxy_pass http://127.0.0.1:8081/modified;
}
}
location /var {
expires $arg_e;
location /var_inner {
# inherited from outer
}
location /var_modified {
expires modified $arg_e;
}
}
}
server {
listen 127.0.0.1:8081;
server_name localhost;
add_header Last-Modified "Mon, 28 Sep 1970 06:00:00 GMT";
}
}
EOF
$t->write_file('t1', '');
$t->write_file('epoch', '');
$t->write_file('max', '');
$t->write_file('off', '');
$t->write_file('access', '');
$t->write_file('access_inner', '');
$t->write_file('negative', '');
$t->write_file('daily', '');
$t->write_file('modified', '');
$t->write_file('var', '');
$t->write_file('var_inner', '');
$t->write_file('var_modified', '');
$t->run();
###############################################################################
my $r;
# test for header field presence
$r = http_get('/t1');
like($r, qr/Cache-Control/, 'good expires');
like($r, qr/X-URI/, 'good add_header');
like($r, qr/X-Always/, 'good add_header always');
unlike($r, qr/ETag/, 'good add_header always empty');
$r = http_get('/nx');
unlike($r, qr/Cache-Control/, 'bad expires');
unlike($r, qr/X-URI/, 'bad add_header');
like($r, qr/X-Always/, 'bad add_header always');
unlike($r, qr/ETag/, 'bad add_header always empty');
# various expires variants
like(http_get('/epoch'), qr/Expires:.*1970/, 'expires epoch');
like(http_get('/max'), qr/Expires:.*2037/, 'expires max');
unlike(http_get('/off'), qr/Expires:/, 'expires off');
like(http_get('/access'), qr/max-age=2048/, 'expires access');
like(http_get('/access_inner'), qr/max-age=2048/, 'expires inner');
like(http_get('/negative'), qr/no-cache/, 'expires negative');
like(http_get('/daily'), qr/Expires:.*:33 GMT/, 'expires daily');
like(http_get('/modified'), qr/max-age=204./, 'expires modified');
# "expires modified" with proxy
like(http_get('/modified/proxy'), qr/Expires: Mon, 28 Sep 1970 06:34:08 GMT/,
'expires modified proxy');
# expires with variables
like(http_get('/var?e=epoch'), qr/Expires:.*1970/, 'expires var epoch');
like(http_get('/var?e=max'), qr/Expires:.*2037/, 'expires var max');
unlike(http_get('/var?e=off'), qr/Expires:/, 'expires var off');
like(http_get('/var?e=2048'), qr/max-age=2048/, 'expires var access');
like(http_get('/var_inner?e=2048'), qr/max-age=2048/, 'expires var inner');
like(http_get('/var?e=-2048'), qr/no-cache/, 'expires var negative');
like(http_get('/var?e=@33s'), qr/Expires:.*:33 GMT/, 'expires var daily');
like(http_get('/var_modified?e=2048'), qr/max-age=204./,
'expires var modified');
# some invalid cases
unlike(http_get('/var'), qr/Expires/, 'expires var empty');
unlike(http_get('/var?e=bad'), qr/Expires/, 'expires var bad');
unlike(http_get('/var_modified?e=epoch'), qr/Expires/,
'expires var modified epoch');
###############################################################################