0af788fc6863519ca1067c21fc29959efd4a083e
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_uri_with.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use URI;
6 use Catalyst::Log;
7
8 use_ok('Catalyst::Request');
9
10 sub cmp_uri {
11     my ($got, $exp_txt, $comment) = @_;
12     $comment ||= '';
13     my $exp = URI->new($exp_txt);
14     foreach my $thing (qw/ scheme host path /) {
15         is $exp->$thing, $got->$thing, "$comment: $thing";
16     }
17     my %got_q = map { split /=/ } split /\&/, ($got->query||'');
18     my %exp_q = map { split /=/ } split /\&/, ($exp->query||'');
19     is_deeply \%got_q, \%exp_q, "$comment: query";
20 }
21
22 my $request = Catalyst::Request->new( {
23                 _log => Catalyst::Log->new,
24                 uri => URI->new('http://127.0.0.1/foo/bar/baz')
25               } );
26
27 cmp_uri(
28     $request->uri_with({}),
29     'http://127.0.0.1/foo/bar/baz',
30     'URI for absolute path'
31 );
32
33 cmp_uri(
34     $request->uri_with({ foo => 'bar' }),
35     'http://127.0.0.1/foo/bar/baz?foo=bar',
36     'URI adds param'
37 );
38
39 my $request2 = Catalyst::Request->new( {
40                 _log => Catalyst::Log->new,
41                 uri => URI->new('http://127.0.0.1/foo/bar/baz?bar=gorch')
42               } );
43
44 cmp_uri(
45     $request2->uri_with({}),
46     'http://127.0.0.1/foo/bar/baz?bar=gorch',
47     'URI retains param'
48 );
49
50 cmp_uri(
51     $request2->uri_with({ me => 'awesome' }),
52     'http://127.0.0.1/foo/bar/baz?bar=gorch&me=awesome',
53     'URI retains param and adds new'
54 );
55
56 cmp_uri(
57     $request2->uri_with({ bar => undef }),
58     'http://127.0.0.1/foo/bar/baz',
59     'URI loses param when explicitly undef'
60 );
61
62 cmp_uri(
63     $request2->uri_with({ bar => 'snort' }),
64     'http://127.0.0.1/foo/bar/baz?bar=snort',
65     'URI changes param'
66 );
67
68 cmp_uri(
69     $request2->uri_with({ bar => [ 'snort', 'ewok' ] }),
70     'http://127.0.0.1/foo/bar/baz?bar=snort&bar=ewok',
71     'overwrite mode URI appends arrayref param'
72 );
73
74 cmp_uri(
75     $request2->uri_with({ bar => 'snort' }, { mode => 'append' }),
76     'http://127.0.0.1/foo/bar/baz?bar=gorch&bar=snort',
77     'append mode URI appends param'
78 );
79
80 cmp_uri(
81     $request2->uri_with({ bar => [ 'snort', 'ewok' ] }, { mode => 'append' }),
82     'http://127.0.0.1/foo/bar/baz?bar=gorch&bar=snort&bar=ewok',
83     'append mode URI appends arrayref param'
84 );
85
86 done_testing;
87