Mercurial > hg > nginx-tests
comparison auth_basic.t @ 150:6b73d6c8f26c
Tests: add some auth basic module tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 08 Apr 2011 21:23:40 +0400 |
parents | |
children | c9be30f65be7 |
comparison
equal
deleted
inserted
replaced
149:2178954eee5d | 150:6b73d6c8f26c |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Tests for auth basic module. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 | |
14 use MIME::Base64; | |
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 auth_basic/)->plan(11) | |
27 ->write_file_expand('nginx.conf', <<'EOF'); | |
28 | |
29 %%TEST_GLOBALS%% | |
30 | |
31 master_process off; | |
32 daemon off; | |
33 | |
34 events { | |
35 } | |
36 | |
37 http { | |
38 %%TEST_GLOBALS_HTTP%% | |
39 | |
40 server { | |
41 listen 127.0.0.1:8080; | |
42 server_name localhost; | |
43 | |
44 location / { | |
45 auth_basic "closed site"; | |
46 auth_basic_user_file %%TESTDIR%%/htpasswd; | |
47 } | |
48 } | |
49 } | |
50 | |
51 EOF | |
52 | |
53 my $d = $t->testdir(); | |
54 | |
55 $t->write_file('index.html', 'SEETHIS'); | |
56 | |
57 $t->write_file( | |
58 'htpasswd', | |
59 'crypt:' . crypt('password', 'salt') . "\n" . | |
60 'crypt1:' . crypt('password', '$1$salt$') . "\n" . | |
61 'apr1:' . '$apr1$salt$Xxd1irWT9ycqoYxGFn4cb.' . "\n" . | |
62 'plain:' . '{PLAIN}password' . "\n" . | |
63 'ssha:' . '{SSHA}yI6cZwQadOA1e+/f+T+H3eCQQhRzYWx0' . "\n" | |
64 ); | |
65 | |
66 $t->run(); | |
67 | |
68 ############################################################################### | |
69 | |
70 like(http_get('/'), qr!401 Unauthorized!ms, 'rejects unathorized'); | |
71 | |
72 like(http_get_auth('/', 'crypt', 'password'), qr!SEETHIS!, 'normal crypt'); | |
73 unlike(http_get_auth('/', 'crypt', '123'), qr!SEETHIS!, 'normal wrong'); | |
74 | |
75 like(http_get_auth('/', 'crypt1', 'password'), qr!SEETHIS!, 'crypt $1$ (md5)'); | |
76 unlike(http_get_auth('/', 'crypt1', '123'), qr!SEETHIS!, 'crypt $1$ wrong'); | |
77 | |
78 TODO: { | |
79 local $TODO = 'not yet'; | |
80 | |
81 like(http_get_auth('/', 'apr1', 'password'), qr!SEETHIS!, 'apr1 md5'); | |
82 like(http_get_auth('/', 'plain', 'password'), qr!SEETHIS!, 'plain password'); | |
83 like(http_get_auth('/', 'ssha', 'password'), qr!SEETHIS!, 'ssha'); | |
84 } | |
85 | |
86 unlike(http_get_auth('/', 'apr1', '123'), qr!SEETHIS!, 'apr1 md5 wrong'); | |
87 unlike(http_get_auth('/', 'plain', '123'), qr!SEETHIS!, 'plain wrong'); | |
88 unlike(http_get_auth('/', 'ssha', '123'), qr!SEETHIS!, 'ssha wrong'); | |
89 | |
90 ############################################################################### | |
91 | |
92 sub http_get_auth { | |
93 my ($url, $user, $password) = @_; | |
94 | |
95 my $auth = encode_base64($user . ':' . $password); | |
96 | |
97 my $r = http(<<EOF); | |
98 GET $url HTTP/1.0 | |
99 Host: localhost | |
100 Authorization: Basic $auth | |
101 | |
102 EOF | |
103 } | |
104 | |
105 ############################################################################### |