4 use Test::More tests => 25;
6 use_ok( 'URI::Template' );
8 # fatal - no template provided
10 eval { URI::Template->new; };
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()' );
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' );
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' );
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' );
40 my $template = URI::Template->new( 'http://foo.com/{z}/' );
42 my $result = $template->process( 'z' => '{x}' );
43 is( $result, 'http://foo.com/%7Bx%7D/', 'values are uri escaped' );
46 my $result = $template->process( );
47 is( $result, 'http://foo.com//', 'no value sent' );
50 my $result = $template->process( 'y' => '1' );
51 is( $result, 'http://foo.com//', 'no valid keys used' );
64 20 => 'this-is-spinal-tap',
66 p => 'quote=to+be+or+not+to+be',
72 'http://example.org/page1#{a}',
73 'http://example.org/page1#fred',
76 'http://example.org/{a}/{b}/',
77 'http://example.org/fred/barney/',
80 'http://example.org/{a}{b}/',
81 'http://example.org/fredbarney/',
84 'http://example.com/order/{c}/{c}/{c}/',
85 'http://example.com/order/cheeseburger/cheeseburger/cheeseburger/',
88 'http://example.org/{d}',
89 'http://example.org/one%20two%20three',
92 'http://example.org/{e}',
93 'http://example.org/20%25%20tricky',
96 'http://example.com/{f}/',
97 'http://example.com//',
100 '{scheme}://{20}.example.org?date={wilma}&option={a}',
101 'https://this-is-spinal-tap.example.org?date=&option=fred',
104 'http://example.org?{p}',
105 'http://example.org?quote=to+be+or+not+to+be',
108 'http://example.com/{q}',
109 'http://example.com/hullo#world',
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' );
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' );