comparison auth_request_set.t @ 322:67c348ba1768

Tests: auth request tests import.
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 21 Aug 2013 19:22:06 +0400
parents
children d48de852157c
comparison
equal deleted inserted replaced
321:f98e8674361b 322:67c348ba1768
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
16 BEGIN { use FindBin; chdir($FindBin::Bin); }
17
18 use lib 'lib';
19 use Test::Nginx;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 my $t = Test::Nginx->new()->has(qw/http rewrite auth_request/)
27 ->plan(6);
28
29 $t->write_file_expand('nginx.conf', <<'EOF');
30
31 %%TEST_GLOBALS%%
32
33 master_process off;
34 daemon off;
35
36 events {
37 }
38
39 http {
40 %%TEST_GLOBALS_HTTP%%
41
42 server {
43 listen 127.0.0.1:8080;
44 server_name localhost;
45
46 location = /t1.html {
47 auth_request /auth;
48 auth_request_set $username $upstream_http_x_username;
49 add_header X-Set-Username $username;
50 }
51
52 location = /t2.html {
53 auth_request /auth;
54 auth_request_set $username $upstream_http_x_username;
55 error_page 404 = /fallback;
56 }
57 location = /fallback {
58 add_header X-Set-Username $username;
59 return 204;
60 }
61
62 location = /t3.html {
63 auth_request /auth;
64 auth_request_set $username $upstream_http_x_username;
65 error_page 404 = @fallback;
66 }
67 location @fallback {
68 add_header X-Set-Username $username;
69 return 204;
70 }
71
72 location = /t4.html {
73 auth_request /auth;
74 auth_request_set $username $upstream_http_x_username;
75 error_page 404 = /t4-fallback.html;
76 }
77 location = /t4-fallback.html {
78 auth_request /auth2;
79 auth_request_set $username $upstream_http_x_username;
80 add_header X-Set-Username $username;
81 }
82
83 location = /t5.html {
84 auth_request /auth;
85 auth_request_set $args "setargs";
86 proxy_pass http://127.0.0.1:8081/t5.html;
87 }
88
89 location = /t6.html {
90 add_header X-Unset-Username "x${username}x";
91 return 204;
92 }
93
94 location = /auth {
95 proxy_pass http://127.0.0.1:8081;
96 }
97 location = /auth2 {
98 proxy_pass http://127.0.0.1:8081;
99 }
100 }
101
102 server {
103 listen 127.0.0.1:8081;
104 server_name localhost;
105
106 location = /auth {
107 add_header X-Username "username";
108 return 204;
109 }
110
111 location = /auth2 {
112 add_header X-Username "username2";
113 return 204;
114 }
115
116 location = /t5.html {
117 add_header X-Args $args;
118 return 204;
119 }
120 }
121 }
122
123 EOF
124
125 $t->write_file('t1.html', '');
126 $t->write_file('t4-fallback.html', '');
127 $t->run();
128
129 ###############################################################################
130
131 like(http_get('/t1.html'), qr/X-Set-Username: username/, 'set normal');
132 like(http_get('/t2.html'), qr/X-Set-Username: username/, 'set after redirect');
133 like(http_get('/t3.html'), qr/X-Set-Username: username/,
134 'set after named location');
135 like(http_get('/t4.html'), qr/X-Set-Username: username2/,
136 'set on second auth');
137
138 # there are two variables with set_handler: $args and $limit_rate
139 # we do test $args as it's a bit more simple thing to do
140
141 like(http_get('/t5.html'), qr/X-Args: setargs/, 'variable with set_handler');
142
143 # check that using variable without setting it returns empty content
144
145 like(http_get('/t6.html'), qr/X-Unset-Username: xx/, 'unset variable');
146
147 ###############################################################################