switch from eval/require to Test::Needs
[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 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
14 use Digest::MD5;
15 use HTTP::Request;
16 use Test::More;
17 use Test::WWW::Mechanize::Catalyst qw/AuthDigestTestApp/;
18
19 sub do_test {
20     my $username = shift;
21     my $uri = shift;
22     my $mech = Test::WWW::Mechanize::Catalyst->new;
23     $mech->get("http://localhost/moose");
24     is( $mech->status, 401, "status is 401" );
25     my $www_auth = $mech->res->headers->header('WWW-Authenticate');
26     my %www_auth_params = map {
27         my @key_val = split /=/, $_, 2;
28         $key_val[0] = lc $key_val[0];
29         $key_val[1] =~ s{"}{}g;    # remove the quotes
30         @key_val;
31     } split /, /, substr( $www_auth, 7 );    #7 == length "Digest "
32     $mech->content_lacks( "foo", "no output" );
33     my $response = '';
34     {
35         my $password = 'Circle Of Life';
36         my $realm    = $www_auth_params{realm};
37         my $nonce    = $www_auth_params{nonce};
38         my $cnonce   = '0a4f113b';
39         my $opaque   = $www_auth_params{opaque};
40         my $nc       = '00000001';
41         my $method   = 'GET';
42         my $qop      = 'auth';
43         $uri         ||= '/moose';
44         my $ctx = Digest::MD5->new;
45         $ctx->add( join( ':', $username, $realm, $password ) );
46         my $A1_digest = $ctx->hexdigest;
47         $ctx = Digest::MD5->new;
48         $ctx->add( join( ':', $method, $uri ) );
49         my $A2_digest = $ctx->hexdigest;
50         my $digest = Digest::MD5::md5_hex(
51             join( ':',
52                 $A1_digest, $nonce, $qop ? ( $nc, $cnonce, $qop ) : (), $A2_digest )
53         );
54
55         $response = qq{Digest username="$username", realm="$realm", nonce="$nonce", uri="$uri", qop=$qop, nc=$nc, cnonce="$cnonce", response="$digest", opaque="$opaque"};
56     }
57     my $r = HTTP::Request->new( GET => "http://localhost" . $uri );
58     $mech->request($r);
59     $r->headers->push_header( Authorization => $response );
60     $mech->request($r);
61     is( $mech->status, 200, "status is 200" );
62     $mech->content_contains( $username, "Mufasa output" );
63 }
64
65 do_test('Mufasa');
66 do_test('Mufasa2');
67 do_test('Mufasa', '/moose?moose_id=1'); # Digest auth includes the full URL path, so need to test query strings