comparison referer.t @ 321:f98e8674361b

Tests: referer tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Tue, 20 Aug 2013 15:04:24 +0400
parents
children 61071d7d28c9
comparison
equal deleted inserted replaced
320:3da39688c14b 321:f98e8674361b
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4
5 # Tests for referer module.
6
7 ###############################################################################
8
9 use warnings;
10 use strict;
11
12 use Test::More;
13
14 BEGIN { use FindBin; chdir($FindBin::Bin); }
15
16 use lib 'lib';
17 use Test::Nginx;
18
19 ###############################################################################
20
21 select STDERR; $| = 1;
22 select STDOUT; $| = 1;
23
24 my $t = Test::Nginx->new()->has(qw/http referer rewrite/)->plan(49);
25
26 $t->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 another;
41
42 valid_referers server_names;
43 return 200 "$host value $invalid_referer";
44 }
45
46 server {
47 listen 127.0.0.1:8080;
48 server_name _;
49
50 location / {
51 valid_referers server_names;
52 return 200 "$host value $invalid_referer";
53 }
54 server_name below;
55 }
56
57 server {
58 listen 127.0.0.1:8080;
59 server_name localhost ~bar;
60
61 location /blocked {
62 valid_referers blocked www.example.org;
63 return 200 "value $invalid_referer";
64 }
65 location /none {
66 valid_referers none www.example.org;
67 return 200 "value $invalid_referer";
68 }
69 location /simple {
70 valid_referers www.example.org;
71 return 200 "value $invalid_referer";
72 }
73 location /regex {
74 valid_referers ~example.org;
75 return 200 "value $invalid_referer";
76 }
77 location /regex2 {
78 valid_referers ~example.org/uri;
79 return 200 "value $invalid_referer";
80 }
81 location /regex3 {
82 valid_referers ~example.org$;
83 return 200 "value $invalid_referer";
84 }
85 location /uri {
86 valid_referers www.example.org/uri;
87 return 200 "value $invalid_referer";
88 }
89 location /sn {
90 valid_referers server_names;
91 return 200 "value $invalid_referer";
92 }
93 location /sn_blocked {
94 valid_referers blocked server_names;
95 return 200 "value $invalid_referer";
96 }
97 location /wc {
98 valid_referers *.example.com *.example.org www.example.* example.*;
99 return 200 "value $invalid_referer";
100 }
101 location /wc2 {
102 valid_referers www.example.*/uri;
103 return 200 "value $invalid_referer";
104 }
105 }
106 }
107
108 EOF
109
110 $t->run();
111
112 ###############################################################################
113
114 ok(valid('/simple', 'http://www.example.org'), 'simple');
115 ok(valid('/simple', 'http://www.example.org/uri'), 'simple uri');
116 ok(valid('/simple', 'http://www.example.org:8080/uri'), 'simple port uri');
117 ok(!valid('/simple', 'localhost'), 'simple invalid');
118 ok(valid('/simple', 'https://www.example.org'), 'https');
119 ok(!valid('/simple', 'example.com'), 'no scheme');
120 ok(!valid('/simple'), 'no none');
121 ok(valid('/none'), 'none');
122 ok(!valid('/none', ''), 'none empty');
123
124 ok(valid('/blocked', 'www.example.org'), 'blocked');
125 ok(valid('/blocked', 'www.example.com'), 'blocked 2');
126 ok(valid('/blocked', 'http://su'), 'blocked short');
127 ok(valid('/blocked', 'foobar'), 'blocked short no scheme');
128 ok(valid('/blocked', ''), 'blocked empty');
129
130 ok(!valid('/simple', 'foobar'), 'small');
131 ok(valid('/simple', 'http://www.example.org/' . 'a' x 256), 'long uri');
132 ok(!valid('/simple', 'http://www.example.' . 'a' x 256), 'long hostname');
133 ok(!valid('/wc', 'http://example.' . 'a' x 256), 'long hostname wildcard');
134
135 ok(valid('/uri', 'http://www.example.org/uri'), 'uri');
136 ok(valid('/uri', 'http://www.example.org/urii'), 'uri prefix');
137 ok(!valid('/uri', 'http://www.example.org/uRi'), 'uri case');
138 ok(valid('/uri', 'http://www.example.org:8080/urii'), 'uri port');
139 ok(!valid('/uri', 'http://www.example.org/ur'), 'uri invalid len');
140 ok(!valid('/uri', 'http://www.example.org/urd'), 'uri invalid cmp');
141
142 ok(valid('/regex', 'http://www.example.org'), 'regex');
143 ok(valid('/regex', 'http://www.eXample.org'), 'regex caseless');
144 ok(valid('/regex', 'http://www.example.org/uri'), 'regex uri');
145 ok(!valid('/regex', 'http://www.example.com'), 'regex mismatch');
146
147 ok(valid('/regex2', 'http://www.example.org/uri'), 'regex 2 uri');
148 ok(!valid('/regex2', 'http://www.example.org'), 'regex 2 no uri');
149 ok(valid('/regex2', 'http://www.example.org/uRI'), 'regex 2 uri caseless');
150
151 TODO: {
152 local $TODO = 'not yet';
153
154 ok(valid('/regex3', 'https://www.eXample.org'), 'regex https');
155
156 }
157
158 ok(valid('/sn', 'http://localhost'), 'server_names');
159 ok(valid('/sn', 'http://localHost'), 'server_names caseless');
160 ok(valid('/sn', 'http://localhost/uri'), 'server_names uri');
161 ok(valid('/sn', 'http://foobar'), 'server_names regex');
162
163 TODO: {
164 local $TODO = 'not yet';
165
166 ok(valid('/sn', 'http://foobAr'), 'server_names regex caseless');
167 ok(valid('/sn', 'http://foobAr/uri'), 'server_names regex caseless uri');
168
169 }
170
171 ok(valid('/sn', 'http://foobar/uri'), 'server_names regex uri');
172 ok(!valid('/sn', 'localhost'), 'server_names no scheme');
173 ok(!valid('/sn', 'foobar'), 'server_names regex no scheme');
174 ok(valid('/sn_blocked', 'localhost'), 'server_names no scheme blocked');
175
176 ok(valid('/wc', 'http://www.example.org'), 'wildcard head');
177 ok(valid('/wc', 'http://www.example.net'), 'wildcard tail');
178 ok(valid('/wc2', 'http://www.example.net/uri'), 'wildcard uri');
179 ok(valid('/wc2', 'http://www.example.net/urii'), 'wildcard uri prefix');
180 ok(!valid('/wc2', 'http://www.example.net/uRI'), 'wildcard uri case');
181
182 ok(valid('/', 'http://another', 'another'), 'server context');
183
184 # server_name below valid_referers
185
186 TODO: {
187 local $TODO = 'not yet';
188
189 ok(valid('/', 'http://below', 'below'), 'server below');
190
191 }
192
193 ###############################################################################
194
195 sub valid {
196 my ($uri, $referer, $host) = @_;
197 my $text;
198
199 $host = 'localhost' unless defined $host;
200
201 unless (defined $referer) {
202 $text = http_get($uri);
203 } else {
204 $text = http(<<EOF);
205 GET $uri HTTP/1.0
206 Host: $host
207 Referer: $referer
208
209 EOF
210 }
211
212 $text =~ /value 1/ && return 0;
213 $text =~ /value/ && return 1;
214 fail("no valid_referers in $uri");
215 }
216
217 ###############################################################################