Mercurial > hg > nginx-tests
comparison geo.t @ 216:f2f37a52e42e
Tests: basic tests for geo and realip modules.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Sat, 12 May 2012 04:54:01 +0400 |
parents | |
children | ea574aae77cb |
comparison
equal
deleted
inserted
replaced
215:7f5095965c88 | 216:f2f37a52e42e |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Tests for nginx geo 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 geo/)->plan(6); | |
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 geo $geo { | |
39 127.0.0.0/8 loopback; | |
40 192.0.2.0/24 test; | |
41 0.0.0.0/0 world; | |
42 } | |
43 | |
44 geo $arg_ip $geo_from_arg { | |
45 default default; | |
46 | |
47 127.0.0.0/8 loopback; | |
48 192.0.2.0/24 test; | |
49 } | |
50 | |
51 geo $geo_proxy { | |
52 default default; | |
53 proxy 127.0.0.1/32; | |
54 127.0.0.0/8 loopback; | |
55 192.0.2.0/24 test; | |
56 } | |
57 | |
58 server { | |
59 listen 127.0.0.1:8080; | |
60 server_name localhost; | |
61 | |
62 location / { | |
63 add_header X-Geo $geo; | |
64 add_header X-Arg $geo_from_arg; | |
65 add_header X-XFF $geo_proxy; | |
66 } | |
67 } | |
68 } | |
69 | |
70 EOF | |
71 | |
72 $t->write_file('1', ''); | |
73 $t->run(); | |
74 | |
75 ############################################################################### | |
76 | |
77 like(http_get('/1'), qr/^X-Geo: loopback/m, 'geo'); | |
78 | |
79 like(http_get('/1?ip=192.0.2.1'), qr/^X-Arg: test/m, 'geo from variable'); | |
80 like(http_get('/1?ip=10.0.0.1'), qr/^X-Arg: default/m, 'geo default'); | |
81 | |
82 like(http_xff('192.0.2.1'), qr/^X-XFF: test/m, 'geo proxy'); | |
83 like(http_xff('10.0.0.1'), qr/^X-XFF: default/m, 'geo proxy default'); | |
84 like(http_xff('10.0.0.1, 192.0.2.1'), qr/^X-XFF: test/m, 'geo proxy long'); | |
85 | |
86 ############################################################################### | |
87 | |
88 sub http_xff { | |
89 my ($xff) = @_; | |
90 return http(<<EOF); | |
91 GET /1 HTTP/1.0 | |
92 Host: localhost | |
93 X-Forwarded-For: $xff | |
94 | |
95 EOF | |
96 } | |
97 | |
98 ############################################################################### |