line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::WWW::Mechanize::Mojo; |
2
|
|
|
|
|
|
|
$Test::WWW::Mechanize::Mojo::VERSION = '0.0.20'; |
3
|
7
|
|
|
7
|
|
3695792
|
use strict; |
|
7
|
|
|
|
|
68
|
|
|
7
|
|
|
|
|
210
|
|
4
|
7
|
|
|
7
|
|
45
|
use warnings; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
175
|
|
5
|
|
|
|
|
|
|
|
6
|
7
|
|
|
7
|
|
168
|
use 5.012; |
|
7
|
|
|
|
|
29
|
|
7
|
|
|
|
|
|
|
|
8
|
7
|
|
|
7
|
|
46
|
use Carp qw/croak/; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
465
|
|
9
|
7
|
|
|
7
|
|
74
|
use Encode qw(); |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
133
|
|
10
|
7
|
|
|
7
|
|
3665
|
use HTML::Entities; |
|
7
|
|
|
|
|
42542
|
|
|
7
|
|
|
|
|
551
|
|
11
|
|
|
|
|
|
|
|
12
|
7
|
|
|
7
|
|
61
|
use base 'Test::WWW::Mechanize'; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
4985
|
|
13
|
|
|
|
|
|
|
|
14
|
7
|
|
|
7
|
|
618624
|
use Test::Mojo; |
|
7
|
|
|
|
|
20
|
|
|
7
|
|
|
|
|
77
|
|
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
|
54
|
my $self = shift; |
36
|
|
|
|
|
|
|
|
37
|
23
|
50
|
|
|
|
76
|
if (@_) |
38
|
|
|
|
|
|
|
{ |
39
|
0
|
|
|
|
|
0
|
$self->{allow_external} = shift; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
23
|
|
|
|
|
98
|
return $self->{allow_external}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub host |
46
|
|
|
|
|
|
|
{ |
47
|
2
|
|
|
2
|
1
|
10
|
my $self = shift; |
48
|
|
|
|
|
|
|
|
49
|
2
|
100
|
|
|
|
8
|
if (@_) |
50
|
|
|
|
|
|
|
{ |
51
|
1
|
|
|
|
|
4
|
$self->{host} = shift; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
2
|
|
|
|
|
4
|
return $self->{host}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub clear_host |
58
|
|
|
|
|
|
|
{ |
59
|
1
|
|
|
1
|
1
|
17566
|
my $self = shift; |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
|
|
3
|
delete( $self->{host} ); |
62
|
|
|
|
|
|
|
|
63
|
1
|
|
|
|
|
3
|
return; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub has_host |
67
|
|
|
|
|
|
|
{ |
68
|
23
|
|
|
23
|
1
|
53
|
my $self = shift; |
69
|
|
|
|
|
|
|
|
70
|
23
|
|
|
|
|
112
|
return exists( $self->{host} ); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub tester |
74
|
|
|
|
|
|
|
{ |
75
|
32
|
|
|
32
|
1
|
71
|
my $self = shift; |
76
|
|
|
|
|
|
|
|
77
|
32
|
100
|
|
|
|
107
|
if (@_) |
78
|
|
|
|
|
|
|
{ |
79
|
9
|
|
|
|
|
28
|
$self->{tester} = shift; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
32
|
|
|
|
|
216
|
return $self->{tester}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub new |
86
|
|
|
|
|
|
|
{ |
87
|
9
|
|
|
9
|
1
|
641724
|
my $class = shift; |
88
|
|
|
|
|
|
|
|
89
|
9
|
50
|
|
|
|
83
|
my $args = ref $_[0] ? $_[0] : {@_}; |
90
|
|
|
|
|
|
|
|
91
|
9
|
|
|
|
|
35
|
my $tester = delete( $args->{tester} ); |
92
|
|
|
|
|
|
|
|
93
|
9
|
|
|
|
|
124
|
my $self = $class->SUPER::new(%$args); |
94
|
|
|
|
|
|
|
|
95
|
9
|
50
|
|
|
|
105211
|
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
|
|
|
|
|
49
|
$self->{allow_external} = 0; |
105
|
|
|
|
|
|
|
|
106
|
9
|
|
|
|
|
168
|
return $self; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _make_request |
110
|
|
|
|
|
|
|
{ |
111
|
23
|
|
|
23
|
|
161200
|
my ( $self, $request ) = @_; |
112
|
|
|
|
|
|
|
|
113
|
23
|
|
|
|
|
92
|
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
|
|
|
|
3079
|
$self->cookie_jar->extract_cookies($response) if $self->cookie_jar; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# fail tests under the Catalyst debug screen |
120
|
23
|
50
|
66
|
|
|
2935
|
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
|
|
|
475
|
if ( $response->header('Location') |
|
|
|
66
|
|
|
|
|
135
|
|
|
|
|
|
|
&& $response->is_redirect |
136
|
|
|
|
|
|
|
&& $self->redirect_ok( $request, $response ) ) |
137
|
|
|
|
|
|
|
{ |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# remember the old response |
140
|
4
|
|
|
|
|
619
|
my $old_response = $response; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# *where* do they want us to redirect to? |
143
|
4
|
|
|
|
|
12
|
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
|
|
|
|
|
192
|
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
|
|
|
|
|
1211
|
$response = $self->_make_request( HTTP::Request->new( GET => $uri ) ); |
152
|
4
|
|
|
|
|
11
|
my $end_of_chain = $response; |
153
|
4
|
|
|
|
|
13
|
while ( $end_of_chain->previous ) # keep going till the end |
154
|
|
|
|
|
|
|
{ |
155
|
1
|
|
|
|
|
13
|
$end_of_chain = $end_of_chain->previous; |
156
|
|
|
|
|
|
|
} # of the chain... |
157
|
4
|
|
|
|
|
110
|
$end_of_chain->previous($old_response); # ...and add us to it |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
else |
160
|
|
|
|
|
|
|
{ |
161
|
19
|
|
|
|
|
915
|
$response->{_raw_content} = $response->content; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
23
|
|
|
|
|
456
|
return $response; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub _do_mojo_request |
168
|
|
|
|
|
|
|
{ |
169
|
23
|
|
|
23
|
|
57
|
my ( $self, $request ) = @_; |
170
|
|
|
|
|
|
|
|
171
|
23
|
|
|
|
|
76
|
my $uri = $request->uri; |
172
|
23
|
100
|
|
|
|
242
|
$uri->scheme('http') unless defined $uri->scheme; |
173
|
23
|
100
|
|
|
|
7108
|
$uri->host('localhost') unless defined $uri->host; |
174
|
|
|
|
|
|
|
|
175
|
23
|
|
|
|
|
1281
|
$request = $self->prepare_request($request); |
176
|
23
|
50
|
|
|
|
13237
|
$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
|
|
|
|
4148
|
if $ENV{MOJO_SERVER}; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# If there's no Host header, set one. |
183
|
23
|
50
|
|
|
|
79
|
unless ( $request->header('Host') ) |
184
|
|
|
|
|
|
|
{ |
185
|
23
|
100
|
|
|
|
981
|
my $host = |
186
|
|
|
|
|
|
|
$self->has_host |
187
|
|
|
|
|
|
|
? $self->host |
188
|
|
|
|
|
|
|
: $uri->host; |
189
|
|
|
|
|
|
|
|
190
|
23
|
|
|
|
|
605
|
$request->header( 'Host', $host ); |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
23
|
|
|
|
|
1231
|
my $res = $self->_check_external_request($request); |
194
|
23
|
50
|
|
|
|
66
|
return $res if $res; |
195
|
|
|
|
|
|
|
|
196
|
23
|
|
|
|
|
124
|
my @creds = $self->get_basic_credentials( "Basic", $uri ); |
197
|
23
|
100
|
|
|
|
1917
|
$request->authorization_basic(@creds) if @creds; |
198
|
|
|
|
|
|
|
|
199
|
23
|
|
|
|
|
375
|
my $t = $self->tester; |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
# Client |
202
|
23
|
|
|
|
|
140
|
my $client = $t->ua; |
203
|
23
|
|
|
|
|
536
|
$client->server->app( $t->app ); |
204
|
|
|
|
|
|
|
|
205
|
23
|
|
|
|
|
997
|
my $method = lc( $request->method() ); |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
my %headers = |
208
|
23
|
|
|
|
|
351
|
( map { $_ => $request->header($_) } $request->header_field_names() ); |
|
80
|
|
|
|
|
3472
|
|
209
|
|
|
|
|
|
|
|
210
|
23
|
|
|
|
|
1043
|
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
|
|
|
|
|
308286
|
[ %{ $mojo_res->headers->to_hash() } ], |
|
23
|
|
|
|
|
211
|
|
217
|
|
|
|
|
|
|
$mojo_res->body() ); |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# LWP would normally do this, but we dont get down that far. |
220
|
23
|
|
|
|
|
7003
|
$response->request($request); |
221
|
|
|
|
|
|
|
|
222
|
23
|
|
|
|
|
301
|
return $response; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
sub _check_external_request |
226
|
|
|
|
|
|
|
{ |
227
|
23
|
|
|
23
|
|
70
|
my ( $self, $request ) = @_; |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# If there's no host then definatley not an external request. |
230
|
23
|
50
|
|
|
|
80
|
$request->uri->can('host_port') or return; |
231
|
|
|
|
|
|
|
|
232
|
23
|
50
|
33
|
|
|
335
|
if ( $self->allow_external && $request->uri->host_port ne 'localhost:80' ) |
233
|
|
|
|
|
|
|
{ |
234
|
0
|
|
|
|
|
0
|
return $self->SUPER::_make_request($request); |
235
|
|
|
|
|
|
|
} |
236
|
23
|
|
|
|
|
56
|
return undef; |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
sub _do_remote_request |
240
|
|
|
|
|
|
|
{ |
241
|
0
|
|
|
0
|
|
|
my ( $self, $request ) = @_; |
242
|
|
|
|
|
|
|
|
243
|
0
|
|
|
|
|
|
my $res = $self->_check_external_request($request); |
244
|
0
|
0
|
|
|
|
|
return $res if $res; |
245
|
|
|
|
|
|
|
|
246
|
0
|
|
|
|
|
|
my $server = URI->new( $ENV{MOJO_SERVER} ); |
247
|
|
|
|
|
|
|
|
248
|
0
|
0
|
|
|
|
|
if ( $server->path =~ m|^(.+)?/$| ) |
249
|
|
|
|
|
|
|
{ |
250
|
0
|
|
|
|
|
|
my $path = $1; |
251
|
0
|
0
|
|
|
|
|
$server->path("$path") if $path; # need to be quoted |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
# the request path needs to be sanitised if $server is using a |
255
|
|
|
|
|
|
|
# non-root path due to potential overlap between request path and |
256
|
|
|
|
|
|
|
# response path. |
257
|
0
|
0
|
|
|
|
|
if ( $server->path ) |
258
|
|
|
|
|
|
|
{ |
259
|
|
|
|
|
|
|
# If request path is '/', we have to add a trailing slash to the |
260
|
|
|
|
|
|
|
# final request URI |
261
|
0
|
|
|
|
|
|
my $add_trailing = $request->uri->path eq '/'; |
262
|
|
|
|
|
|
|
|
263
|
0
|
|
|
|
|
|
my @sp = split '/', $server->path; |
264
|
0
|
|
|
|
|
|
my @rp = split '/', $request->uri->path; |
265
|
0
|
|
|
|
|
|
shift @sp; |
266
|
0
|
|
|
|
|
|
shift @rp; # leading / |
267
|
0
|
0
|
|
|
|
|
if (@rp) |
268
|
|
|
|
|
|
|
{ |
269
|
0
|
|
|
|
|
|
foreach my $sp (@sp) |
270
|
|
|
|
|
|
|
{ |
271
|
0
|
0
|
|
|
|
|
$sp eq $rp[0] ? shift @rp : last; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
} |
274
|
0
|
|
|
|
|
|
$request->uri->path( join '/', @rp ); |
275
|
|
|
|
|
|
|
|
276
|
0
|
0
|
|
|
|
|
if ($add_trailing) |
277
|
|
|
|
|
|
|
{ |
278
|
0
|
|
|
|
|
|
$request->uri->path( $request->uri->path . '/' ); |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
|
282
|
0
|
|
|
|
|
|
$request->uri->scheme( $server->scheme ); |
283
|
0
|
|
|
|
|
|
$request->uri->host( $server->host ); |
284
|
0
|
|
|
|
|
|
$request->uri->port( $server->port ); |
285
|
0
|
|
|
|
|
|
$request->uri->path( $server->path . $request->uri->path ); |
286
|
0
|
|
|
|
|
|
return $self->SUPER::_make_request($request); |
287
|
|
|
|
|
|
|
} |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
1; |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
__END__ |