Commit | Line | Data |
3c9e6294 |
1 | use strict; |
2 | use warnings; |
3 | |
7c1c4dc6 |
4 | use Test::More; |
3c9e6294 |
5 | use URI; |
7c1c4dc6 |
6 | use Catalyst::Log; |
3c9e6294 |
7 | |
8 | use_ok('Catalyst::Request'); |
9 | |
1e8c91ac |
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 | |
3c9e6294 |
22 | my $request = Catalyst::Request->new( { |
7c1c4dc6 |
23 | _log => Catalyst::Log->new, |
3c9e6294 |
24 | uri => URI->new('http://127.0.0.1/foo/bar/baz') |
25 | } ); |
26 | |
1e8c91ac |
27 | cmp_uri( |
3c9e6294 |
28 | $request->uri_with({}), |
29 | 'http://127.0.0.1/foo/bar/baz', |
30 | 'URI for absolute path' |
31 | ); |
32 | |
1e8c91ac |
33 | cmp_uri( |
3c9e6294 |
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( { |
7c1c4dc6 |
40 | _log => Catalyst::Log->new, |
3c9e6294 |
41 | uri => URI->new('http://127.0.0.1/foo/bar/baz?bar=gorch') |
42 | } ); |
1e8c91ac |
43 | |
44 | cmp_uri( |
3c9e6294 |
45 | $request2->uri_with({}), |
46 | 'http://127.0.0.1/foo/bar/baz?bar=gorch', |
47 | 'URI retains param' |
48 | ); |
49 | |
1e8c91ac |
50 | cmp_uri( |
3c9e6294 |
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 | |
1e8c91ac |
56 | cmp_uri( |
3c9e6294 |
57 | $request2->uri_with({ bar => undef }), |
58 | 'http://127.0.0.1/foo/bar/baz', |
59 | 'URI loses param when explicitly undef' |
60 | ); |
61 | |
1e8c91ac |
62 | cmp_uri( |
3c9e6294 |
63 | $request2->uri_with({ bar => 'snort' }), |
64 | 'http://127.0.0.1/foo/bar/baz?bar=snort', |
65 | 'URI changes param' |
66 | ); |
a375a206 |
67 | |
1e8c91ac |
68 | cmp_uri( |
a375a206 |
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 | |
1e8c91ac |
74 | cmp_uri( |
a375a206 |
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 | |
1e8c91ac |
80 | cmp_uri( |
a375a206 |
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 | |
7c1c4dc6 |
86 | done_testing; |
87 | |