Skip to content

Commit 6a9cefc

Browse files
oaldersgenio
authored andcommitted
If an object is passed to HTTP::Request, it must provide a canonical()
I ran into this issue today when I tried to GET a Mojo::URL object. No error was thrown until HTTP::Request tried to call the canonical() method on the object. So, just checking if an object provides a scheme() method is not enough.
1 parent 7f1ac10 commit 6a9cefc

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

Changes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Revision history for HTTP-Message
22

33
{{$NEXT}}
4+
- If an object is passed to HTTP::Request, it must provide a canonical()
5+
method (Olaf Alders)
46

57
6.11 2015-09-09
68
- fix an undefined value warning in HTTP::Headers::as_string

lib/HTTP/Request.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ sub uri
6565
Carp::croak("A URI can't be a " . ref($uri) . " reference")
6666
if ref($uri) eq 'HASH' or ref($uri) eq 'ARRAY';
6767
Carp::croak("Can't use a " . ref($uri) . " object as a URI")
68-
unless $uri->can('scheme');
68+
unless $uri->can('scheme') && $uri->can('canonical');
6969
$uri = $uri->clone;
7070
unless ($HTTP::URI_CLASS eq "URI") {
7171
# Argh!! Hate this... old LWP legacy!

t/request.t

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use strict;
55
use warnings;
66

77
use Test::More;
8-
plan tests => 11;
8+
use Test::Fatal qw( dies_ok lives_ok );
9+
plan tests => 15;
910

1011
use HTTP::Request;
1112

@@ -31,3 +32,41 @@ is($r2->method, "DELETE");
3132
is($r2->uri, "http:");
3233
is($r2->protocol, "HTTP/1.1");
3334
is($r2->header("Accept-Encoding"), $req->header("Accept-Encoding"));
35+
36+
# Test objects which are accepted as URI-like
37+
{
38+
package Foo::URI;
39+
40+
use strict;
41+
use warnings;
42+
43+
sub new { return bless {}, shift; }
44+
sub clone { return shift }
45+
sub scheme { }
46+
47+
1;
48+
49+
package Foo::URI::WithCanonical;
50+
51+
sub new { return bless {}, shift; }
52+
sub clone { return shift }
53+
sub scheme { }
54+
sub canonical { }
55+
56+
1;
57+
58+
package main;
59+
60+
ok( Foo::URI->new->can( 'scheme' ), 'Object can scheme()' );
61+
dies_ok(
62+
sub { HTTP::Request->new( GET => Foo::URI->new ) },
63+
'Object without canonical method triggers an exception'
64+
);
65+
66+
ok( Foo::URI::WithCanonical->new->can( 'canonical' ),
67+
'Object can canonical()' );
68+
lives_ok(
69+
sub { HTTP::Request->new( GET => Foo::URI::WithCanonical->new ) },
70+
'Object with canonical method does not trigger an exception'
71+
);
72+
}

0 commit comments

Comments
 (0)