Commit | Line | Data |
b2cd5ce7 |
1 | #!/usr/bin/perl |
2 | use strict; |
3 | use warnings; |
4 | use Test::More; |
5 | BEGIN { |
6 | eval { require Test::WWW::Mechanize::Catalyst } |
7 | or plan skip_all => |
8 | "Test::WWW::Mechanize::Catalyst is needed for this test"; |
9 | plan tests => 9; |
10 | } |
11 | use HTTP::Request; |
12 | { |
13 | package AuthTestApp; |
14 | use Catalyst qw/ |
15 | Authentication |
16 | /; |
17 | use Test::More; |
18 | our %users; |
19 | __PACKAGE__->config(authentication => { |
20 | default_realm => 'test_proxy', |
21 | realms => { |
22 | test => { |
23 | store => { |
24 | class => 'Minimal', |
25 | users => \%users, |
26 | }, |
27 | credential => { |
28 | class => 'HTTP', |
29 | type => 'basic', |
30 | password_type => 'clear', |
31 | password_field => 'password' |
32 | }, |
33 | }, |
34 | test_proxy => { |
35 | store => { |
36 | class => 'Minimal', |
37 | users => { |
38 | foo => { password => 'proxypass' } |
39 | }, |
40 | }, |
41 | credential => { |
42 | class => 'HTTP::Proxy', |
43 | url => 'http://localhost/moose', |
44 | type => 'basic', |
45 | }, |
46 | } |
47 | }, |
48 | }); |
49 | sub moose : Local { |
50 | my ( $self, $c ) = @_; |
51 | $c->authenticate({}, 'test'); |
52 | $c->res->body( 'test realm:' . $c->user->id ); |
53 | } |
54 | sub yak : Local { |
55 | my ( $self, $c ) = @_; |
56 | $c->authenticate({}, 'test_proxy'); |
57 | $c->res->body( 'test_proxy realm:' . $c->user->id ); |
58 | } |
59 | %users = ( |
60 | foo => { password => "s3cr3t", }, |
61 | ); |
62 | __PACKAGE__->setup; |
63 | } |
64 | use Test::WWW::Mechanize::Catalyst qw/AuthTestApp/; |
65 | { |
66 | no warnings qw/once redefine/; |
67 | *Catalyst::Authentication::Credential::HTTP::Proxy::User::new = sub { Test::WWW::Mechanize::Catalyst->new }; |
68 | } |
69 | SKIP: { |
70 | |
71 | skip 'Catalyst engine is not reenterant, this will not work', 5; |
72 | last; |
73 | |
74 | my $mech = Test::WWW::Mechanize::Catalyst->new; |
75 | { # HTTP, no auth |
76 | # $mech->get("http://localhost/moose"); |
77 | is( $mech->status, 401, "status is 401" ) or die $mech->content; |
78 | $mech->content_lacks( "foo", "no output" ); |
79 | } |
80 | |
81 | {# HTTP with auth |
82 | my $r = HTTP::Request->new( GET => "http://localhost/moose" ); |
83 | $r->authorization_basic(qw/foo s3cr3t/); |
84 | # $mech->request($r); |
85 | is( $mech->status, 200, "status is 200" ); |
86 | $mech->content_contains( "test realm:foo", "test realm:foo output" ); |
87 | } |
88 | |
89 | { # HTTP with other auth |
90 | my $r = HTTP::Request->new( GET => "http://localhost/moose" ); |
91 | $r->authorization_basic(qw/foo proxypass/); |
92 | # $mech->request($r); |
93 | is( $mech->status, 401, "status is 401" ) or die $mech->content; |
94 | } |
95 | } |
96 | |
97 | SKIP: { |
98 | |
99 | skip 'Catalyst engine is not reenterant, this will not work', 4; |
100 | last; |
101 | |
102 | my $mech = Test::WWW::Mechanize::Catalyst->new; |
103 | { # Proxy, no auth |
104 | # $mech->get("http://localhost/yak"); |
105 | is( $mech->status, 401, "status is 401" ) or die $mech->content; |
106 | $mech->content_lacks( "foo", "no output" ); |
107 | } |
108 | |
109 | { # Proxy with other auth |
110 | my $r = HTTP::Request->new( GET => "http://localhost/yak" ); |
111 | $r->authorization_basic(qw/foo s3cr3t/); |
112 | # $mech->request($r); |
113 | is( $mech->status, 401, "status is 401" ) or die $mech->content; |
114 | } |
115 | |
116 | { # HTTP with other auth |
117 | my $r = HTTP::Request->new( GET => "http://localhost/yak" ); |
118 | $r->authorization_basic(qw/foo proxypass/); |
119 | # $mech->request($r); |
120 | is( $mech->status, 200, "status is 200" ); |
121 | $mech->content_contains( "test_proxy realm:foo", "test_proxy realm:foo output" ); |
122 | } |
123 | } |