comparison ssi_if.t @ 190:1d66a6a509d0

Tests: SSI "if" statement tests.
author Valentin Bartenev <ne@vbart.ru>
date Fri, 23 Dec 2011 12:29:26 +0300
parents
children 241b522ce7a5
comparison
equal deleted inserted replaced
189:802fc0786165 190:1d66a6a509d0
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4 # (C) Valentin Bartenev
5
6 # Tests for nginx ssi module, "if" statement.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http ssi/)->plan(44);
26
27 $t->write_file_expand('nginx.conf', <<'EOF');
28
29 %%TEST_GLOBALS%%
30
31 daemon off;
32
33 events {
34 }
35
36 http {
37 %%TEST_GLOBALS_HTTP%%
38
39 server {
40 listen 127.0.0.1:8080;
41 server_name localhost;
42 location / {
43 ssi on;
44 }
45 }
46 }
47
48 EOF
49
50
51 my $if_elif_else =
52 '<!--#if expr="$arg_if" -->IF'
53 . '<!--#elif expr="$arg_elif" -->ELIF'
54 . '<!--#else -->ELSE'
55 . '<!--#endif -->';
56
57 my $zig = 'GOOD';
58 my $zag = 'GOOD';
59
60 foreach my $i (reverse 1 .. 15) {
61 if ($i % 2) {
62 $zig =
63 "<!--#if expr='\$arg_$i' -->$i<!--#else -->$zig<!--#endif -->";
64 $zag =
65 "<!--#if expr='\$arg_$i' -->$zag<!--#else -->$i<!--#endif -->";
66 } else {
67 $zig =
68 "<!--#if expr='\$arg_$i' -->$zig<!--#else -->$i<!--#endif -->";
69 $zag =
70 "<!--#if expr='\$arg_$i' -->$i<!--#else -->$zag<!--#endif -->";
71 }
72 }
73
74 $t->run();
75
76 ###############################################################################
77
78 $t->write_file('if_var.html', 'x<!--#if expr="$arg_v" -->OK<!--#endif -->x');
79
80 like(http_get('/if_var.html?v=1'), qr/^xOKx$/m, 'if variable exists');
81 like(http_get('/if_var.html'), qr/^xx$/m, 'if variable not exists');
82
83
84 $t->write_file('if_eq.html',
85 'x<!--#if expr="$arg_v = equal" -->OK<!--#endif -->x');
86
87 like(http_get('/if_eq.html?v=equal'), qr/^xOKx$/m, 'if var = text');
88 like(http_get('/if_eq.html?v=notequal'), qr/^xx$/m, 'if var = text (false)');
89
90
91 $t->write_file('if_neq.html',
92 'x<!--#if expr="equal != $arg_v" -->OK<!--#endif -->x');
93
94 like(http_get('/if_neq.html?v=notequal'), qr/^xOKx$/m, 'if text != var');
95 like(http_get('/if_neq.html?v=equal'), qr/^xx$/m, 'if text != var (false)');
96
97
98 $t->write_file('if_eq_re.html',
99 'x<!--#if expr="$arg_v = /re+gexp?/" -->OK<!--#endif -->x');
100
101 like(http_get('/if_eq_re.html?v=XreeeegexX'), qr/^xOKx$/m, 'if var = /regex/');
102 like(http_get('/if_eq_re.html?v=XrgxX'), qr/^xx$/m, 'if var = /regex/ (false)');
103
104
105 $t->write_file('if_neq_re.html',
106 'x<!--#if expr="$arg_v != /re+gexp?/" -->OK<!--#endif -->x');
107
108 like(http_get('/if_neq_re.html?v=XrgxX'), qr/^xOKx$/m, 'if var != /regex/');
109 like(http_get('/if_neq_re.html?v=XreeeegexX'), qr/^xx$/m,
110 'if var != /regex/ (false)');
111
112
113 $t->write_file('if_varvar.html',
114 'x<!--#if expr="$arg_v = var$arg_v2" -->OK<!--#endif -->x');
115
116 like(http_get('/if_varvar.html?v=varHERE&v2=HERE'), qr/^xOKx$/m,
117 'if var = complex');
118
119
120 TODO: {
121 local $TODO = 'support for captures in regexps';
122
123 $t->write_file('if_cap_re.html',
124 'x<!--#if expr="$arg_v = /(CAP\d).*(CAP\d)/" -->'
125 . '<!--#echo var="1" -->x<!--#echo var="2" -->'
126 . '<!--#endif -->x');
127
128 like(http_get('/if_cap_re.html?v=hereCAP1andCAP2'), qr/^xCAP1xCAP2x$/m,
129 'if regex with captures');
130
131
132 $t->write_file('if_ncap_re.html',
133 'x<!--#if expr="$arg_v = /(?P<ncap>HERE)/" -->'
134 . '<!--#echo var="ncap" -->'
135 . '<!--#endif -->x');
136
137 like(http_get('/if_ncap_re.html?v=captureHEREeee'), qr/^xHEREx$/m,
138 'if regex with named capture');
139
140 }
141
142
143 $t->write_file('if.html', 'x' . $if_elif_else . 'x');
144
145 like(http_get('/if.html?if=1'), qr/^xIFx$/m, 'if');
146 like(http_get('/if.html?if=1&elif=1'), qr/^xIFx$/m, 'if suppresses elif');
147 like(http_get('/if.html?elif=1'), qr/^xELIFx$/m, 'elif');
148 like(http_get('/if.html'), qr/^xELSEx$/m, 'else');
149
150
151 $t->write_file('if_multi.html',
152 'x<!--#if expr="$arg_1" -->IF1<!--#else -->ELSE1<!--#endif -->'
153 . 'x<!--#if expr="$arg_2" -->IF2<!--#else -->ELSE2<!--#endif -->'
154 . 'x<!--#if expr="$arg_3" -->IF3<!--#else -->ELSE3<!--#endif -->'
155 . 'x<!--#if expr="$arg_4" -->IF4<!--#else -->ELSE4<!--#endif -->'
156 . 'x<!--#if expr="$arg_5" -->IF5<!--#else -->ELSE5<!--#endif -->x');
157
158 like(http_get('/if_multi.html?1=t&2=t&3=t&4=t&5=t'),
159 qr/^xIF1xIF2xIF3xIF4xIF5x$/m, 'multiple if (sequentially)');
160 like(http_get('/if_multi.html?1=t&3=t&5=t'), qr/^xIF1xELSE2xIF3xELSE4xIF5x$/m,
161 'multiple if (interlaced)');
162 like(http_get('/if_multi.html?2=t&4=t'), qr/^xELSE1xIF2xELSE3xIF4xELSE5x$/m,
163 'multiple if (interlaced reversed)');
164
165
166 TODO: {
167 local $TODO = 'fix "if" conditions inside the "block" command';
168
169 $t->write_file('if_in_block.html',
170 '<!--#block name="one" -->' . $if_elif_else . '<!--#endblock -->'
171 . 'x<!--#include virtual="/404?$args" stub="one" -->x');
172
173 like(http_get('/if_in_block.html?if=1'), qr/^xIFx$/m, 'if (in block)');
174 like(http_get('/if_in_block.html?if=1&elif=1'), qr/^xIFx$/m,
175 'if suppresses elif (in block)');
176 like(http_get('/if_in_block.html?elif=1'), qr/^xELIFx$/m, 'elif (in block)');
177 like(http_get('/if_in_block.html'), qr/^xELSEx$/m, 'else (in block)');
178
179 }
180
181
182 $t->write_file('if_config_set_echo.html',
183 'x<!--#if expr="$arg_if" -->'
184 . '<!--#config timefmt="IF" -->'
185 . '<!--#set var="v" value="$date_gmt" -->'
186 . '<!--#echo var="v" -->'
187 . '<!--#else -->'
188 . '<!--#config timefmt="ELSE" -->'
189 . '<!--#set var="v" value="$date_gmt" -->'
190 . '<!--#echo var="v" -->'
191 . '<!--#endif -->x');
192
193 like(http_get('/if_config_set_echo.html?if=1'), qr/^xIFx$/m,
194 'if config-set-echo');
195 like(http_get('/if_config_set_echo.html'), qr/^xELSEx$/m,
196 'else config-set-echo');
197
198
199 $t->write_file('if_include.html',
200 'x<!--#if expr="$arg_if" -->'
201 . '<!--#include virtual="/if.html?if=1" -->'
202 . '<!--#else -->'
203 . '<!--#include virtual="/if.html" -->'
204 . '<!--#endif -->x');
205
206 like(http_get('/if_include.html?if=1'), qr/^xxIFxx$/m,
207 'if include');
208 like(http_get('/if_include.html'), qr/^xxELSExx$/m,
209 'else include');
210
211
212 $t->write_file('if_block.html',
213 '<!--#if expr="$arg_if" -->'
214 . '<!--#block name="one" -->IF<!--#endblock -->'
215 . '<!--#else -->'
216 . '<!--#block name="one" -->ELSE<!--#endblock -->'
217 . '<!--#endif -->'
218 . 'x<!--#include virtual="/404" stub="one" -->x');
219
220 like(http_get('/if_block.html?if=1'), qr/^xIFx$/m, 'if block');
221 like(http_get('/if_block.html'), qr/^xELSEx$/m, 'else block');
222
223
224 TODO: {
225 local $TODO = 'support for nested ifs';
226
227 $t->write_file('ifif.html',
228 'x<!--#if expr="$arg__if" -->IFx' . $if_elif_else
229 . '<!--#elif expr="$arg__elif" -->ELIFx' . $if_elif_else
230 . '<!--#else -->ELSEx' . $if_elif_else
231 . '<!--#endif -->x');
232
233 like(http_get('/ifif.html?_if=1&if=1'), qr/^xIFxIFx$/m, 'if if');
234 like(http_get('/ifif.html?_if=1&elif=1'), qr/^xIFxELIFx$/m, 'if elif');
235 like(http_get('/ifif.html?_if=1'), qr/^xIFxELSEx$/m, 'if else');
236
237 like(http_get('/ifif.html?_elif=1&if=1'), qr/^xELIFxIFx$/m, 'elif if');
238 like(http_get('/ifif.html?_elif=1&elif=1'), qr/^xELIFxELIFx$/m, 'elif elif');
239 like(http_get('/ifif.html?_elif=1'), qr/^xELIFxELSEx$/m, 'elif else');
240
241 like(http_get('/ifif.html?if=1'), qr/^xELSExIFx$/m, 'else if');
242 like(http_get('/ifif.html?elif=1'), qr/^xELSExELIFx$/m, 'else elif');
243 like(http_get('/ifif.html'), qr/^xELSExELSEx$/m, 'else else');
244
245
246 $t->write_file('zigzag.html',
247 "x<!--#if expr='\$arg_0' -->$zig<!--#else -->$zag<!--#endif -->x");
248
249 like(http_get('/zigzag.html?0=t&2=t&4=t&6=t&8=t&10=t&12=t&14=t'),
250 qr/^xGOODx$/m, 'zigzag');
251 like(http_get('/zigzag.html?1=t&3=t&5=t&7=t&9=t&11=t&13=t&15=t'),
252 qr/^xGOODx$/m, 'zagzig');
253
254
255 $t->write_file('zigzag_block.html',
256 '<!--#block name="one" -->'
257 . "x<!--#if expr='\$arg_0' -->$zig<!--#else -->$zag<!--#endif -->x"
258 . '<!--#endblock -->'
259 . 'x<!--#include virtual="/404?$args" stub="one" -->x');
260
261 like(http_get('/zigzag_block.html?0=t&2=t&4=t&6=t&8=t&10=t&12=t&14=t'),
262 qr/^xGOODx$/m, 'zigzag block');
263 like(http_get('/zigzag_block.html?1=t&3=t&5=t&7=t&9=t&11=t&13=t&15=t'),
264 qr/^xGOODx$/m, 'zagzig block');
265
266 }
267
268
269 like(`grep -F '[alert]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no alerts');
270
271 ###############################################################################