switch from eval/require to Test::Needs
[catagits/Catalyst-Authentication-Credential-HTTP.git] / t / live_app_digest.t
CommitLineData
007935b8 1use strict;
2use warnings;
de3a252c 3use FindBin qw/$Bin/;
4use lib "$Bin/lib";
007935b8 5use Test::More;
419d9af2 6use Test::Needs {
7 'Test::WWW::Mechanize::Catalyst' => '0.51',
8 'Catalyst::Plugin::Cache' => '0',
9 'Cache::FileCache' => undef,
10};
11
12plan tests => 12;
13
c5a1fa88 14use Digest::MD5;
007935b8 15use HTTP::Request;
861c2f44 16use Test::More;
17use Test::WWW::Mechanize::Catalyst qw/AuthDigestTestApp/;
a14203f8 18
c5a1fa88 19sub do_test {
20 my $username = shift;
2dad9ca6 21 my $uri = shift;
c5a1fa88 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';
2dad9ca6 43 $uri ||= '/moose';
c5a1fa88 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 }
2dad9ca6 57 my $r = HTTP::Request->new( GET => "http://localhost" . $uri );
c5a1fa88 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" );
007935b8 63}
513d8ab6 64
c5a1fa88 65do_test('Mufasa');
2dad9ca6 66do_test('Mufasa2');
67do_test('Mufasa', '/moose?moose_id=1'); # Digest auth includes the full URL path, so need to test query strings