| 1 | package Test::WWW::Mechanize::Catalyst::WithContext; | ||||||||||||
| 2 | 4 | ▶ | use 5.008001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 4 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 4 | ▶ | use MooseX::Aliases; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 4 | ▶ | use HTTP::Request; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 4 | ▶ | use Carp 'croak'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 4 | require Catalyst::Test; | |||||||||||
| 9 | 4 | extends 'Test::WWW::Mechanize::Catalyst'; | |||||||||||
| 10 | |||||||||||||
| 11 | 4 | our $VERSION = "0.03"; | |||||||||||
| 12 | 4 | $VERSION = eval $VERSION; | |||||||||||
| 13 | |||||||||||||
| 14 | 4 | has ctx => ( | |||||||||||
| 15 | is => 'ro', | ||||||||||||
| 16 | isa => 'Maybe[Catalyst]', | ||||||||||||
| 17 | writer => '_set_ctx', | ||||||||||||
| 18 | alias => 'c', | ||||||||||||
| 19 | default => undef, | ||||||||||||
| 20 | ); | ||||||||||||
| 21 | |||||||||||||
| 22 | # this stores the ctx_request function as a code reference | ||||||||||||
| 23 | 4 | has _get_context => ( | |||||||||||
| 24 | is => 'ro', | ||||||||||||
| 25 | isa => 'CodeRef', | ||||||||||||
| 26 | lazy => 1, | ||||||||||||
| 27 | builder => '_build__get_context', | ||||||||||||
| 28 | ); | ||||||||||||
| 29 | |||||||||||||
| 30 | 3 | ▶ | sub _build__get_context { | ||||||||||
|
Subroutine
_build__get_context: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | 3 | my ($self) = @_; | |||||||||||
| 32 | |||||||||||||
| 33 | # we need $request for ctx_request | ||||||||||||
| 34 | my $request = Catalyst::Test::_build_request_export( | ||||||||||||
| 35 | undef, # this is C::T's $self | ||||||||||||
| 36 | { class => $self->{catalyst_app}, remote => $ENV{CATALYST_SERVER} } | ||||||||||||
| 37 | 3 | ); | |||||||||||
| 38 | |||||||||||||
| 39 | return Catalyst::Test::_build_ctx_request_export( | ||||||||||||
| 40 | undef, # this is C::T's $self | ||||||||||||
| 41 | { | ||||||||||||
| 42 | class => $self->{catalyst_app}, | ||||||||||||
| 43 | 3 | request => $request, | |||||||||||
| 44 | } | ||||||||||||
| 45 | ); | ||||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | # this is deprecated | ||||||||||||
| 49 | 2 | ▶ | sub get_context { | ||||||||||
|
Subroutine
get_context: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 50 | 2 | my ( $self, $url ) = @_; | |||||||||||
| 51 | |||||||||||||
| 52 | 2 | warnings::warn( 'deprecated', | |||||||||||
| 53 | 'get_context is deprecated and will be removed in a future release' ); | ||||||||||||
| 54 | |||||||||||||
| 55 | 2 | ▶ | croak 'url is required' unless $url; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | |||||||||||||
| 57 | 1 | ▶ | my $request = | ||||||||||
|
Condition50%1/2
$self->base || 'http://localhost'
MC/DC0%0/1
$self->base || 'http://localhost'
$self->base
Truth table50%1/2
A
A= $self->base
| |||||||||||||
| 58 | HTTP::Request->new( GET => URI->new_abs( $url, $self->base || 'http://localhost' ) ); | ||||||||||||
| 59 | 1 | $self->cookie_jar->add_cookie_header($request); | |||||||||||
| 60 | |||||||||||||
| 61 | 1 | my ( $res, $c ) = $self->_get_context->($request); | |||||||||||
| 62 | |||||||||||||
| 63 | 1 | return $res, $c; | |||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | # This code is based on the method for Test::WWW::Mechanize::Catalyst | ||||||||||||
| 67 | # and overwrites it. | ||||||||||||
| 68 | 7 | ▶ | sub _do_catalyst_request { | ||||||||||
|
Subroutine
_do_catalyst_request: called CC=7 SCAR=19.7
| |||||||||||||
| 69 | 7 | my ( $self, $request ) = @_; | |||||||||||
| 70 | |||||||||||||
| 71 | 7 | my $uri = $request->uri; | |||||||||||
| 72 | 7 | ▶ | $uri->scheme('http') unless defined $uri->scheme; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 7 | ▶ | $uri->host('localhost') unless defined $uri->host; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | |||||||||||||
| 75 | 7 | $request = $self->prepare_request($request); | |||||||||||
| 76 | 7 | ▶ | $self->cookie_jar->add_cookie_header($request) if $self->cookie_jar; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | |||||||||||||
| 78 | # Woe betide anyone who unsets CATALYST_SERVER | ||||||||||||
| 79 | return $self->_do_remote_request($request) | ||||||||||||
| 80 | 7 | ▶ | if $ENV{CATALYST_SERVER}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | |||||||||||||
| 82 | 7 | $self->_set_host_header($request); | |||||||||||
| 83 | |||||||||||||
| 84 | 7 | my $res = $self->_check_external_request($request); | |||||||||||
| 85 | 7 | ▶ | return $res if $res; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | |||||||||||||
| 87 | 7 | my @creds = $self->get_basic_credentials( "Basic", $uri ); | |||||||||||
| 88 | 7 | ▶ | $request->authorization_basic(@creds) if @creds; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | |||||||||||||
| 90 | 7 | my ( $response, $c ) = $self->_get_context->($request); | |||||||||||
| 91 | |||||||||||||
| 92 | 7 | $self->_set_ctx($c); | |||||||||||
| 93 | |||||||||||||
| 94 | # LWP would normally do this, but we don't get down that far. | ||||||||||||
| 95 | 7 | $response->request($request); | |||||||||||
| 96 | |||||||||||||
| 97 | 7 | return $response; | |||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | 4 | __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); | |||||||||||
| 101 | |||||||||||||
| 102 | 4 | 1; | |||||||||||
| 103 | __END__ | ||||||||||||