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