Commit | Line | Data |
8c3623e2 |
1 | use strictures 1; |
2 | use Test::More; |
3 | |
4 | my $auth_result; |
5 | my @auth_args; |
6 | |
7 | { |
8 | package TestApp; |
9 | |
10 | use Web::Simple; |
11 | use Plack::Middleware::Auth::Basic; |
12 | |
13 | sub dispatch_request { |
14 | sub () { |
15 | Plack::Middleware::Auth::Basic->new( |
16 | authenticator => sub { |
17 | @auth_args = @_; return $auth_result |
18 | } |
19 | ) |
20 | }, |
21 | sub () { |
22 | [ 200, [ 'Content-type' => 'text/plain' ], [ 'Woo' ] ] |
23 | } |
24 | } |
25 | } |
26 | |
27 | my $ta = TestApp->new; |
28 | |
29 | my $res = $ta->run_test_request(GET => '/'); |
30 | |
31 | is($res->code, '401', 'Auth failed with no user/pass'); |
32 | ok(!@auth_args, 'Auth callback never called with no user/pass'); |
33 | |
34 | $res = $ta->run_test_request(GET => 'bob:secret@/'); |
35 | |
36 | is($res->code, '401', 'Auth failed with bad user/pass'); |
37 | is($auth_args[0], 'bob', 'Username passed ok'); |
38 | is($auth_args[1], 'secret', 'Password passed ok'); |
39 | |
40 | $auth_result = 1; |
41 | @auth_args = (); |
42 | |
43 | $res = $ta->run_test_request(GET => '/'); |
44 | |
45 | is($res->code, '401', 'Auth failed with no user/pass'); |
46 | ok(!@auth_args, 'Auth callback never called with no user/pass'); |
47 | |
48 | $res = $ta->run_test_request(GET => 'bob:secret@/'); |
49 | |
50 | is($res->code, '200', 'Auth succeeded with good user/pass'); |
51 | is($auth_args[0], 'bob', 'Username passed ok'); |
52 | is($auth_args[1], 'secret', 'Password passed ok'); |
53 | |
54 | done_testing; |