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