X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Flive_app_digest.t;h=32afff684264d1a0d109392d9e550746e9665b6b;hb=c5a1fa888d417417060fa21bd72f5d862cb60513;hp=79c46e12eb70837c9bf01774d923042fb8168b3f;hpb=f1f73b5393230beeb7ee969a4b56974c8edc6281;p=catagits%2FCatalyst-Authentication-Credential-HTTP.git diff --git a/t/live_app_digest.t b/t/live_app_digest.t index 79c46e1..32afff6 100644 --- a/t/live_app_digest.t +++ b/t/live_app_digest.t @@ -12,8 +12,9 @@ BEGIN { eval { require Cache::FileCache } or plan skip_all => "Cache::FileCache is needed for this test"; - plan tests => 4; + plan tests => 8; } +use Digest::MD5; use HTTP::Request; { package AuthTestApp; @@ -29,7 +30,12 @@ use HTTP::Request; $c->authenticate(); $c->res->body( $c->user->id ); } - %users = ( Mufasa => { pass => "Circle Of Life", }, ); + my $digest_pass = Digest::MD5->new; + $digest_pass->add('Mufasa2:testrealm@host.com:Circle Of Life'); + %users = ( + Mufasa => { pass => "Circle Of Life", }, + Mufasa2 => { pass => $digest_pass->hexdigest, }, + ); __PACKAGE__->config->{cache}{backend} = { class => 'Cache::FileCache', }; @@ -53,46 +59,51 @@ use HTTP::Request; __PACKAGE__->setup; } use Test::WWW::Mechanize::Catalyst qw/AuthTestApp/; -my $mech = Test::WWW::Mechanize::Catalyst->new; -$mech->get("http://localhost/moose"); -is( $mech->status, 401, "status is 401" ); -my $www_auth = $mech->res->headers->header('WWW-Authenticate'); -my %www_auth_params = map { - my @key_val = split /=/, $_, 2; - $key_val[0] = lc $key_val[0]; - $key_val[1] =~ s{"}{}g; # remove the quotes - @key_val; -} split /, /, substr( $www_auth, 7 ); #7 == length "Digest " -$mech->content_lacks( "foo", "no output" ); -my $response = ''; -{ - my $username = 'Mufasa'; - my $password = 'Circle Of Life'; - my $realm = $www_auth_params{realm}; - my $nonce = $www_auth_params{nonce}; - my $cnonce = '0a4f113b'; - my $opaque = $www_auth_params{opaque}; - my $nc = '00000001'; - my $method = 'GET'; - my $qop = 'auth'; - my $uri = '/moose'; - my $ctx = Digest::MD5->new; - $ctx->add( join( ':', $username, $realm, $password ) ); - my $A1_digest = $ctx->hexdigest; - $ctx = Digest::MD5->new; - $ctx->add( join( ':', $method, $uri ) ); - my $A2_digest = $ctx->hexdigest; - my $digest = Digest::MD5::md5_hex( - join( ':', - $A1_digest, $nonce, $qop ? ( $nc, $cnonce, $qop ) : (), $A2_digest ) - ); - $response = qq{Digest username="$username", realm="$realm", nonce="$nonce", uri="$uri", qop=$qop, nc=$nc, cnonce="$cnonce", response="$digest", opaque="$opaque"}; +sub do_test { + my $username = shift; + my $mech = Test::WWW::Mechanize::Catalyst->new; + $mech->get("http://localhost/moose"); + is( $mech->status, 401, "status is 401" ); + my $www_auth = $mech->res->headers->header('WWW-Authenticate'); + my %www_auth_params = map { + my @key_val = split /=/, $_, 2; + $key_val[0] = lc $key_val[0]; + $key_val[1] =~ s{"}{}g; # remove the quotes + @key_val; + } split /, /, substr( $www_auth, 7 ); #7 == length "Digest " + $mech->content_lacks( "foo", "no output" ); + my $response = ''; + { + my $password = 'Circle Of Life'; + my $realm = $www_auth_params{realm}; + my $nonce = $www_auth_params{nonce}; + my $cnonce = '0a4f113b'; + my $opaque = $www_auth_params{opaque}; + my $nc = '00000001'; + my $method = 'GET'; + my $qop = 'auth'; + my $uri = '/moose'; + my $ctx = Digest::MD5->new; + $ctx->add( join( ':', $username, $realm, $password ) ); + my $A1_digest = $ctx->hexdigest; + $ctx = Digest::MD5->new; + $ctx->add( join( ':', $method, $uri ) ); + my $A2_digest = $ctx->hexdigest; + my $digest = Digest::MD5::md5_hex( + join( ':', + $A1_digest, $nonce, $qop ? ( $nc, $cnonce, $qop ) : (), $A2_digest ) + ); + + $response = qq{Digest username="$username", realm="$realm", nonce="$nonce", uri="$uri", qop=$qop, nc=$nc, cnonce="$cnonce", response="$digest", opaque="$opaque"}; + } + my $r = HTTP::Request->new( GET => "http://localhost/moose" ); + $mech->request($r); + $r->headers->push_header( Authorization => $response ); + $mech->request($r); + is( $mech->status, 200, "status is 200" ); + $mech->content_contains( $username, "Mufasa output" ); } -my $r = HTTP::Request->new( GET => "http://localhost/moose" ); -$mech->request($r); -$r->headers->push_header( Authorization => $response ); -$mech->request($r); -is( $mech->status, 200, "status is 200" ); -$mech->content_contains( "Mufasa", "Mufasa output" ); +do_test('Mufasa'); +do_test('Mufasa2'); \ No newline at end of file