Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 181 lines (152 sloc) 4.025 kb
3ebfa076 »
2000-06-25 Initial revision
1 #============================================================= -*-perl-*-
2 #
3 # t/url.t
4 #
5 # Template script testing URL plugin.
6 #
82cbea87 »
2000-11-01 version 2.00
7 # Written by Andy Wardley <abw@kfs.org>
3ebfa076 »
2000-06-25 Initial revision
8 #
9 # Copyright (C) 2000 Andy Wardley. All Rights Reserved.
10 #
11 # This is free software; you can redistribute it and/or modify it
12 # under the same terms as Perl itself.
13 #
14 # $Id$
15 #
16 #========================================================================
17
18 use strict;
19 use lib qw( ../lib );
20 use Template qw( :status );
21 use Template::Test;
0aecf5b8 »
2002-06-13 * added couple more tests showing pre-defined URL's
22 use Template::Plugin::URL;
3ebfa076 »
2000-06-25 Initial revision
23 $^W = 1;
24
25 $Template::Test::DEBUG = 0;
26
0aecf5b8 »
2002-06-13 * added couple more tests showing pre-defined URL's
27 my $urls = {
28 product => {
29 map {
30 $_->{ name }, Template::Plugin::URL->new(undef, # no context
31 $_->{ url },
32 $_->{ args });
33 }
34 (
35 {
36 name => 'view',
37 url => '/product',
38 },
39 {
40 name => 'add',
41 url => '/product',
42 args => { action => 'add' },
43 },
44 {
45 name => 'edit',
46 url => '/product',
47 args => { action => 'edit', style => 'editor' },
48 },
49 ),
50 },
51 };
52
53 my $vars = {
54 url => $urls,
55 sorted => \&sort_params,
e7347adb »
2006-01-30 * added test for $JOINT
56 no_escape => sub { $Template::Plugin::URL::JOINT = '&' },
0aecf5b8 »
2002-06-13 * added couple more tests showing pre-defined URL's
57 };
58
59 test_expect(\*DATA, { INTERPOLATE => 1 }, $vars);
3ebfa076 »
2000-06-25 Initial revision
60
61 # url params are constructed in a non-deterministic order. we obviously
62 # can't test against this so we use this devious hack to reorder a
63 # query so that its parameters are in alphabetical order.
ac1af6e5 »
2002-01-10 * added test for multi-value params
64 # ------------------------------------------------------------------------
65 # later note: in adding support for parameters with multiple values, the
66 # sort_params() hacked below got broken so as a temporary solution, I
67 # changed teh URL plugin to sort all params by key when generating the
68 # URL
3ebfa076 »
2000-06-25 Initial revision
69
70 sub sort_params {
71 my $query = shift;
72 my ($base, $args) = split(/\?/, $query);
73 my (@args, @keys, %argtab);
74
75 print STDERR "sort_parms(\"$query\")\n" if $Template::Test::DEBUG;
76
867f44f2 »
2000-11-14 rc2
77 @args = split('&amp;', $args);
3ebfa076 »
2000-06-25 Initial revision
78 @keys = map { (split('=', $_))[0] } @args;
79 @argtab{ @keys } = @args;
80 @keys = sort keys %argtab;
81 @args = map { $argtab{ $_ } } @keys;
867f44f2 »
2000-11-14 rc2
82 $args = join('&amp;', @args);
3ebfa076 »
2000-06-25 Initial revision
83 $query = join('?', length $base ? ($base, $args) : $args);
84
85 print STDERR "returning [$query]\n" if $Template::Test::DEBUG;
86
87 return $query;
88 }
89
90
91 #------------------------------------------------------------------------
92 # test input
93 #------------------------------------------------------------------------
94
95 __DATA__
96 -- test --
97 [% USE url -%]
98 loaded
99 [% url %]
100 [% url('foo') %]
101 [% url(foo='bar') %]
102 [% url('bar', wiz='woz') %]
103
104 -- expect --
105 loaded
106
107 foo
108 foo=bar
109 bar?wiz=woz
110
111 -- test --
112 [% USE url('here') -%]
113 [% url %]
114 [% url('there') %]
115 [% url(any='where') %]
116 [% url('every', which='way') %]
117 [% sorted( url('every', which='way', you='can') ) %]
118
119 -- expect --
120 here
121 there
122 here?any=where
123 every?which=way
de70993b »
2006-01-05 * restored original (broken) behaviour using &amp; to join args
124 every?which=way&amp;you=can
3ebfa076 »
2000-06-25 Initial revision
125
126 -- test --
127 [% USE url('there', name='fred') -%]
128 [% url %]
129 [% url(name='tom') %]
130 [% sorted( url(age=24) ) %]
131 [% sorted( url(age=42, name='frank') ) %]
132
133 -- expect --
134 there?name=fred
135 there?name=tom
de70993b »
2006-01-05 * restored original (broken) behaviour using &amp; to join args
136 there?age=24&amp;name=fred
137 there?age=42&amp;name=frank
3ebfa076 »
2000-06-25 Initial revision
138
139 -- test --
140 [% USE url('/cgi-bin/woz.pl') -%]
141 [% url(name="Elrich von Benjy d'Weiro") %]
142
143 -- expect --
144 /cgi-bin/woz.pl?name=Elrich%20von%20Benjy%20d%27Weiro
145
ac1af6e5 »
2002-01-10 * added test for multi-value params
146 -- test --
147 [% USE url '/script' { one => 1, two => [ 2, 4 ], three => [ 3, 6, 9] } -%]
148 [% url %]
149
150 -- expect --
de70993b »
2006-01-05 * restored original (broken) behaviour using &amp; to join args
151 /script?one=1&amp;three=3&amp;three=6&amp;three=9&amp;two=2&amp;two=4
3ebfa076 »
2000-06-25 Initial revision
152
0aecf5b8 »
2002-06-13 * added couple more tests showing pre-defined URL's
153 -- test --
154 [% url.product.view %]
155 [% url.product.view(style='compact') %]
156 -- expect --
157 /product
158 /product?style=compact
159
160 -- test --
161 [% url.product.add %]
162 [% url.product.add(style='compact') %]
163 -- expect --
164 /product?action=add
de70993b »
2006-01-05 * restored original (broken) behaviour using &amp; to join args
165 /product?action=add&amp;style=compact
0aecf5b8 »
2002-06-13 * added couple more tests showing pre-defined URL's
166
167 -- test --
168 [% url.product.edit %]
169 [% url.product.edit(style='compact') %]
170 -- expect --
de70993b »
2006-01-05 * restored original (broken) behaviour using &amp; to join args
171 /product?action=edit&amp;style=editor
172 /product?action=edit&amp;style=compact
e7347adb »
2006-01-30 * added test for $JOINT
173
174 -- test --
175 [% CALL no_escape -%]
176 [% url.product.edit %]
177 [% url.product.edit(style='compact') %]
178 -- expect --
179 /product?action=edit&style=editor
180 /product?action=edit&style=compact
Something went wrong with that request. Please try again.