Mercurial > hg > ngx_http_auth_request_module
annotate t/auth-request.t @ 5:cc231fa95159
Auth request: expand example in README.
1. An example how to don't pass to upstream server body (if happend to be
already read from client) and Content-Length header to handle POSTs
correctly.
2. An example how to pass original request URI to backend.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 05 Mar 2010 21:20:32 +0300 |
parents | 35f0ee7a3c28 |
children | 70f3d876b569 |
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 | |
4
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
12 use Socket qw/ CRLF /; |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
13 |
0 | 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 proxy fastcgi auth_basic/) | |
4
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
23 ->plan(17); |
0 | 24 |
25 $t->write_file_expand('nginx.conf', <<'EOF'); | |
26 | |
27 master_process off; | |
28 daemon off; | |
29 | |
30 events { | |
31 } | |
32 | |
33 http { | |
34 %%TEST_GLOBALS_HTTP%% | |
35 | |
36 server { | |
37 listen 127.0.0.1:8080; | |
38 server_name localhost; | |
39 | |
40 location / { | |
41 return 444; | |
42 } | |
43 | |
44 location /open { | |
45 auth_request /auth-open; | |
46 } | |
47 location = /auth-open { | |
48 return 204; | |
49 } | |
50 | |
51 location /open-static { | |
52 auth_request /auth-open-static; | |
53 } | |
54 location = /auth-open-static { | |
55 # nothing, use static file | |
56 } | |
57 | |
58 location /unauthorized { | |
59 auth_request /auth-unauthorized; | |
60 } | |
61 location = /auth-unauthorized { | |
62 return 401; | |
63 } | |
64 | |
65 location /forbidden { | |
66 auth_request /auth-forbidden; | |
67 } | |
68 location = /auth-forbidden { | |
69 return 403; | |
70 } | |
71 | |
72 location /error { | |
73 auth_request /auth-error; | |
74 } | |
75 location = /auth-error { | |
76 return 404; | |
77 } | |
78 | |
1
dfc5ae42367a
Auth request: support switching off.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
79 location /off { |
dfc5ae42367a
Auth request: support switching off.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
80 auth_request off; |
dfc5ae42367a
Auth request: support switching off.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
81 } |
dfc5ae42367a
Auth request: support switching off.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
82 |
0 | 83 location /proxy { |
84 auth_request /auth-proxy; | |
85 } | |
86 location = /auth-proxy { | |
87 proxy_pass http://127.0.0.1:8080/auth-basic; | |
4
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
88 proxy_pass_request_body off; |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
89 proxy_set_header Content-Length ""; |
0 | 90 } |
91 location = /auth-basic { | |
92 auth_basic "restricted"; | |
93 auth_basic_user_file %%TESTDIR%%/htpasswd; | |
94 } | |
95 | |
96 location /fastcgi { | |
97 auth_request /auth-fastcgi; | |
98 } | |
99 location = /auth-fastcgi { | |
100 fastcgi_pass 127.0.0.1:8081; | |
4
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
101 fastcgi_pass_request_body off; |
0 | 102 } |
103 } | |
104 } | |
105 | |
106 EOF | |
107 | |
108 $t->write_file('htpasswd', 'user:zz1T8N4tWvmbE' . "\n"); | |
109 $t->write_file('auth-basic', 'INVISIBLE'); | |
110 $t->write_file('auth-open-static', 'INVISIBLE'); | |
111 $t->run(); | |
112 | |
113 ############################################################################### | |
114 | |
115 pass('runs'); | |
116 | |
117 like(http_get('/open'), qr/ 404 /, 'auth open'); | |
118 like(http_get('/unauthorized'), qr/ 401 /, 'auth unauthorized'); | |
119 like(http_get('/forbidden'), qr/ 403 /, 'auth forbidden'); | |
120 like(http_get('/error'), qr/ 500 /, 'auth error'); | |
1
dfc5ae42367a
Auth request: support switching off.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
121 like(http_get('/off'), qr/ 404 /, 'auth off'); |
0 | 122 |
4
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
123 like(http_post('/open'), qr/ 404 /, 'auth post open'); |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
124 like(http_post('/unauthorized'), qr/ 401 /, 'auth post unauthorized'); |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
125 |
0 | 126 like(http_get('/open-static'), qr/ 404 /, 'auth open static'); |
127 unlike(http_get('/open-static'), qr/INVISIBLE/, 'auth static no content'); | |
128 | |
129 like(http_get('/proxy'), qr/ 401 /, 'proxy auth unauthorized'); | |
130 like(http_get('/proxy'), qr/WWW-Authenticate: Basic realm="restricted"/, | |
131 'proxy auth has www-authenticate'); | |
132 like(http_get_auth('/proxy'), qr/ 404 /, 'proxy auth pass'); | |
133 unlike(http_get_auth('/proxy'), qr/INVISIBLE/, 'proxy auth no content'); | |
134 | |
4
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
135 like(http_post('/proxy'), qr/ 401 /, 'proxy auth post'); |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
136 |
0 | 137 SKIP: { |
138 eval { require FCGI; }; | |
139 skip 'FCGI not installed', 2 if $@; | |
140 | |
141 $t->run_daemon(\&fastcgi_daemon); | |
142 $t->waitforsocket('127.0.0.1:8081'); | |
143 | |
144 like(http_get('/fastcgi'), qr/ 404 /, 'fastcgi auth open'); | |
145 unlike(http_get('/fastcgi'), qr/INVISIBLE/, 'fastcgi auth no content'); | |
146 } | |
147 | |
148 ############################################################################### | |
149 | |
150 sub http_get_auth { | |
151 my ($url, %extra) = @_; | |
152 return http(<<EOF, %extra); | |
153 GET $url HTTP/1.0 | |
154 Host: localhost | |
155 Authorization: Basic dXNlcjpzZWNyZXQ= | |
156 | |
157 EOF | |
158 } | |
159 | |
4
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
160 sub http_post { |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
161 my ($url, %extra) = @_; |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
162 |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
163 my $p = "POST $url HTTP/1.0" . CRLF . |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
164 "Host: localhost" . CRLF . |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
165 "Content-Length: 10" . CRLF . |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
166 CRLF . |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
167 "1234567890"; |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
168 |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
169 return http($p, %extra); |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
170 } |
35f0ee7a3c28
Auth request: fix SIGSEGV on POST.
Maxim Dounin <mdounin@mdounin.ru>
parents:
1
diff
changeset
|
171 |
0 | 172 ############################################################################### |
173 | |
174 sub fastcgi_daemon { | |
175 my $socket = FCGI::OpenSocket('127.0.0.1:8081', 5); | |
176 my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, | |
177 $socket); | |
178 | |
179 while ($request->Accept() >= 0) { | |
180 print <<EOF; | |
181 Content-Type: text/html | |
182 | |
183 INVISIBLE | |
184 EOF | |
185 } | |
186 | |
187 FCGI::CloseSocket($socket); | |
188 } | |
189 | |
190 ############################################################################### |