Skip to content

Commit a2064ef

Browse files
committed
Tests: rewrite module tests, the "if" directive.
1 parent fea22d5 commit a2064ef

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

rewrite_if.t

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Sergey Kandaurov
4+
# (C) Nginx, Inc.
5+
6+
# Tests for rewrite "if" condition.
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 rewrite/)->plan(33)
26+
->write_file_expand('nginx.conf', <<'EOF');
27+
28+
%%TEST_GLOBALS%%
29+
30+
daemon off;
31+
32+
events {
33+
}
34+
35+
http {
36+
%%TEST_GLOBALS_HTTP%%
37+
38+
server {
39+
listen 127.0.0.1:8080;
40+
server_name localhost;
41+
42+
location / {
43+
if ($arg_c) {
44+
return 204;
45+
}
46+
}
47+
48+
location /sp {
49+
if ( $arg_c ) {
50+
return 204;
51+
}
52+
}
53+
54+
location /eq {
55+
if ($arg_c = 1) {
56+
return 204;
57+
}
58+
}
59+
60+
location /not {
61+
if ($arg_c != 2) {
62+
return 204;
63+
}
64+
}
65+
66+
location /pos {
67+
if ($arg_c ~ foo) {
68+
return 204;
69+
}
70+
}
71+
72+
location /cpos {
73+
if ($arg_c ~* foo) {
74+
return 204;
75+
}
76+
}
77+
78+
location /neg {
79+
if ($arg_c !~ foo) {
80+
return 204;
81+
}
82+
}
83+
84+
location /cneg {
85+
if ($arg_c !~* foo) {
86+
return 204;
87+
}
88+
}
89+
90+
location /plain {
91+
if (-f %%TESTDIR%%/$arg_c) {
92+
return 204;
93+
}
94+
}
95+
96+
location /dir {
97+
if (-d %%TESTDIR%%/$arg_c) {
98+
return 204;
99+
}
100+
}
101+
102+
location /exist {
103+
if (-e %%TESTDIR%%/$arg_c) {
104+
return 204;
105+
}
106+
}
107+
108+
location /exec {
109+
if (-x %%TESTDIR%%/$arg_c) {
110+
return 204;
111+
}
112+
}
113+
114+
location /not_plain {
115+
if (!-f %%TESTDIR%%/$arg_c) {
116+
return 204;
117+
}
118+
}
119+
120+
location /not_dir {
121+
if (!-d %%TESTDIR%%/$arg_c) {
122+
return 204;
123+
}
124+
}
125+
126+
location /not_exist {
127+
if (!-e %%TESTDIR%%/$arg_c) {
128+
return 204;
129+
}
130+
}
131+
132+
location /not_exec {
133+
if (!-x %%TESTDIR%%/$arg_c) {
134+
return 204;
135+
}
136+
}
137+
}
138+
}
139+
140+
EOF
141+
142+
$t->write_file('file', '');
143+
mkdir($t->testdir() . '/dir');
144+
145+
$t->run();
146+
147+
###############################################################################
148+
149+
like(http_get('/?c=1'), qr/ 204 /, 'var');
150+
unlike(http_get('/?c=0'), qr/ 204 /, 'false');
151+
like(http_get('/sp?c=1'), qr/ 204 /, 'spaces');
152+
153+
like(http_get('/eq?c=1'), qr/ 204 /, 'equal');
154+
unlike(http_get('/eq?c=2'), qr/ 204 /, 'equal false');
155+
like(http_get('/not?c=1'), qr/ 204 /, 'not equal');
156+
unlike(http_get('/not?c=2'), qr/ 204 /, 'not equal false');
157+
158+
like(http_get('/pos?c=food'), qr/ 204 /, 'match');
159+
like(http_get('/cpos?c=FooD'), qr/ 204 /, 'match case');
160+
like(http_get('/neg?c=FooD'), qr/ 204 /, 'match negative');
161+
like(http_get('/cneg?c=bar'), qr/ 204 /, 'match negative case');
162+
163+
unlike(http_get('/pos?c=FooD'), qr/ 204 /, 'mismatch');
164+
unlike(http_get('/cpos?c=bar'), qr/ 204 /, 'mismatch case');
165+
unlike(http_get('/neg?c=food'), qr/ 204 /, 'mismatch negative');
166+
unlike(http_get('/cneg?c=FooD'), qr/ 204 /, 'mismatch negative case');
167+
168+
like(http_get('/plain?c=file'), qr/ 204 /, 'plain file');
169+
unlike(http_get('/plain?c=dir'), qr/ 204 /, 'plain dir');
170+
unlike(http_get('/not_plain?c=file'), qr/ 204 /, 'not plain file');
171+
like(http_get('/not_plain?c=dir'), qr/ 204 /, 'not plain dir');
172+
173+
unlike(http_get('/dir/?c=file'), qr/ 204 /, 'directory file');
174+
like(http_get('/dir?c=dir'), qr/ 204 /, 'directory dir');
175+
like(http_get('/not_dir?c=file'), qr/ 204 /, 'not directory file');
176+
unlike(http_get('/not_dir?c=dir'), qr/ 204 /, 'not directory dir');
177+
178+
like(http_get('/exist?c=file'), qr/ 204 /, 'exist file');
179+
like(http_get('/exist?c=dir'), qr/ 204 /, 'exist dir');
180+
unlike(http_get('/exist?c=nx'), qr/ 204 /, 'exist non-existent');
181+
unlike(http_get('/not_exist?c=file'), qr/ 204 /, 'not exist file');
182+
unlike(http_get('/not_exist?c=dir'), qr/ 204 /, 'not exist dir');
183+
like(http_get('/not_exist?c=nx'), qr/ 204 /, 'not exist non-existent');
184+
185+
SKIP: {
186+
skip 'no exec on win32', 4 if $^O eq 'MSWin32';
187+
188+
unlike(http_get('/exec?c=file'), qr/ 204 /, 'executable file');
189+
like(http_get('/exec?c=dir'), qr/ 204 /, 'executable dir');
190+
like(http_get('/not_exec?c=file'), qr/ 204 /, 'not executable file');
191+
unlike(http_get('/not_exec?c=dir'), qr/ 204 /, 'not executable dir');
192+
193+
}
194+
195+
###############################################################################

0 commit comments

Comments
 (0)