comparison proxy_redirect.t @ 186:1613a63b5100

Tests: added proxy_redirect tests.
author Valentin Bartenev <ne@vbart.ru>
date Wed, 30 Nov 2011 17:02:45 +0300
parents
children 50063559d85a
comparison
equal deleted inserted replaced
185:43fe964de06a 186:1613a63b5100
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4 # (C) Valentin Bartenev
5
6 # Tests for the proxy_redirect directive.
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/)->plan(6);
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 set $some_var var_here;
45
46 proxy_pass http://127.0.0.1:8081;
47
48 proxy_redirect http://127.0.0.1:8081/var_in_second/ /$some_var/;
49 proxy_redirect http://127.0.0.1:8081/$some_var/ /replaced/;
50
51 proxy_redirect ~^(.+)81/regex_w_([^/]+) $180/$2/test.html;
52 proxy_redirect ~*re+gexp? /replaced/test.html;
53 }
54
55 location /expl_default/ {
56 proxy_pass http://127.0.0.1:8081/replace_this/;
57 proxy_redirect wrong wrong;
58 proxy_redirect default;
59 }
60
61 location /impl_default/ {
62 proxy_pass http://127.0.0.1:8081/replace_this/;
63 }
64 }
65
66 server {
67 listen 127.0.0.1:8081;
68 server_name localhost;
69
70 location / {
71 return http://127.0.0.1:8081$uri;
72 }
73 }
74 }
75
76 EOF
77
78 $t->run();
79
80 ###############################################################################
81
82
83 is(http_get_location('http://127.0.0.1:8080/impl_default/test.html'),
84 'http://127.0.0.1:8080/impl_default/test.html', 'implicit default');
85 is(http_get_location('http://127.0.0.1:8080/expl_default/test.html'),
86 'http://127.0.0.1:8080/expl_default/test.html', 'explicit default');
87
88 is(http_get_location('http://127.0.0.1:8080/var_in_second/test.html'),
89 'http://127.0.0.1:8080/var_here/test.html', 'variable in second arg');
90
91 TODO:{
92 local $TODO = 'support variables in first argument';
93
94 is(http_get_location('http://127.0.0.1:8080/var_here/test.html'),
95 'http://127.0.0.1:8080/replaced/test.html', 'variable in first arg');
96
97 }
98
99 TODO:{
100 local $TODO = 'support for regular expressions';
101
102 is(http_get_location('http://127.0.0.1:8080/ReeegEX/test.html'),
103 'http://127.0.0.1:8080/replaced/test.html', 'caseless regexp');
104 is(http_get_location('http://127.0.0.1:8080/regex_w_captures/test.html'),
105 'http://127.0.0.1:8080/captures/test.html', 'regexp w/captures');
106
107 }
108
109
110 ###############################################################################
111
112 sub http_get_location {
113 my ($url) = @_;
114 http_get($url) =~ /^Location:\s(.+?)\x0d?$/mi;
115 return $1;
116 }