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