Files
oddmuse/t/cookie.t
Alex Schroeder 79610f7490 cookie.t: fix tests
When we decided to no longer show changes to the cookie, we knew that we
would have to change some of the tests. This commit does that for
cookie.t. All the tests that looked at the HTML document now look at the
HTTP response headers.
2015-08-15 16:05:16 +02:00

89 lines
3.7 KiB
Perl
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Copyright (C) 20092015 Alex Schroeder <alex@gnu.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 't/test.pl';
package OddMuse;
use Test::More tests => 18;
use utf8;
# Basic usage that broke when I last changed the cookie handling code.
test_page(get_page('username=Alex'), 'Status: 404');
test_page(get_page('action=browse id=Alex'), 'Alex');
# Username
test_page(get_page('action=browse id=HomePage username=Alex'), "\nSet-Cookie: Wiki=username%251eAlex");
test_page(get_page('action=browse id=HomePage username=01234567890123456789012345678901234567890123456789'),
"\nSet-Cookie: Wiki=username%251e01234567890123456789012345678901234567890123456789");
test_page(get_page('action=browse id=HomePage username=01234567890123456789012345678901234567890123456789X'),
'UserName must be 50 characters or less: not saved');
test_page(get_page('action=browse id=HomePage username=AlexSchroeder'),
"\nSet-Cookie: Wiki=username%251eAlexSchroeder");
test_page(get_page('action=browse id=HomePage username=Alex%20Schroeder'),
"\nSet-Cookie: Wiki=username%251eAlex%20Schroeder");
AppendStringToFile($ConfigFile, "\$FreeLinks = 0;\n");
AppendStringToFile($ConfigFile, "\$WikiLinks = 1;\n");
test_page(get_page('action=browse id=HomePage username=Alex%20Schroeder'),
'Invalid UserName Alex Schroeder: not saved');
test_page(get_page('action=browse id=HomePage username=AlexSchroeder'),
"\nSet-Cookie: Wiki=username%251eAlexSchroeder");
test_page(get_page('action=browse id=HomePage username=Alex'),
'Invalid UserName Alex: not saved');
# single words are ok if we switch off $WikiLinks as well!
AppendStringToFile($ConfigFile, "\$WikiLinks = 0;\n");
test_page(get_page('action=browse id=HomePage username=Alex'),
"\nSet-Cookie: Wiki=username%251eAlex");
SKIP: {
eval { require LWP::UserAgent; };
skip "LWP::UserAgent not installed", 7 if $@;
eval { require HTTP::Cookies; };
skip "HTTP::Cookies not installed", 7 if $@;
my $wiki = 'http://127.0.0.1/cgi-bin/wiki.pl';
my $ua = LWP::UserAgent->new;
my $response = $ua->get("$wiki?action=version");
skip("No wiki running at $wiki", 7)
unless $response->is_success;
$ua = LWP::UserAgent->new;
my $cookie = HTTP::Cookies->new;
$ua ->cookie_jar($cookie);
# Set the cookie
$response = $ua->get("$wiki?action=debug;pwd=foo");
ok($response->is_success, 'request the page');
$ua->cookie_jar->as_string =~ /Set-Cookie.*: ([^=]+)=pwd%251efoo/;
my $cookie = $1;
ok($cookie, 'pwd was set in the cookie');
test_page_negative($response->content, 'pwd');
# Change the cookie
$response = $ua->get("$wiki?action=debug;pwd=test");
test_page($ua->cookie_jar->as_string, qq{Set-Cookie.*: $cookie=pwd%251etest});
# Delete the cookie
$response = $ua->get("$wiki?action=debug;pwd=");
test_page($ua->cookie_jar->as_string, qq{Set-Cookie.*: $cookie=""});
# Encoding issues
$response = $ua->get("$wiki?action=rc;username=Alex\%20Schr\%C3\%B6der");
test_page($response->header('Set-Cookie'),
qq{^Wiki=username%251eAlex%20Schr%C3%B6der});
test_page($ua->cookie_jar->as_string,
qq{Set-Cookie.*: $cookie=username%251eAlex%20Schr%C3%B6der});
};