7dc6308f8eb7393117ebb4a55643ea41b6ffe005
[liburi-template-perl.git] / t / 10-basic.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 25;
5
6 use_ok( 'URI::Template' );
7
8 # fatal - no template provided
9 {
10    eval { URI::Template->new; };
11    ok( $@ );
12 }
13
14 {
15     my $text     = 'http://foo.com/{bar}/{baz}?q=%7B';
16     my $template = URI::Template->new( $text );
17     isa_ok( $template, 'URI::Template' );
18     is_deeply( [ sort $template->variables ], [ qw( bar baz ) ], 'variables()' );
19     is( "$template", $text, 'as_string()' );
20
21     {
22         my $result = $template->process( bar => 'x', baz => 'y' );
23         is( $result, 'http://foo.com/x/y?q=%7B', 'process()' );
24         isa_ok( $result, 'URI', 'return value from process() isa URI' );
25     }
26     {
27         my $result = $template->process_to_string( bar => 'x', baz => 'y' );
28         is( $result, 'http://foo.com/x/y?q=%7B', 'process_to_string()' );
29         ok( !ref $result, 'result is not a ref' );
30     }
31 }
32
33 {
34     my $template = URI::Template->new( 'http://foo.com/{z(}/' );
35     my $result = $template->process( 'z(' => 'x' );
36     is( $result, 'http://foo.com/x/', 'potential regex issue escaped' );
37 }
38
39 {
40     my $template = URI::Template->new( 'http://foo.com/{z}/' );
41     {
42         my $result = $template->process( 'z' => '{x}' );
43         is( $result, 'http://foo.com/%7Bx%7D/', 'values are uri escaped' );
44     }
45     {
46         my $result = $template->process( );
47         is( $result, 'http://foo.com//', 'no value sent' );
48     }
49     {
50         my $result = $template->process( 'y' => '1' );
51         is( $result, 'http://foo.com//', 'no valid keys used' );
52     }
53 }
54
55 # test from spec
56 {
57     my %vals = (
58         a => 'fred',
59         b => 'barney',
60         c => 'cheeseburger',
61         d => 'one two three',
62         e => '20% tricky',
63         f => '',
64         20 => 'this-is-spinal-tap',
65         scheme => 'https',
66         p => 'quote=to+be+or+not+to+be',
67         q => 'hullo#world',
68     );
69
70     my @urls = (
71         [ (
72             'http://example.org/page1#{a}',
73             'http://example.org/page1#fred',
74         ) ],
75         [ (
76             'http://example.org/{a}/{b}/',
77             'http://example.org/fred/barney/',
78         ) ],
79         [ (
80             'http://example.org/{a}{b}/',
81             'http://example.org/fredbarney/',
82         ) ],
83         [ (
84             'http://example.com/order/{c}/{c}/{c}/',
85             'http://example.com/order/cheeseburger/cheeseburger/cheeseburger/',
86         ) ],
87         [ (
88             'http://example.org/{d}',
89             'http://example.org/one%20two%20three',
90         ) ],
91         [ (
92             'http://example.org/{e}',
93             'http://example.org/20%25%20tricky',
94         ) ],
95         [ (
96             'http://example.com/{f}/',
97             'http://example.com//',
98         ) ],
99         [ (
100             '{scheme}://{20}.example.org?date={wilma}&option={a}',
101             'https://this-is-spinal-tap.example.org?date=&option=fred',
102         ) ],
103         [ (
104             'http://example.org?{p}',
105             'http://example.org?quote=to+be+or+not+to+be',
106         ) ],
107         [ (
108             'http://example.com/{q}',
109             'http://example.com/hullo#world',
110         ) ],
111     );
112
113     for my $list ( @urls ) {
114         my $template = URI::Template->new( $list->[ 0 ] );
115         my $result = $template->process( %vals );
116         is( $result, $list->[ 1 ], 'escaped properly' );
117     }
118 }
119
120 {
121     my $template = URI::Template->new( 'http://foo.com/{z}/{z}/' );
122     is_deeply( [ $template->variables ], [ 'z' ], 'unique vars' );
123     my $result = $template->process( 'z' => 'x' );
124     is( $result, 'http://foo.com/x/x/', 'multiple replaces' );
125 }
126