-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathssi_if.t
273 lines (191 loc) · 7.79 KB
/
ssi_if.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
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
#!/usr/bin/perl
# (C) Maxim Dounin
# (C) Valentin Bartenev
# Tests for nginx ssi module, "if" statement.
###############################################################################
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 ssi/)->plan(43);
$t->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;
location / {
ssi on;
}
}
}
EOF
my $if_elif_else =
'<!--#if expr="$arg_if" -->IF'
. '<!--#elif expr="$arg_elif" -->ELIF'
. '<!--#else -->ELSE'
. '<!--#endif -->';
my $zig = 'GOOD';
my $zag = 'GOOD';
foreach my $i (reverse 1 .. 15) {
if ($i % 2) {
$zig =
"<!--#if expr='\$arg_$i' -->$i<!--#else -->$zig<!--#endif -->";
$zag =
"<!--#if expr='\$arg_$i' -->$zag<!--#else -->$i<!--#endif -->";
} else {
$zig =
"<!--#if expr='\$arg_$i' -->$zig<!--#else -->$i<!--#endif -->";
$zag =
"<!--#if expr='\$arg_$i' -->$i<!--#else -->$zag<!--#endif -->";
}
}
$t->run();
###############################################################################
$t->write_file('if_var.html', 'x<!--#if expr="$arg_v" -->OK<!--#endif -->x');
like(http_get('/if_var.html?v=1'), qr/^xOKx$/m, 'if variable exists');
like(http_get('/if_var.html'), qr/^xx$/m, 'if variable not exists');
$t->write_file('if_eq.html',
'x<!--#if expr="$arg_v = equal" -->OK<!--#endif -->x');
like(http_get('/if_eq.html?v=equal'), qr/^xOKx$/m, 'if var = text');
like(http_get('/if_eq.html?v=notequal'), qr/^xx$/m, 'if var = text (false)');
$t->write_file('if_neq.html',
'x<!--#if expr="equal != $arg_v" -->OK<!--#endif -->x');
like(http_get('/if_neq.html?v=notequal'), qr/^xOKx$/m, 'if text != var');
like(http_get('/if_neq.html?v=equal'), qr/^xx$/m, 'if text != var (false)');
SKIP: {
# PCRE may not be available unless we have rewrite module
skip 'no PCRE', 4 unless $t->has_module('rewrite');
$t->write_file('if_eq_re.html',
'x<!--#if expr="$arg_v = /re+gexp?/" -->OK<!--#endif -->x');
like(http_get('/if_eq_re.html?v=XreeeegexX'), qr/^xOKx$/m,
'if var = /regex/');
like(http_get('/if_eq_re.html?v=XrgxX'), qr/^xx$/m,
'if var = /regex/ (false)');
$t->write_file('if_neq_re.html',
'x<!--#if expr="$arg_v != /re+gexp?/" -->OK<!--#endif -->x');
like(http_get('/if_neq_re.html?v=XrgxX'), qr/^xOKx$/m,
'if var != /regex/');
like(http_get('/if_neq_re.html?v=XreeeegexX'), qr/^xx$/m,
'if var != /regex/ (false)');
}
$t->write_file('if_varvar.html',
'x<!--#if expr="$arg_v = var$arg_v2" -->OK<!--#endif -->x');
like(http_get('/if_varvar.html?v=varHERE&v2=HERE'), qr/^xOKx$/m,
'if var = complex');
SKIP: {
# PCRE may not be available unless we have rewrite module
skip 'no PCRE', 2 unless $t->has_module('rewrite');
$t->write_file('if_cap_re.html',
'x<!--#if expr="$arg_v = /(CAP\d).*(CAP\d)/" -->'
. '<!--#echo var="1" -->x<!--#echo var="2" -->'
. '<!--#endif -->x');
like(http_get('/if_cap_re.html?v=hereCAP1andCAP2'), qr/^xCAP1xCAP2x$/m,
'if regex with captures');
$t->write_file('if_ncap_re.html',
'x<!--#if expr="$arg_v = /(?P<ncap>HERE)/" -->'
. '<!--#echo var="ncap" -->'
. '<!--#endif -->x');
like(http_get('/if_ncap_re.html?v=captureHEREeee'), qr/^xHEREx$/m,
'if regex with named capture');
}
$t->write_file('if.html', 'x' . $if_elif_else . 'x');
like(http_get('/if.html?if=1'), qr/^xIFx$/m, 'if');
like(http_get('/if.html?if=1&elif=1'), qr/^xIFx$/m, 'if suppresses elif');
like(http_get('/if.html?elif=1'), qr/^xELIFx$/m, 'elif');
like(http_get('/if.html'), qr/^xELSEx$/m, 'else');
$t->write_file('if_multi.html',
'x<!--#if expr="$arg_1" -->IF1<!--#else -->ELSE1<!--#endif -->'
. 'x<!--#if expr="$arg_2" -->IF2<!--#else -->ELSE2<!--#endif -->'
. 'x<!--#if expr="$arg_3" -->IF3<!--#else -->ELSE3<!--#endif -->'
. 'x<!--#if expr="$arg_4" -->IF4<!--#else -->ELSE4<!--#endif -->'
. 'x<!--#if expr="$arg_5" -->IF5<!--#else -->ELSE5<!--#endif -->x');
like(http_get('/if_multi.html?1=t&2=t&3=t&4=t&5=t'),
qr/^xIF1xIF2xIF3xIF4xIF5x$/m, 'multiple if (sequentially)');
like(http_get('/if_multi.html?1=t&3=t&5=t'), qr/^xIF1xELSE2xIF3xELSE4xIF5x$/m,
'multiple if (interlaced)');
like(http_get('/if_multi.html?2=t&4=t'), qr/^xELSE1xIF2xELSE3xIF4xELSE5x$/m,
'multiple if (interlaced reversed)');
$t->write_file('if_in_block.html',
'<!--#block name="one" -->' . $if_elif_else . '<!--#endblock -->'
. 'x<!--#include virtual="/404?$args" stub="one" -->x');
like(http_get('/if_in_block.html?if=1'), qr/^xIFx$/m, 'if (in block)');
like(http_get('/if_in_block.html?if=1&elif=1'), qr/^xIFx$/m,
'if suppresses elif (in block)');
like(http_get('/if_in_block.html?elif=1'), qr/^xELIFx$/m, 'elif (in block)');
like(http_get('/if_in_block.html'), qr/^xELSEx$/m, 'else (in block)');
$t->write_file('if_config_set_echo.html',
'x<!--#if expr="$arg_if" -->'
. '<!--#config timefmt="IF" -->'
. '<!--#set var="v" value="$date_gmt" -->'
. '<!--#echo var="v" -->'
. '<!--#else -->'
. '<!--#config timefmt="ELSE" -->'
. '<!--#set var="v" value="$date_gmt" -->'
. '<!--#echo var="v" -->'
. '<!--#endif -->x');
like(http_get('/if_config_set_echo.html?if=1'), qr/^xIFx$/m,
'if config-set-echo');
like(http_get('/if_config_set_echo.html'), qr/^xELSEx$/m,
'else config-set-echo');
$t->write_file('if_include.html',
'x<!--#if expr="$arg_if" -->'
. '<!--#include virtual="/if.html?if=1" -->'
. '<!--#else -->'
. '<!--#include virtual="/if.html" -->'
. '<!--#endif -->x');
like(http_get('/if_include.html?if=1'), qr/^xxIFxx$/m,
'if include');
like(http_get('/if_include.html'), qr/^xxELSExx$/m,
'else include');
$t->write_file('if_block.html',
'<!--#if expr="$arg_if" -->'
. '<!--#block name="one" -->IF<!--#endblock -->'
. '<!--#else -->'
. '<!--#block name="one" -->ELSE<!--#endblock -->'
. '<!--#endif -->'
. 'x<!--#include virtual="/404" stub="one" -->x');
like(http_get('/if_block.html?if=1'), qr/^xIFx$/m, 'if block');
like(http_get('/if_block.html'), qr/^xELSEx$/m, 'else block');
TODO: {
local $TODO = 'support for nested ifs';
$t->write_file('ifif.html',
'x<!--#if expr="$arg__if" -->IFx' . $if_elif_else
. '<!--#elif expr="$arg__elif" -->ELIFx' . $if_elif_else
. '<!--#else -->ELSEx' . $if_elif_else
. '<!--#endif -->x');
like(http_get('/ifif.html?_if=1&if=1'), qr/^xIFxIFx$/m, 'if if');
like(http_get('/ifif.html?_if=1&elif=1'), qr/^xIFxELIFx$/m, 'if elif');
like(http_get('/ifif.html?_if=1'), qr/^xIFxELSEx$/m, 'if else');
like(http_get('/ifif.html?_elif=1&if=1'), qr/^xELIFxIFx$/m, 'elif if');
like(http_get('/ifif.html?_elif=1&elif=1'), qr/^xELIFxELIFx$/m, 'elif elif');
like(http_get('/ifif.html?_elif=1'), qr/^xELIFxELSEx$/m, 'elif else');
like(http_get('/ifif.html?if=1'), qr/^xELSExIFx$/m, 'else if');
like(http_get('/ifif.html?elif=1'), qr/^xELSExELIFx$/m, 'else elif');
like(http_get('/ifif.html'), qr/^xELSExELSEx$/m, 'else else');
$t->write_file('zigzag.html',
"x<!--#if expr='\$arg_0' -->$zig<!--#else -->$zag<!--#endif -->x");
like(http_get('/zigzag.html?0=t&2=t&4=t&6=t&8=t&10=t&12=t&14=t'),
qr/^xGOODx$/m, 'zigzag');
like(http_get('/zigzag.html?1=t&3=t&5=t&7=t&9=t&11=t&13=t&15=t'),
qr/^xGOODx$/m, 'zagzig');
$t->write_file('zigzag_block.html',
'<!--#block name="one" -->'
. "x<!--#if expr='\$arg_0' -->$zig<!--#else -->$zag<!--#endif -->x"
. '<!--#endblock -->'
. 'x<!--#include virtual="/404?$args" stub="one" -->x');
like(http_get('/zigzag_block.html?0=t&2=t&4=t&6=t&8=t&10=t&12=t&14=t'),
qr/^xGOODx$/m, 'zigzag block');
like(http_get('/zigzag_block.html?1=t&3=t&5=t&7=t&9=t&11=t&13=t&15=t'),
qr/^xGOODx$/m, 'zagzig block');
}
###############################################################################