comparison t/auth-request-set.t @ 8:b77577b9c005

Auth request: auth_request_set tests.
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 24 Mar 2010 07:02:27 +0300
parents
children
comparison
equal deleted inserted replaced
7:fb05a061532c 8:b77577b9c005
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4
5 # Tests for auth request module, auth_request_set.
6
7 ###############################################################################
8
9 use warnings;
10 use strict;
11
12 use Socket qw/ CRLF /;
13
14 use Test::More;
15 use Test::Nginx;
16
17 ###############################################################################
18
19 select STDERR; $| = 1;
20 select STDOUT; $| = 1;
21
22 my $t = Test::Nginx->new()->has(qw/http rewrite/)
23 ->plan(6);
24
25 $t->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.html {
43 auth_request /auth;
44 auth_request_set $username $upstream_http_x_username;
45 add_header X-Set-Username $username;
46 }
47
48 location = /t2.html {
49 auth_request /auth;
50 auth_request_set $username $upstream_http_x_username;
51 error_page 404 = /fallback;
52 }
53 location = /fallback {
54 add_header X-Set-Username $username;
55 return 204;
56 }
57
58 location = /t3.html {
59 auth_request /auth;
60 auth_request_set $username $upstream_http_x_username;
61 error_page 404 = @fallback;
62 }
63 location @fallback {
64 add_header X-Set-Username $username;
65 return 204;
66 }
67
68 location = /t4.html {
69 auth_request /auth;
70 auth_request_set $username $upstream_http_x_username;
71 error_page 404 = /t4-fallback.html;
72 }
73 location = /t4-fallback.html {
74 auth_request /auth2;
75 auth_request_set $username $upstream_http_x_username;
76 add_header X-Set-Username $username;
77 }
78
79 location = /t5.html {
80 auth_request /auth;
81 auth_request_set $args "setargs";
82 proxy_pass http://127.0.0.1:8081/t5.html;
83 }
84
85 location = /t6.html {
86 add_header X-Unset-Username "x${username}x";
87 return 204;
88 }
89
90 location = /auth {
91 proxy_pass http://127.0.0.1:8081;
92 }
93 location = /auth2 {
94 proxy_pass http://127.0.0.1:8081;
95 }
96 }
97
98 server {
99 listen 127.0.0.1:8081;
100 server_name localhost;
101
102 location = /auth {
103 add_header X-Username "username";
104 return 204;
105 }
106
107 location = /auth2 {
108 add_header X-Username "username2";
109 return 204;
110 }
111
112 location = /t5.html {
113 add_header X-Args $args;
114 return 204;
115 }
116 }
117 }
118
119 EOF
120
121 $t->write_file('t1.html', '');
122 $t->write_file('t4-fallback.html', '');
123 $t->run();
124
125 ###############################################################################
126
127 like(http_get('/t1.html'), qr/X-Set-Username: username/, 'set normal');
128 like(http_get('/t2.html'), qr/X-Set-Username: username/, 'set after redirect');
129 like(http_get('/t3.html'), qr/X-Set-Username: username/,
130 'set after named location');
131 like(http_get('/t4.html'), qr/X-Set-Username: username2/,
132 'set on second auth');
133
134 # there are two variables with set_handler: $args and $limit_rate
135 # we do test $args as it's a bit more simple thing to do
136
137 like(http_get('/t5.html'), qr/X-Args: setargs/, 'variable with set_handler');
138
139 # check that using variable without setting it returns empty content
140
141 like(http_get('/t6.html'), qr/X-Unset-Username: xx/, 'unset variable');
142
143 ###############################################################################