Move actions out of test app
[catagits/Catalyst-Authentication-Credential-HTTP.git] / t / live_app_digest.t
CommitLineData
007935b8 1#!/usr/bin/perl
007935b8 2use strict;
3use warnings;
de3a252c 4use FindBin qw/$Bin/;
5use lib "$Bin/lib";
007935b8 6use Test::More;
007935b8 7BEGIN {
8 eval { require Test::WWW::Mechanize::Catalyst }
9 or plan skip_all =>
10 "Test::WWW::Mechanize::Catalyst is needed for this test";
5b51e987 11 eval { require Catalyst::Plugin::Cache }
007935b8 12 or plan skip_all =>
5b51e987 13 "Catalyst::Plugin::Cache is needed for this test";
513d8ab6 14 eval { require Cache::FileCache }
5b51e987 15 or plan skip_all =>
16 "Cache::FileCache is needed for this test";
2dad9ca6 17 plan tests => 12;
007935b8 18}
c5a1fa88 19use Digest::MD5;
007935b8 20use HTTP::Request;
861c2f44 21use Test::More;
22use Test::WWW::Mechanize::Catalyst qw/AuthDigestTestApp/;
a14203f8 23
c5a1fa88 24sub do_test {
25 my $username = shift;
2dad9ca6 26 my $uri = shift;
c5a1fa88 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';
2dad9ca6 48 $uri ||= '/moose';
c5a1fa88 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 }
2dad9ca6 62 my $r = HTTP::Request->new( GET => "http://localhost" . $uri );
c5a1fa88 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" );
007935b8 68}
513d8ab6 69
c5a1fa88 70do_test('Mufasa');
2dad9ca6 71do_test('Mufasa2');
72do_test('Mufasa', '/moose?moose_id=1'); # Digest auth includes the full URL path, so need to test query strings