Skip to content

Commit

Permalink
fixed clone bugs in Mojo::URL
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 25, 2012
1 parent c9f0b6c commit 84b1d53
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

3.62 2012-11-26
- Improved tests.
- Fixed clone bugs in Mojo::URL.

3.61 2012-11-25
- Added protocol method to Mojo::URL.
Expand Down
16 changes: 11 additions & 5 deletions lib/Mojo/URL.pm
Expand Up @@ -136,12 +136,14 @@ sub query {

sub to_abs {
my $self = shift;
my $base = shift || $self->base->clone;

# Scheme
# Already absolute
my $abs = $self->clone;
return $abs if $abs->is_abs;
$abs->scheme($base->scheme);

# Scheme
my $base = shift || $abs->base;
$abs->base($base)->scheme($base->scheme);

# Authority
return $abs if $abs->authority;
Expand Down Expand Up @@ -170,10 +172,14 @@ sub to_abs {

sub to_rel {
my $self = shift;
my $base = shift || $self->base->clone;

# Already relative
my $rel = $self->clone;
return $rel unless $rel->is_abs;

# Scheme and authority
my $rel = $self->clone->base($base)->scheme(undef);
my $base = shift || $rel->base;
$rel->base($base)->scheme(undef);
$rel->userinfo(undef)->host(undef)->port(undef) if $base->authority;

# Path
Expand Down
8 changes: 8 additions & 0 deletions t/mojo/url.t
Expand Up @@ -202,6 +202,14 @@ is $rel, 'c/d/index.html', 'right relative version';
is $rel->to_abs, 'http://kraih.com/a/b/c/d/index.html',
'right absolute version';
is $rel->to_abs->to_rel, 'c/d/index.html', 'right relative version';
$url = Mojo::URL->new('/foo');
ok !$url->base->is_abs, 'right base';
ok $url->to_abs(Mojo::URL->new('http://kraih.com'))->base->is_abs,
'right base';
$url = Mojo::URL->new('http://kraih.com/foo');
ok !$url->base->is_abs, 'right base';
ok $url->to_rel(Mojo::URL->new('http://kraih.com'))->base->is_abs,
'right base';

# Relative path
$url = Mojo::URL->new('http://kraih.com/foo/?foo=bar#23');
Expand Down

0 comments on commit 84b1d53

Please sign in to comment.