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