comparison rewrite_unescape.t @ 149:2178954eee5d

Tests: add tests for rewrite escaping problems.
author Maxim Dounin <mdounin@mdounin.ru>
date Fri, 04 Mar 2011 17:34:57 +0300
parents
children c0ae29632905
comparison
equal deleted inserted replaced
148:b714d6df958c 149:2178954eee5d
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4
5 # Tests for escaping/unescaping in rewrite 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 rewrite/)->plan(9)
25 ->write_file_expand('nginx.conf', <<'EOF');
26
27 %%TEST_GLOBALS%%
28
29 master_process off;
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 /t1 {
43 rewrite ^ $arg_r? redirect;
44 }
45
46 location /t2 {
47 rewrite ^ http://example.com$request_uri? redirect;
48 }
49
50 location /t3 {
51 rewrite ^ http://example.com$uri redirect;
52 }
53
54 location /t4 {
55 rewrite ^(.*) http://example.com$1 redirect;
56 }
57
58 location /t5 {
59 rewrite ^ http://example.com/blah%20%3Fblah redirect;
60 }
61
62 location /t6 {
63 rewrite ^ http://example.com/blah%20%2Fblah redirect;
64 }
65 }
66 }
67
68 EOF
69
70 mkdir($t->testdir() . '/directory');
71
72 $t->run();
73
74 ###############################################################################
75
76 # Some rewrites and expected (?) behaviour
77 #
78 # /t1?r=http%3A%2F%2Fexample.com%2F%3Ffrom
79 # rewrite ^ $arg_r? redirect;
80 # expected: http://example.com/?from
81 # got: http://example.com/?from
82 #
83 # /t1?r=http%3A%2F%2Fexample.com%0D%0Asplit
84 # rewrite ^ $arg_r? redirect;
85 # expected: http://example.com%0D%0Asplit
86 # got: http://example.com%0D%0Asplit
87 #
88 # /t1?r=http%3A%2F%2Fexample.com%2F%3Ffrom%3Dblah
89 # rewrite ^ $arg_r? redirect;
90 # expected: http://example.com/?from=blah
91 # got: http://example.com/?from%3Dblah
92 #
93 # /blah%3Fblah
94 # rewrite ^ http://example.com$request_uri? redirect;
95 # expected: http://example.com/blah%3Fblah
96 # got: http://example.com/blah?blah
97 #
98 # /blah%3Fblah
99 # rewrite ^ http://example.com$uri redirect;
100 # expected: http://example.com/blah%3Fblah
101 # got: http://example.com/blah?blah
102 #
103 # /blah%3Fblah
104 # rewrite ^(.*) http://example.com$1 redirect;
105 # expected: http://example.com/blah%3Fblah
106 # got: http://example.com/blah?blah
107 #
108 # /
109 # rewrite ^ http://example.com/blah%3Fblah redirect;
110 # expected: http://example.com/blah%3Fblah
111 # got: http://example.com/blah?blah
112 #
113
114 location('/t1?r=http%3A%2F%2Fexample.com%2F%3Ffrom',
115 'http://example.com/?from', 'escaped argument');
116
117 location('/t1?r=http%3A%2F%2Fexample.com%0D%0Asplit',
118 'http://example.com%0D%0Asplit', 'escaped argument header splitting');
119
120 TODO: {
121 local $TODO = 'not yet';
122
123 # Fixing this cases will require major changes to the whole aproach and
124 # likely to break some currently working cases. On the other hand, current
125 # behaviour is far from acceptable. Should be carefully thought.
126
127 location('/t1?r=http%3A%2F%2Fexample.com%2F%3Ffrom%3Dblah',
128 'http://example.com/?from=blah', 'escaped argument with complex query');
129
130 location('/t2/blah%20%3Fblah',
131 'http://example.com/t2/blah%20%3Fblah', 'escaped $request_uri');
132
133 location('/t3/blah%20%3Fblah',
134 'http://example.com/t3/blah%20%3Fblah', 'escaped $uri');
135
136 location('/t4/blah%20%3Fblah',
137 'http://example.com/t4/blah%20%3Fblah', 'escaped $1');
138
139 location('/t5',
140 'http://example.com/blah%20%3Fblah', 'escaped static');
141
142 location('/t5?arg=blah',
143 'http://example.com/blah%20%3Fblah?arg=blah',
144 'escaped static with argument');
145
146 location('/t6',
147 'http://example.com/blah%20%2Fblah', 'escaped static slash');
148
149 }
150
151 ###############################################################################
152
153 sub location {
154 my ($url, $value, $name) = @_;
155 my $data = http_get($url);
156 if ($data !~ qr!^Location: (.*?)\x0d?$!ms) {
157 fail($name);
158 return;
159 }
160 my $location = $1;
161 is($location, $value, $name);
162 }
163
164 ###############################################################################