comparison proxy_if.t @ 509:f135127e97af

Tests: various proxy_pass + if tests.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 08 Dec 2014 20:29:43 +0300
parents
children 4892d701d558
comparison
equal deleted inserted replaced
508:4ac60aad723e 509:f135127e97af
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4
5 # Tests for http proxy module related to use with the "if" directive.
6 # See http://wiki.nginx.org/IfIsEvil for more details.
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 proxy rewrite http_ssl/)->plan(15);
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
43 location / {
44 proxy_pass http://127.0.0.1:8081/;
45 }
46
47 # request will be sent to backend without uri changed
48 # to '/' due to if
49
50 location /proxy-pass-uri {
51 proxy_pass http://127.0.0.1:8081/replacement;
52
53 if ($arg_if) {
54 # nothing
55 }
56
57 location /proxy-pass-uri/inner {
58 # no proxy_pass here, static
59
60 if ($arg_if) {
61 # nothing
62 }
63 }
64 }
65
66 # same as the above, but there is a special handling
67 # in configuration merge; it may do wrong things though
68
69 location /proxy-pass-uri-lmt {
70 proxy_pass http://127.0.0.1:8081/replacement;
71
72 limit_except POST {
73 # nothing
74 }
75
76 location /proxy-pass-uri-lmt/inner {
77 # no proxy_pass here, static
78
79 limit_except POST {
80 # nothing
81 }
82 }
83 }
84
85 location /proxy-pass-uri-lmt-different {
86 proxy_pass http://127.0.0.1:8081/replacement;
87
88 limit_except POST {
89 proxy_pass http://127.0.0.1:8081;
90 }
91 }
92
93 # segmentation fault in old versions,
94 # fixed to return 500 Internal Error in nginx 1.3.10
95
96 location /proxy-inside-if-crash {
97
98 set $true 1;
99
100 if ($true) {
101 # proxy_pass inside if
102 proxy_pass http://127.0.0.1:8081;
103 }
104
105 if ($true) {
106 # no handler here
107 }
108 }
109
110 # normal proxy_pass and proxy_pass with variables
111 # use distinct field, and inheritance should be mutually
112 # exclusive
113
114 location /variables {
115 proxy_pass http://127.0.0.1:8081/outer/$host;
116
117 if ($arg_if) {
118 proxy_pass http://127.0.0.1:8081;
119 }
120
121 location /variables/inner {
122 proxy_pass http://127.0.0.1:8081;
123 }
124 }
125
126 # ssl context shouldn't be inherited into nested
127 # locations with different proxy_pass, but should
128 # be correctly inherited into if's
129
130 location /ssl {
131 proxy_pass https://127.0.0.1:8082/outer;
132
133 if ($arg_if) {
134 # inherited from outer
135 }
136
137 location /ssl/inner {
138 proxy_pass http://127.0.0.1:8081;
139 }
140 }
141 }
142
143 server {
144 listen 127.0.0.1:8081;
145 listen 127.0.0.1:8082 ssl;
146 server_name localhost;
147
148 ssl_certificate localhost.crt;
149 ssl_certificate_key localhost.key;
150
151 return 200 "uri:$uri\n";
152 }
153 }
154
155 EOF
156
157 $t->write_file('openssl.conf', <<EOF);
158 [ req ]
159 default_bits = 2048
160 encrypt_key = no
161 distinguished_name = req_distinguished_name
162 [ req_distinguished_name ]
163 EOF
164
165 my $d = $t->testdir();
166
167 foreach my $name ('localhost') {
168 system('openssl req -x509 -new '
169 . "-config '$d/openssl.conf' -subj '/CN=$name/' "
170 . "-out '$d/$name.crt' -keyout '$d/$name.key' "
171 . ">>$d/openssl.out 2>&1") == 0
172 or die "Can't create certificate for $name: $!\n";
173 }
174
175 $t->run();
176
177 ###############################################################################
178
179 like(http_get('/'), qr!uri:/$!, 'proxy request');
180
181 like(http_get('/proxy-pass-uri'), qr!uri:/replacement$!,
182 'proxy_pass uri changed');
183
184 TODO: {
185 local $TODO = 'not yet';
186
187 # due to missing information about an original location where
188 # proxy_pass was specified, this used to pass request with
189 # original unmodified uri
190
191 like(http_get('/proxy-pass-uri?if=1'), qr!uri:/replacement$!,
192 'proxy_pass uri changed in if');
193
194 }
195
196 like(http_get('/proxy-pass-uri/inner'), qr!404 Not Found!,
197 'proxy_pass uri changed inner');
198 like(http_get('/proxy-pass-uri/inner?if=1'), qr!404 Not Found!,
199 'proxy_pass uri changed inner in if');
200
201 # limit_except
202
203 like(http_get('/proxy-pass-uri-lmt'), qr!uri:/replacement$!,
204 'proxy_pass uri and limit_except');
205
206 TODO: {
207 local $TODO = 'not yet';
208
209 # special handling of limit_except results in wrong handling
210 # of requests in nested locations
211
212 like(http_get('/proxy-pass-uri-lmt/inner'), qr!404 Not Found!,
213 'proxy_pass uri and limit_except, inner');
214
215 }
216
217 like(http_get('/proxy-pass-uri-lmt-different'),
218 qr!uri:/proxy-pass-uri-lmt-different!,
219 'proxy_pass and limit_except with different proxy_pass');
220
221 # segmentation fault in old versions,
222 # fixed to return 500 Internal Error in nginx 1.3.10
223
224 like(http_get('/proxy-inside-if-crash'), qr!500 Internal Server Error!,
225 'proxy_pass inside if');
226
227 # normal proxy_pass and proxy_pass with variables
228 # use distinct field, and inheritance should be mutually
229 # exclusive, see ticket #537
230
231 like(http_get('/variables'), qr!uri:/outer!,
232 'proxy_pass variables');
233
234 TODO: {
235 local $TODO = 'not yet';
236
237 like(http_get('/variables?if=1'), qr!uri:/variables!,
238 'proxy_pass variables if');
239 like(http_get('/variables/inner'), qr!uri:/variables/inner!,
240 'proxy_pass variables nested');
241
242 }
243
244 # ssl context shouldn't be inherited into nested
245 # locations with different proxy_pass, but should
246 # be correctly inherited into if's
247
248 like(http_get('/ssl'), qr!uri:/outer!,
249 'proxy_pass ssl');
250 like(http_get('/ssl?if=1'), qr!uri:/outer!,
251 'proxy_pass ssl inside if');
252
253 TODO: {
254 local $TODO = 'not yet';
255
256 like(http_get('/ssl/inner'), qr!uri:/ssl/inner!,
257 'proxy_pass nossl inside ssl');
258
259 }
260
261 ###############################################################################