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