1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#============================================================= -*-perl-*-
#
# t/url.t
#
# Test the Badger::URL module.
#
# Run with -h option for help.
#
# Written by Andy Wardley <abw@wardley.org>.
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
#========================================================================
use strict;
use warnings;
use lib qw( t/core/lib ./lib ../lib ../../lib );
use Badger::Test
debug => 'Badger::URL',
args => \@ARGV,
tests => 46;
use Badger::URL 'URL';
my $SCHEME = 'http';
my $USER = 'badger';
my $HOST = 'badgerpower.com';
my $PORT = 8080;
my $AUTHORITY = "$USER\@$HOST:$PORT";
my $PATH = '/over/there';
my $QUERY = 'animal=badger';
my $FRAGMENT = 'nose';
my $SERVER = "$SCHEME://$AUTHORITY";
my $SERVICE = "$SERVER$PATH";
my $REQUEST = "$SERVICE?$QUERY";
my $URL = "$REQUEST#$FRAGMENT";
my $url = URL->new($URL);
ok( $url, 'created a URL object' );
is( $url, $URL, 'returns source URL on stringification' );
$url = URL($URL);
ok( $url, 'created a URL object from constructor function' );
is( $url, $URL, 'also returns source URL on stringification' );
#------------------------------------------------------------------------
# test accessors to read various parts of the URL
#------------------------------------------------------------------------
is( $url->scheme, $SCHEME, "scheme is $SCHEME" );
is( $url->authority, $AUTHORITY,"authority is $AUTHORITY" );
is( $url->user, $USER, "user is $USER" );
is( $url->host, $HOST, "host is $HOST" );
is( $url->port, $PORT, "port is $PORT" );
is( $url->path, $PATH, "path is $PATH" );
is( $url->query, $QUERY, "query is $QUERY" );
is( $url->fragment, $FRAGMENT, "fragment is $FRAGMENT" );
is( $url->server, $SERVER, "server is $SERVER" );
is( $url->service, $SERVICE, "service is $SERVICE" );
is( $url->request, $REQUEST, "request is $REQUEST" );
my $params = $url->params;
ok( $params, 'got params' );
is( $params->{ animal }, 'badger', 'animal is a badger' );
my $copy = $url->copy;
ok( $copy, 'got a copy' );
$copy->params( friend => 'ferret', food => 'berries' );
is($copy, "$SERVICE?animal=badger&food=berries&friend=ferret#nose", 'new url with params' );
#------------------------------------------------------------------------
# now use same methods as mutators to change parts of the URL
#------------------------------------------------------------------------
$copy = $url->copy;
ok( $copy, 'got another copy' );
is( $copy->scheme('ftp'), 'ftp', 'set scheme to ftp' );
is( $copy, "ftp://$AUTHORITY$PATH?$QUERY#$FRAGMENT",
'changed scheme' );
is( $copy->user('ferret'), 'ferret', 'set user to ferret' );
is( $copy, "ftp://ferret\@$HOST:$PORT$PATH?$QUERY#$FRAGMENT",
'changed user' );
is( $copy->host('example.com'), 'example.com', 'set host to example.com' );
is( $copy, "ftp://ferret\@example.com:$PORT$PATH?$QUERY#$FRAGMENT",
'changed host' );
is( $copy->port(1234), 1234, 'set port to 1234' );
is( $copy, "ftp://ferret\@example.com:1234$PATH?$QUERY#$FRAGMENT",
'changed port' );
is( $copy->path('/right/here'), '/right/here', 'set path to /right/here' );
is( $copy, "ftp://ferret\@example.com:1234/right/here?$QUERY#$FRAGMENT",
'changed path' );
is( $copy->query('animal=ferret'), 'animal=ferret', 'set query to animal=ferret' );
is( $copy, "ftp://ferret\@example.com:1234/right/here?animal=ferret#$FRAGMENT",
'changed query' );
is( $copy->fragment('feet'), 'feet', 'set fragment to feet' );
is( $copy, "ftp://ferret\@example.com:1234/right/here?animal=ferret#feet",
'changed fragment' );
#-----------------------------------------------------------------------
# test relative URLs
#-----------------------------------------------------------------------
is( $url->relative('foo'),
"$SERVER/over/there/foo?$QUERY#$FRAGMENT",
'set relative path: foo'
);
is( $url->relative('/foo'),
"$SERVER/foo?$QUERY#$FRAGMENT",
'set relative path: /foo'
);
is( $url->absolute('bar'),
"$SERVER/bar?$QUERY#$FRAGMENT",
'set absolute path: bar'
);
is( $url->absolute('/bar'),
"$SERVER/bar?$QUERY#$FRAGMENT",
'set absolute path: /bar'
);
# original URL should not be changed
is( $url->url, $URL, 'url is unchanged' );
#-----------------------------------------------------------------------
# test constructor with separate elements
#-----------------------------------------------------------------------
$url = URL->new(
scheme => 'http',
user => 'Mr.T',
host => 'badgerpower.com',
port => '8081',
path => '/somewhere/else',
query => 'animal=badger',
fragment => 'stripe',
);
ok( $url, 'created url from params' );
is( $url->authority, 'Mr.T@badgerpower.com:8081', 'got params authority' );
is( $url->server, 'http://Mr.T@badgerpower.com:8081', 'got params server' );
is( $url->service, 'http://Mr.T@badgerpower.com:8081/somewhere/else', 'got params service' );
is( $url->request, 'http://Mr.T@badgerpower.com:8081/somewhere/else?animal=badger', 'got params request' );
$url = URL->new('http://badgerpower.com/');
$url->port('8080');
$url->path('/under/ground');
$url->query('animal=badger');
is($url, 'http://badgerpower.com:8080/under/ground?animal=badger', 'new url' );
$url->params( friend => 'ferret', food => 'berries' );
is($url, 'http://badgerpower.com:8080/under/ground?animal=badger&food=berries&friend=ferret', 'new url with params' );
__END__
# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4:
|