1 #!/usr/local/bin/perl -w
9 use Test::More tests => 86;
10 use CGI::Util qw(escape unescape);
11 use POSIX qw(strftime);
13 #-----------------------------------------------------------------------------
14 # make sure module loaded
15 #-----------------------------------------------------------------------------
17 BEGIN {use_ok('CGI::Cookie');}
20 'foo=123; bar=qwerty; baz=wibble; qux=a1',
21 'foo=123; bar=qwerty; baz=wibble;',
22 'foo=vixen; bar=cow; baz=bitch; qux=politician',
23 'foo=a%20phrase; bar=yes%2C%20a%20phrase; baz=%5Ewibble; qux=%27',
26 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
31 my $result = CGI::Cookie->parse($test_cookie[0]);
33 is(ref($result), 'HASH', "Hash ref returned in scalar context");
35 my @result = CGI::Cookie->parse($test_cookie[0]);
37 is(@result, 8, "returns correct number of fields");
39 @result = CGI::Cookie->parse($test_cookie[1]);
41 is(@result, 6, "returns correct number of fields");
43 my %result = CGI::Cookie->parse($test_cookie[0]);
45 is($result{foo}->value, '123', "cookie foo is correct");
46 is($result{bar}->value, 'qwerty', "cookie bar is correct");
47 is($result{baz}->value, 'wibble', "cookie baz is correct");
48 is($result{qux}->value, 'a1', "cookie qux is correct");
51 #-----------------------------------------------------------------------------
53 #-----------------------------------------------------------------------------
56 # make sure there are no cookies in the environment
57 delete $ENV{HTTP_COOKIE};
60 my %result = CGI::Cookie->fetch();
61 ok(keys %result == 0, "No cookies in environment, returns empty list");
63 # now set a cookie in the environment and try again
64 $ENV{HTTP_COOKIE} = $test_cookie[2];
65 %result = CGI::Cookie->fetch();
66 ok(eq_set([keys %result], [qw(foo bar baz qux)]),
67 "expected cookies extracted");
69 is(ref($result{foo}), 'CGI::Cookie', 'Type of objects returned is correct');
70 is($result{foo}->value, 'vixen', "cookie foo is correct");
71 is($result{bar}->value, 'cow', "cookie bar is correct");
72 is($result{baz}->value, 'bitch', "cookie baz is correct");
73 is($result{qux}->value, 'politician', "cookie qux is correct");
75 # Delete that and make sure it goes away
76 delete $ENV{HTTP_COOKIE};
77 %result = CGI::Cookie->fetch();
78 ok(keys %result == 0, "No cookies in environment, returns empty list");
80 # try another cookie in the other environment variable thats supposed to work
81 $ENV{COOKIE} = $test_cookie[3];
82 %result = CGI::Cookie->fetch();
83 ok(eq_set([keys %result], [qw(foo bar baz qux)]),
84 "expected cookies extracted");
86 is(ref($result{foo}), 'CGI::Cookie', 'Type of objects returned is correct');
87 is($result{foo}->value, 'a phrase', "cookie foo is correct");
88 is($result{bar}->value, 'yes, a phrase', "cookie bar is correct");
89 is($result{baz}->value, '^wibble', "cookie baz is correct");
90 is($result{qux}->value, "'", "cookie qux is correct");
93 #-----------------------------------------------------------------------------
95 #-----------------------------------------------------------------------------
98 # make sure there are no cookies in the environment
99 delete $ENV{HTTP_COOKIE};
102 my %result = CGI::Cookie->raw_fetch();
103 ok(keys %result == 0, "No cookies in environment, returns empty list");
105 # now set a cookie in the environment and try again
106 $ENV{HTTP_COOKIE} = $test_cookie[2];
107 %result = CGI::Cookie->raw_fetch();
108 ok(eq_set([keys %result], [qw(foo bar baz qux)]),
109 "expected cookies extracted");
111 is(ref($result{foo}), '', 'Plain scalar returned');
112 is($result{foo}, 'vixen', "cookie foo is correct");
113 is($result{bar}, 'cow', "cookie bar is correct");
114 is($result{baz}, 'bitch', "cookie baz is correct");
115 is($result{qux}, 'politician', "cookie qux is correct");
117 # Delete that and make sure it goes away
118 delete $ENV{HTTP_COOKIE};
119 %result = CGI::Cookie->raw_fetch();
120 ok(keys %result == 0, "No cookies in environment, returns empty list");
122 # try another cookie in the other environment variable thats supposed to work
123 $ENV{COOKIE} = $test_cookie[3];
124 %result = CGI::Cookie->raw_fetch();
125 ok(eq_set([keys %result], [qw(foo bar baz qux)]),
126 "expected cookies extracted");
128 is(ref($result{foo}), '', 'Plain scalar returned');
129 is($result{foo}, 'a%20phrase', "cookie foo is correct");
130 is($result{bar}, 'yes%2C%20a%20phrase', "cookie bar is correct");
131 is($result{baz}, '%5Ewibble', "cookie baz is correct");
132 is($result{qux}, '%27', "cookie qux is correct");
135 #-----------------------------------------------------------------------------
137 #-----------------------------------------------------------------------------
140 # Try new with full information provided
141 my $c = CGI::Cookie->new(-name => 'foo',
144 -domain => '.capricorn.com',
145 -path => '/cgi-bin/database',
148 is(ref($c), 'CGI::Cookie', 'new returns objects of correct type');
149 is($c->name , 'foo', 'name is correct');
150 is($c->value , 'bar', 'value is correct');
151 like($c->expires, '/^[a-z]{3},\s*\d{2}-[a-z]{3}-\d{4}/i', 'expires in correct format');
152 is($c->domain , '.capricorn.com', 'domain is correct');
153 is($c->path , '/cgi-bin/database', 'path is correct');
154 ok($c->secure , 'secure attribute is set');
156 # now try it with the only two manditory values (should also set the default path)
157 $c = CGI::Cookie->new(-name => 'baz',
160 is(ref($c), 'CGI::Cookie', 'new returns objects of correct type');
161 is($c->name , 'baz', 'name is correct');
162 is($c->value , 'qux', 'value is correct');
163 ok(!defined $c->expires, 'expires is not set');
164 ok(!defined $c->domain , 'domain attributeis not set');
165 is($c->path, '/', 'path atribute is set to default');
166 ok(!defined $c->secure , 'secure attribute is set');
168 # I'm really not happy about the restults of this section. You pass
169 # the new method invalid arguments and it just merilly creates a
171 # I've commented them out because they currently pass but I don't
172 # think they should. I think this is testing broken behaviour :-(
174 # # This shouldn't work
175 # $c = CGI::Cookie->new(-name => 'baz' );
177 # is(ref($c), 'CGI::Cookie', 'new returns objects of correct type');
178 # is($c->name , 'baz', 'name is correct');
179 # ok(!defined $c->value, "Value is undefined ");
180 # ok(!defined $c->expires, 'expires is not set');
181 # ok(!defined $c->domain , 'domain attributeis not set');
182 # is($c->path , '/', 'path atribute is set to default');
183 # ok(!defined $c->secure , 'secure attribute is set');
187 #-----------------------------------------------------------------------------
189 #-----------------------------------------------------------------------------
192 my $c = CGI::Cookie->new(-name => 'Jam',
195 -domain => '.pie-shop.com',
201 like($c->as_string, "/$name/", "Stringified cookie contains name");
203 my $value = $c->value;
204 like($c->as_string, "/$value/", "Stringified cookie contains value");
206 my $expires = $c->expires;
207 like($c->as_string, "/$expires/", "Stringified cookie contains expires");
209 my $domain = $c->domain;
210 like($c->as_string, "/$domain/", "Stringified cookie contains domain");
213 like($c->as_string, "/$path/", "Stringified cookie contains path");
215 like($c->as_string, '/secure/', "Stringified cookie contains secure");
217 $c = CGI::Cookie->new(-name => 'Hamster-Jam',
222 like($c->as_string, "/$name/", "Stringified cookie contains name");
225 like($c->as_string, "/$value/", "Stringified cookie contains value");
227 ok($c->as_string !~ /expires/, "Stringified cookie has no expires field");
229 ok($c->as_string !~ /domain/, "Stringified cookie has no domain field");
232 like($c->as_string, "/$path/", "Stringified cookie contains path");
234 ok($c->as_string !~ /secure/, "Stringified cookie does not contain secure");
237 #-----------------------------------------------------------------------------
239 #-----------------------------------------------------------------------------
242 my $c1 = CGI::Cookie->new(-name => 'Jam',
245 -domain => '.pie-shop.com',
250 # have to use $c1->expires because the time will occasionally be
251 # different between the two creates causing spurious failures.
252 my $c2 = CGI::Cookie->new(-name => 'Jam',
254 -expires => $c1->expires,
255 -domain => '.pie-shop.com',
260 # This looks titally whacked, but it does the -1, 0, 1 comparison
261 # thing so 0 means they match
262 is($c1->compare("$c1"), 0, "Cookies are identical");
263 is($c1->compare("$c2"), 0, "Cookies are identical");
265 $c1 = CGI::Cookie->new(-name => 'Jam',
267 -domain => '.foo.bar.com'
270 # have to use $c1->expires because the time will occasionally be
271 # different between the two creates causing spurious failures.
272 $c2 = CGI::Cookie->new(-name => 'Jam',
276 # This looks titally whacked, but it does the -1, 0, 1 comparison
277 # thing so 0 (i.e. false) means they match
278 is($c1->compare("$c1"), 0, "Cookies are identical");
279 ok($c1->compare("$c2"), "Cookies are not identical");
281 $c2->domain('.foo.bar.com');
282 is($c1->compare("$c2"), 0, "Cookies are identical");
285 #-----------------------------------------------------------------------------
286 # Test name, value, domain, secure, expires and path
287 #-----------------------------------------------------------------------------
290 my $c = CGI::Cookie->new(-name => 'Jam',
293 -domain => '.pie-shop.com',
298 is($c->name, 'Jam', 'name is correct');
299 is($c->name('Clash'), 'Clash', 'name is set correctly');
300 is($c->name, 'Clash', 'name now returns updated value');
302 # this is insane! it returns a simple scalar but can't accept one as
303 # an argument, you have to give it an arrary ref. It's totally
304 # inconsitent with these other methods :-(
305 is($c->value, 'Hamster', 'value is correct');
306 is($c->value(['Gerbil']), 'Gerbil', 'value is set correctly');
307 is($c->value, 'Gerbil', 'value now returns updated value');
309 my $exp = $c->expires;
310 like($c->expires, '/^[a-z]{3},\s*\d{2}-[a-z]{3}-\d{4}/i', 'expires is correct');
311 like($c->expires('+12h'), '/^[a-z]{3},\s*\d{2}-[a-z]{3}-\d{4}/i', 'expires is set correctly');
312 like($c->expires, '/^[a-z]{3},\s*\d{2}-[a-z]{3}-\d{4}/i', 'expires now returns updated value');
313 isnt($c->expires, $exp, "Expiry time has changed");
315 is($c->domain, '.pie-shop.com', 'domain is correct');
316 is($c->domain('.wibble.co.uk'), '.wibble.co.uk', 'domain is set correctly');
317 is($c->domain, '.wibble.co.uk', 'domain now returns updated value');
319 is($c->path, '/', 'path is correct');
320 is($c->path('/basket/'), '/basket/', 'path is set correctly');
321 is($c->path, '/basket/', 'path now returns updated value');
323 ok($c->secure, 'secure attribute is set');
324 ok(!$c->secure(0), 'secure attribute is cleared');
325 ok(!$c->secure, 'secure attribute is cleared');