Mercurial > hg > ngx_http_auth_request_module
annotate t/auth-request.t @ 3:666c7bf3e009
Added tag 0.1 for changeset 187ac993cd15
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Sat, 27 Feb 2010 21:25:18 +0300 |
parents | dfc5ae42367a |
children | 35f0ee7a3c28 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Tests for auth request module. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 use Test::Nginx; | |
14 | |
15 ############################################################################### | |
16 | |
17 select STDERR; $| = 1; | |
18 select STDOUT; $| = 1; | |
19 | |
20 my $t = Test::Nginx->new()->has(qw/http rewrite proxy fastcgi auth_basic/) | |
1
dfc5ae42367a
Auth request: support switching off.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
21 ->plan(14); |
0 | 22 |
23 $t->write_file_expand('nginx.conf', <<'EOF'); | |
24 | |
25 master_process off; | |
26 daemon off; | |
27 | |
28 events { | |
29 } | |
30 | |
31 http { | |
32 %%TEST_GLOBALS_HTTP%% | |
33 | |
34 server { | |
35 listen 127.0.0.1:8080; | |
36 server_name localhost; | |
37 | |
38 location / { | |
39 return 444; | |
40 } | |
41 | |
42 location /open { | |
43 auth_request /auth-open; | |
44 } | |
45 location = /auth-open { | |
46 return 204; | |
47 } | |
48 | |
49 location /open-static { | |
50 auth_request /auth-open-static; | |
51 } | |
52 location = /auth-open-static { | |
53 # nothing, use static file | |
54 } | |
55 | |
56 location /unauthorized { | |
57 auth_request /auth-unauthorized; | |
58 } | |
59 location = /auth-unauthorized { | |
60 return 401; | |
61 } | |
62 | |
63 location /forbidden { | |
64 auth_request /auth-forbidden; | |
65 } | |
66 location = /auth-forbidden { | |
67 return 403; | |
68 } | |
69 | |
70 location /error { | |
71 auth_request /auth-error; | |
72 } | |
73 location = /auth-error { | |
74 return 404; | |
75 } | |
76 | |
1
dfc5ae42367a
Auth request: support switching off.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
77 location /off { |
dfc5ae42367a
Auth request: support switching off.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
78 auth_request off; |
dfc5ae42367a
Auth request: support switching off.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
79 } |
dfc5ae42367a
Auth request: support switching off.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
80 |
0 | 81 location /proxy { |
82 auth_request /auth-proxy; | |
83 } | |
84 location = /auth-proxy { | |
85 proxy_pass http://127.0.0.1:8080/auth-basic; | |
86 } | |
87 location = /auth-basic { | |
88 auth_basic "restricted"; | |
89 auth_basic_user_file %%TESTDIR%%/htpasswd; | |
90 } | |
91 | |
92 location /fastcgi { | |
93 auth_request /auth-fastcgi; | |
94 } | |
95 location = /auth-fastcgi { | |
96 fastcgi_pass 127.0.0.1:8081; | |
97 } | |
98 } | |
99 } | |
100 | |
101 EOF | |
102 | |
103 $t->write_file('htpasswd', 'user:zz1T8N4tWvmbE' . "\n"); | |
104 $t->write_file('auth-basic', 'INVISIBLE'); | |
105 $t->write_file('auth-open-static', 'INVISIBLE'); | |
106 $t->run(); | |
107 | |
108 ############################################################################### | |
109 | |
110 pass('runs'); | |
111 | |
112 like(http_get('/open'), qr/ 404 /, 'auth open'); | |
113 like(http_get('/unauthorized'), qr/ 401 /, 'auth unauthorized'); | |
114 like(http_get('/forbidden'), qr/ 403 /, 'auth forbidden'); | |
115 like(http_get('/error'), qr/ 500 /, 'auth error'); | |
1
dfc5ae42367a
Auth request: support switching off.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
116 like(http_get('/off'), qr/ 404 /, 'auth off'); |
0 | 117 |
118 like(http_get('/open-static'), qr/ 404 /, 'auth open static'); | |
119 unlike(http_get('/open-static'), qr/INVISIBLE/, 'auth static no content'); | |
120 | |
121 like(http_get('/proxy'), qr/ 401 /, 'proxy auth unauthorized'); | |
122 like(http_get('/proxy'), qr/WWW-Authenticate: Basic realm="restricted"/, | |
123 'proxy auth has www-authenticate'); | |
124 like(http_get_auth('/proxy'), qr/ 404 /, 'proxy auth pass'); | |
125 unlike(http_get_auth('/proxy'), qr/INVISIBLE/, 'proxy auth no content'); | |
126 | |
127 SKIP: { | |
128 eval { require FCGI; }; | |
129 skip 'FCGI not installed', 2 if $@; | |
130 | |
131 $t->run_daemon(\&fastcgi_daemon); | |
132 $t->waitforsocket('127.0.0.1:8081'); | |
133 | |
134 like(http_get('/fastcgi'), qr/ 404 /, 'fastcgi auth open'); | |
135 unlike(http_get('/fastcgi'), qr/INVISIBLE/, 'fastcgi auth no content'); | |
136 } | |
137 | |
138 ############################################################################### | |
139 | |
140 sub http_get_auth { | |
141 my ($url, %extra) = @_; | |
142 return http(<<EOF, %extra); | |
143 GET $url HTTP/1.0 | |
144 Host: localhost | |
145 Authorization: Basic dXNlcjpzZWNyZXQ= | |
146 | |
147 EOF | |
148 } | |
149 | |
150 ############################################################################### | |
151 | |
152 sub fastcgi_daemon { | |
153 my $socket = FCGI::OpenSocket('127.0.0.1:8081', 5); | |
154 my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, | |
155 $socket); | |
156 | |
157 while ($request->Accept() >= 0) { | |
158 print <<EOF; | |
159 Content-Type: text/html | |
160 | |
161 INVISIBLE | |
162 EOF | |
163 } | |
164 | |
165 FCGI::CloseSocket($socket); | |
166 } | |
167 | |
168 ############################################################################### |