9 eval { require Test::WWW::Mechanize::Catalyst }
11 Test::WWW::Mechanize::Catalyst->VERSION('0.51')
14 "Test::WWW::Mechanize::Catalyst is needed for this test";
15 eval { require Catalyst::Plugin::Cache }
17 "Catalyst::Plugin::Cache is needed for this test";
18 eval { require Cache::FileCache }
20 "Cache::FileCache is needed for this test";
26 use Test::WWW::Mechanize::Catalyst;
29 my ($username, $uri, $emulate_dotnet, $fail) = @_;
30 my $app = $fail ? 'AuthDigestTestApp' : 'AuthDigestDotnetTestApp';
31 my $mech = Test::WWW::Mechanize::Catalyst->new(catalyst_app => $app);
32 $mech->get("http://localhost/moose");
33 is( $mech->status, 401, "status is 401" );
34 my $www_auth = $mech->res->headers->header('WWW-Authenticate');
35 my %www_auth_params = map {
36 my @key_val = split /=/, $_, 2;
37 $key_val[0] = lc $key_val[0];
38 $key_val[1] =~ s{"}{}g; # remove the quotes
40 } split /, /, substr( $www_auth, 7 ); #7 == length "Digest "
41 $mech->content_lacks( "foo", "no output" );
44 my $password = 'Circle Of Life';
45 my $realm = $www_auth_params{realm};
46 my $nonce = $www_auth_params{nonce};
47 my $cnonce = '0a4f113b';
48 my $opaque = $www_auth_params{opaque};
54 if ($emulate_dotnet) {
55 $auth_uri =~ s/\?.*//;
57 my $ctx = Digest::MD5->new;
58 $ctx->add( join( ':', $username, $realm, $password ) );
59 my $A1_digest = $ctx->hexdigest;
60 $ctx = Digest::MD5->new;
61 $ctx->add( join( ':', $method, $auth_uri ) );
62 my $A2_digest = $ctx->hexdigest;
63 my $digest = Digest::MD5::md5_hex(
65 $A1_digest, $nonce, $qop ? ( $nc, $cnonce, $qop ) : (), $A2_digest )
68 $response = qq{Digest username="$username", realm="$realm", nonce="$nonce", uri="$auth_uri", qop=$qop, nc=$nc, cnonce="$cnonce", response="$digest", opaque="$opaque"};
70 my $r = HTTP::Request->new( GET => "http://localhost" . $uri );
72 $r->headers->push_header( Authorization => $response );
75 is( $mech->status, 400, "status is 400" );
77 is( $mech->status, 200, "status is 200" );
78 $mech->content_contains( $username, "Mufasa output" );
84 # Test with query string
85 do_test('Mufasa2', '/moose?moose_id=1');
86 # Test with query string, emulating .NET, which omits the query string
87 # from the Authorization header
88 do_test('Mufasa2', '/moose?moose_id=1', 1);
90 # Test with query string, emulating .NET, against app without .NET setting;
91 # authorization should fail
92 do_test('Mufasa2', '/moose?moose_id=1', 1, 1);