line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::WWW::Mechanize::Mojo; |
2
|
|
|
|
|
|
|
$Test::WWW::Mechanize::Mojo::VERSION = '0.0.21'; |
3
|
7
|
|
|
7
|
|
3877731
|
use strict; |
|
7
|
|
|
|
|
71
|
|
|
7
|
|
|
|
|
235
|
|
4
|
7
|
|
|
7
|
|
43
|
use warnings; |
|
7
|
|
|
|
|
18
|
|
|
7
|
|
|
|
|
198
|
|
5
|
|
|
|
|
|
|
|
6
|
7
|
|
|
7
|
|
203
|
use 5.012; |
|
7
|
|
|
|
|
27
|
|
7
|
|
|
|
|
|
|
|
8
|
7
|
|
|
7
|
|
67
|
use Carp qw/croak/; |
|
7
|
|
|
|
|
27
|
|
|
7
|
|
|
|
|
566
|
|
9
|
7
|
|
|
7
|
|
72
|
use Encode qw(); |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
192
|
|
10
|
7
|
|
|
7
|
|
3701
|
use HTML::Entities; |
|
7
|
|
|
|
|
43002
|
|
|
7
|
|
|
|
|
559
|
|
11
|
|
|
|
|
|
|
|
12
|
7
|
|
|
7
|
|
62
|
use base 'Test::WWW::Mechanize'; |
|
7
|
|
|
|
|
18
|
|
|
7
|
|
|
|
|
5004
|
|
13
|
|
|
|
|
|
|
|
14
|
7
|
|
|
7
|
|
649487
|
use Test::Mojo; |
|
7
|
|
|
|
|
21
|
|
|
7
|
|
|
|
|
75
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $APP_CLASS; |
17
|
|
|
|
|
|
|
my $Test = Test::Builder->new(); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub mojo_app |
20
|
|
|
|
|
|
|
{ |
21
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
0
|
return $self->{mojo_app}; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub has_mojo_app |
27
|
|
|
|
|
|
|
{ |
28
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
0
|
return exists( $self->{mojo_app} ); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub allow_external |
34
|
|
|
|
|
|
|
{ |
35
|
23
|
|
|
23
|
1
|
58
|
my $self = shift; |
36
|
|
|
|
|
|
|
|
37
|
23
|
50
|
|
|
|
76
|
if (@_) |
38
|
|
|
|
|
|
|
{ |
39
|
0
|
|
|
|
|
0
|
$self->{allow_external} = shift; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
23
|
|
|
|
|
100
|
return $self->{allow_external}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub host |
46
|
|
|
|
|
|
|
{ |
47
|
2
|
|
|
2
|
1
|
9
|
my $self = shift; |
48
|
|
|
|
|
|
|
|
49
|
2
|
100
|
|
|
|
7
|
if (@_) |
50
|
|
|
|
|
|
|
{ |
51
|
1
|
|
|
|
|
3
|
$self->{host} = shift; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
2
|
|
|
|
|
5
|
return $self->{host}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub clear_host |
58
|
|
|
|
|
|
|
{ |
59
|
1
|
|
|
1
|
1
|
18345
|
my $self = shift; |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
|
|
4
|
delete( $self->{host} ); |
62
|
|
|
|
|
|
|
|
63
|
1
|
|
|
|
|
5
|
return (); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub has_host |
67
|
|
|
|
|
|
|
{ |
68
|
23
|
|
|
23
|
1
|
43
|
my $self = shift; |
69
|
|
|
|
|
|
|
|
70
|
23
|
|
|
|
|
110
|
return exists( $self->{host} ); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub tester |
74
|
|
|
|
|
|
|
{ |
75
|
32
|
|
|
32
|
1
|
74
|
my $self = shift; |
76
|
|
|
|
|
|
|
|
77
|
32
|
100
|
|
|
|
115
|
if (@_) |
78
|
|
|
|
|
|
|
{ |
79
|
9
|
|
|
|
|
31
|
$self->{tester} = shift; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
32
|
|
|
|
|
176
|
return $self->{tester}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub new |
86
|
|
|
|
|
|
|
{ |
87
|
9
|
|
|
9
|
1
|
685097
|
my $class = shift; |
88
|
|
|
|
|
|
|
|
89
|
9
|
50
|
|
|
|
79
|
my $args = ref $_[0] ? $_[0] : {@_}; |
90
|
|
|
|
|
|
|
|
91
|
9
|
|
|
|
|
39
|
my $tester = delete( $args->{tester} ); |
92
|
|
|
|
|
|
|
|
93
|
9
|
|
|
|
|
123
|
my $self = $class->SUPER::new(%$args); |
94
|
|
|
|
|
|
|
|
95
|
9
|
50
|
|
|
|
109465
|
if ($tester) |
96
|
|
|
|
|
|
|
{ |
97
|
9
|
|
|
|
|
49
|
$self->tester($tester); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
else |
100
|
|
|
|
|
|
|
{ |
101
|
0
|
|
|
|
|
0
|
$self->tester( Test::Mojo->new() ); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
9
|
|
|
|
|
27
|
$self->{allow_external} = 0; |
105
|
|
|
|
|
|
|
|
106
|
9
|
|
|
|
|
108
|
return $self; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _make_request |
110
|
|
|
|
|
|
|
{ |
111
|
23
|
|
|
23
|
|
162653
|
my ( $self, $request ) = @_; |
112
|
|
|
|
|
|
|
|
113
|
23
|
|
|
|
|
96
|
my $response = $self->_do_mojo_request($request); |
114
|
23
|
50
|
|
|
|
127
|
$response->header( 'Content-Base', $response->request->uri ) |
115
|
|
|
|
|
|
|
unless $response->header('Content-Base'); |
116
|
|
|
|
|
|
|
|
117
|
23
|
50
|
|
|
|
3111
|
$self->cookie_jar->extract_cookies($response) if $self->cookie_jar; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# fail tests under the Catalyst debug screen |
120
|
23
|
50
|
66
|
|
|
3020
|
if ( !$self->{catalyst_debug} |
|
|
|
66
|
|
|
|
|
121
|
|
|
|
|
|
|
&& $response->code == 500 |
122
|
|
|
|
|
|
|
&& $response->content =~ /on Catalyst \d+\.\d+/ ) |
123
|
|
|
|
|
|
|
{ |
124
|
0
|
|
|
|
|
0
|
my ($error) = |
125
|
|
|
|
|
|
|
( $response->content =~ /(.*?)<\/code>/s ); |
126
|
0
|
|
0
|
|
|
0
|
$error ||= "unknown error"; |
127
|
0
|
|
|
|
|
0
|
decode_entities($error); |
128
|
0
|
|
|
|
|
0
|
$Test->diag("Catalyst error screen: $error"); |
129
|
0
|
|
|
|
|
0
|
$response->content(''); |
130
|
0
|
|
|
|
|
0
|
$response->content_type(''); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# check if that was a redirect |
134
|
23
|
100
|
100
|
|
|
506
|
if ( $response->header('Location') |
|
|
|
66
|
|
|
|
|
135
|
|
|
|
|
|
|
&& $response->is_redirect |
136
|
|
|
|
|
|
|
&& $self->redirect_ok( $request, $response ) ) |
137
|
|
|
|
|
|
|
{ |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# remember the old response |
140
|
4
|
|
|
|
|
644
|
my $old_response = $response; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# *where* do they want us to redirect to? |
143
|
4
|
|
|
|
|
13
|
my $location = $old_response->header('Location'); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# no-one *should* be returning non-absolute URLs, but if they |
146
|
|
|
|
|
|
|
# are then we'd better cope with it. Let's create a new URI, using |
147
|
|
|
|
|
|
|
# our request as the base. |
148
|
4
|
|
|
|
|
178
|
my $uri = URI->new_abs( $location, $request->uri )->as_string; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# make a new response, and save the old response in it |
151
|
4
|
|
|
|
|
1179
|
$response = $self->_make_request( HTTP::Request->new( GET => $uri ) ); |
152
|
4
|
|
|
|
|
8
|
my $end_of_chain = $response; |
153
|
4
|
|
|
|
|
15
|
while ( $end_of_chain->previous ) # keep going till the end |
154
|
|
|
|
|
|
|
{ |
155
|
1
|
|
|
|
|
12
|
$end_of_chain = $end_of_chain->previous; |
156
|
|
|
|
|
|
|
} # of the chain... |
157
|
4
|
|
|
|
|
56
|
$end_of_chain->previous($old_response); # ...and add us to it |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
else |
160
|
|
|
|
|
|
|
{ |
161
|
19
|
|
|
|
|
900
|
$response->{_raw_content} = $response->content; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
23
|
|
|
|
|
501
|
return $response; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub _do_mojo_request |
168
|
|
|
|
|
|
|
{ |
169
|
23
|
|
|
23
|
|
62
|
my ( $self, $request ) = @_; |
170
|
|
|
|
|
|
|
|
171
|
23
|
|
|
|
|
70
|
my $uri = $request->uri; |
172
|
23
|
100
|
|
|
|
241
|
$uri->scheme('http') unless defined $uri->scheme; |
173
|
23
|
100
|
|
|
|
7201
|
$uri->host('localhost') unless defined $uri->host; |
174
|
|
|
|
|
|
|
|
175
|
23
|
|
|
|
|
1345
|
$request = $self->prepare_request($request); |
176
|
23
|
50
|
|
|
|
14593
|
$self->cookie_jar->add_cookie_header($request) if $self->cookie_jar; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Woe betide anyone who unsets MOJO_SERVER |
179
|
|
|
|
|
|
|
return $self->_do_remote_request($request) |
180
|
23
|
50
|
|
|
|
4115
|
if $ENV{MOJO_SERVER}; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# If there's no Host header, set one. |
183
|
23
|
50
|
|
|
|
73
|
unless ( $request->header('Host') ) |
184
|
|
|
|
|
|
|
{ |
185
|
23
|
100
|
|
|
|
1020
|
my $host = |
186
|
|
|
|
|
|
|
$self->has_host |
187
|
|
|
|
|
|
|
? $self->host |
188
|
|
|
|
|
|
|
: $uri->host; |
189
|
|
|
|
|
|
|
|
190
|
23
|
|
|
|
|
616
|
$request->header( 'Host', $host ); |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
23
|
|
|
|
|
1209
|
my $res = $self->_check_external_request($request); |
194
|
23
|
50
|
|
|
|
66
|
return $res if $res; |
195
|
|
|
|
|
|
|
|
196
|
23
|
|
|
|
|
121
|
my @creds = $self->get_basic_credentials( "Basic", $uri ); |
197
|
23
|
100
|
|
|
|
1976
|
$request->authorization_basic(@creds) if @creds; |
198
|
|
|
|
|
|
|
|
199
|
23
|
|
|
|
|
357
|
my $t = $self->tester; |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
# Client |
202
|
23
|
|
|
|
|
137
|
my $client = $t->ua; |
203
|
23
|
|
|
|
|
932
|
$client->server->app( $t->app ); |
204
|
|
|
|
|
|
|
|
205
|
23
|
|
|
|
|
1014
|
my $method = lc( $request->method() ); |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
my %headers = |
208
|
23
|
|
|
|
|
362
|
( map { $_ => $request->header($_) } $request->header_field_names() ); |
|
80
|
|
|
|
|
3631
|
|
209
|
|
|
|
|
|
|
|
210
|
23
|
|
|
|
|
1018
|
my $mojo_res = |
211
|
|
|
|
|
|
|
$client->$method( $uri->path_query(), {%headers}, $request->content, ) |
212
|
|
|
|
|
|
|
->res; |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
my $response = |
215
|
|
|
|
|
|
|
HTTP::Response->new( $mojo_res->code(), $mojo_res->message(), |
216
|
23
|
|
|
|
|
347443
|
[ %{ $mojo_res->headers->to_hash() } ], |
|
23
|
|
|
|
|
210
|
|
217
|
|
|
|
|
|
|
$mojo_res->body() ); |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# LWP would normally do this, but we dont get down that far. |
220
|
23
|
|
|
|
|
7082
|
$response->request($request); |
221
|
|
|
|
|
|
|
|
222
|
23
|
|
|
|
|
293
|
return $response; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
sub _check_external_request |
226
|
|
|
|
|
|
|
{ |
227
|
23
|
|
|
23
|
|
71
|
my ( $self, $request ) = @_; |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# If there's no host then definitely not an external request. |
230
|
23
|
50
|
|
|
|
87
|
if ( not $request->uri->can('host_port') ) |
231
|
|
|
|
|
|
|
{ |
232
|
0
|
|
|
|
|
0
|
return undef; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
23
|
50
|
33
|
|
|
310
|
if ( $self->allow_external && $request->uri->host_port ne 'localhost:80' ) |
236
|
|
|
|
|
|
|
{ |
237
|
0
|
|
|
|
|
0
|
return $self->SUPER::_make_request($request); |
238
|
|
|
|
|
|
|
} |
239
|
23
|
|
|
|
|
62
|
return undef; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
sub _do_remote_request |
243
|
|
|
|
|
|
|
{ |
244
|
0
|
|
|
0
|
|
|
my ( $self, $request ) = @_; |
245
|
|
|
|
|
|
|
|
246
|
0
|
|
|
|
|
|
my $res = $self->_check_external_request($request); |
247
|
0
|
0
|
|
|
|
|
return $res if $res; |
248
|
|
|
|
|
|
|
|
249
|
0
|
|
|
|
|
|
my $server = URI->new( $ENV{MOJO_SERVER} ); |
250
|
|
|
|
|
|
|
|
251
|
0
|
0
|
|
|
|
|
if ( $server->path =~ m|^(.+)?/$| ) |
252
|
|
|
|
|
|
|
{ |
253
|
0
|
|
|
|
|
|
my $path = $1; |
254
|
0
|
0
|
|
|
|
|
$server->path("$path") if $path; # need to be quoted |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
# the request path needs to be sanitised if $server is using a |
258
|
|
|
|
|
|
|
# non-root path due to potential overlap between request path and |
259
|
|
|
|
|
|
|
# response path. |
260
|
0
|
0
|
|
|
|
|
if ( $server->path ) |
261
|
|
|
|
|
|
|
{ |
262
|
|
|
|
|
|
|
# If request path is '/', we have to add a trailing slash to the |
263
|
|
|
|
|
|
|
# final request URI |
264
|
0
|
|
|
|
|
|
my $add_trailing = $request->uri->path eq '/'; |
265
|
|
|
|
|
|
|
|
266
|
0
|
|
|
|
|
|
my @sp = split '/', $server->path; |
267
|
0
|
|
|
|
|
|
my @rp = split '/', $request->uri->path; |
268
|
0
|
|
|
|
|
|
shift @sp; |
269
|
0
|
|
|
|
|
|
shift @rp; # leading / |
270
|
0
|
0
|
|
|
|
|
if (@rp) |
271
|
|
|
|
|
|
|
{ |
272
|
0
|
|
|
|
|
|
foreach my $sp (@sp) |
273
|
|
|
|
|
|
|
{ |
274
|
0
|
0
|
|
|
|
|
$sp eq $rp[0] ? shift @rp : last; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
} |
277
|
0
|
|
|
|
|
|
$request->uri->path( join '/', @rp ); |
278
|
|
|
|
|
|
|
|
279
|
0
|
0
|
|
|
|
|
if ($add_trailing) |
280
|
|
|
|
|
|
|
{ |
281
|
0
|
|
|
|
|
|
$request->uri->path( $request->uri->path . '/' ); |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
|
285
|
0
|
|
|
|
|
|
$request->uri->scheme( $server->scheme ); |
286
|
0
|
|
|
|
|
|
$request->uri->host( $server->host ); |
287
|
0
|
|
|
|
|
|
$request->uri->port( $server->port ); |
288
|
0
|
|
|
|
|
|
$request->uri->path( $server->path . $request->uri->path ); |
289
|
0
|
|
|
|
|
|
return $self->SUPER::_make_request($request); |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
1; |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
__END__ |