Commit | Line | Data |
007935b8 |
1 | use strict; |
2 | use warnings; |
de3a252c |
3 | use FindBin qw/$Bin/; |
4 | use lib "$Bin/lib"; |
007935b8 |
5 | use Test::More; |
419d9af2 |
6 | use Test::Needs { |
7 | 'Test::WWW::Mechanize::Catalyst' => '0.51', |
8 | 'Catalyst::Plugin::Cache' => '0', |
9 | 'Cache::FileCache' => undef, |
10 | }; |
11 | |
12 | plan tests => 12; |
13 | |
c5a1fa88 |
14 | use Digest::MD5; |
007935b8 |
15 | use HTTP::Request; |
36df1612 |
16 | Test::WWW::Mechanize::Catalyst->import(qw/AuthDigestTestApp/); |
a14203f8 |
17 | |
c5a1fa88 |
18 | sub do_test { |
19 | my $username = shift; |
2dad9ca6 |
20 | my $uri = shift; |
c5a1fa88 |
21 | my $mech = Test::WWW::Mechanize::Catalyst->new; |
22 | $mech->get("http://localhost/moose"); |
23 | is( $mech->status, 401, "status is 401" ); |
24 | my $www_auth = $mech->res->headers->header('WWW-Authenticate'); |
25 | my %www_auth_params = map { |
26 | my @key_val = split /=/, $_, 2; |
27 | $key_val[0] = lc $key_val[0]; |
28 | $key_val[1] =~ s{"}{}g; # remove the quotes |
29 | @key_val; |
30 | } split /, /, substr( $www_auth, 7 ); #7 == length "Digest " |
31 | $mech->content_lacks( "foo", "no output" ); |
32 | my $response = ''; |
33 | { |
34 | my $password = 'Circle Of Life'; |
35 | my $realm = $www_auth_params{realm}; |
36 | my $nonce = $www_auth_params{nonce}; |
37 | my $cnonce = '0a4f113b'; |
38 | my $opaque = $www_auth_params{opaque}; |
39 | my $nc = '00000001'; |
40 | my $method = 'GET'; |
41 | my $qop = 'auth'; |
2dad9ca6 |
42 | $uri ||= '/moose'; |
c5a1fa88 |
43 | my $ctx = Digest::MD5->new; |
44 | $ctx->add( join( ':', $username, $realm, $password ) ); |
45 | my $A1_digest = $ctx->hexdigest; |
46 | $ctx = Digest::MD5->new; |
47 | $ctx->add( join( ':', $method, $uri ) ); |
48 | my $A2_digest = $ctx->hexdigest; |
49 | my $digest = Digest::MD5::md5_hex( |
50 | join( ':', |
51 | $A1_digest, $nonce, $qop ? ( $nc, $cnonce, $qop ) : (), $A2_digest ) |
52 | ); |
53 | |
54 | $response = qq{Digest username="$username", realm="$realm", nonce="$nonce", uri="$uri", qop=$qop, nc=$nc, cnonce="$cnonce", response="$digest", opaque="$opaque"}; |
55 | } |
2dad9ca6 |
56 | my $r = HTTP::Request->new( GET => "http://localhost" . $uri ); |
c5a1fa88 |
57 | $mech->request($r); |
58 | $r->headers->push_header( Authorization => $response ); |
59 | $mech->request($r); |
60 | is( $mech->status, 200, "status is 200" ); |
61 | $mech->content_contains( $username, "Mufasa output" ); |
007935b8 |
62 | } |
513d8ab6 |
63 | |
c5a1fa88 |
64 | do_test('Mufasa'); |
2dad9ca6 |
65 | do_test('Mufasa2'); |
66 | do_test('Mufasa', '/moose?moose_id=1'); # Digest auth includes the full URL path, so need to test query strings |