| HTTP-WebTest documentation | Contained in the HTTP-WebTest distribution. |
HTTP::WebTest::Plugin::Cookies - Send and recieve cookies in tests
Not Applicable
This plugin provides means to control sending and recieve cookies in web test.
yes, no
yes
yes, no
yes
Copyright (c) 2000-2001 Richard Anderson. All rights reserved.
Copyright (c) 2001-2003 Ilya Martynov. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
HTTP::WebTest::API (HTTP::WebTest::API)
HTTP::WebTest::Plugins (HTTP::WebTest::Plugins)
| HTTP-WebTest documentation | Contained in the HTTP-WebTest distribution. |
# $Id: Cookies.pm,v 1.7 2003/03/02 11:52:09 m_ilya Exp $ package HTTP::WebTest::Plugin::Cookies;
use strict; use base qw(HTTP::WebTest::Plugin); use HTTP::Status;
sub param_types { return q(accept_cookies yesno send_cookies yesno cookie list cookies list); } use constant NCOOKIE_REFORMAT => 10; sub prepare_request { my $self = shift; $self->validate_params(qw(accept_cookies send_cookies cookies cookie)); my $accept_cookies = $self->yesno_test_param('accept_cookies', 1); my $send_cookies = $self->yesno_test_param('send_cookies', 1); my $cookies = $self->test_param('cookies'); $cookies ||= $self->test_param('cookie'); # alias for parameter $cookies = $self->transform_cookies($cookies) if defined $cookies; my $cookie_jar = $self->webtest->user_agent->cookie_jar; # configure cookie jar $cookie_jar->accept_cookies($accept_cookies); $cookie_jar->send_cookies($send_cookies); if(defined $cookies) { for my $cookie (@$cookies) { $cookie_jar->set_cookie(@$cookie); } } } sub check_response { my $self = shift; # we don't check here anything - just some clean up my $cookie_jar = $self->webtest->user_agent->cookie_jar; delete $cookie_jar->{accept_cookies}; delete $cookie_jar->{send_cookies}; return (); } # transform cookies to some canonic representation sub transform_cookies { my $self = shift; my $cookies = shift; # check if $cookies is array of arrays unless(ref($$cookies[0]) eq 'ARRAY') { return $self->transform_cookies([ $cookies ]); } my @cookies = (); for my $cookie (@$cookies) { # simple heuristic to distinguish deprecated format from new: # in new format $cookie->[0] cannot be a number while it is # expected for deprecated if($cookie->[0] =~ /^ \d* $/x) { $cookie = $self->transform_cookie_deprecated($cookie); } else { $cookie = $self->transform_cookie($cookie); } die "HTTP::WebTest: missing cookie name" unless defined $cookie->[1]; die "HTTP::WebTest: missing cookie path" unless defined $cookie->[3]; die "HTTP::WebTest: missing cookie domain" unless defined $cookie->[4]; push @cookies, $cookie; } return \@cookies; } # transform cookie to the canonic representation (a list expected by # HTTP::Cookie::set_cookie) sub transform_cookie { my $self = shift; my $cookie = shift; my %fields = ( version => 0, name => 1, value => 2, path => 3, domain => 4, port => 5, path_spec => 6, secure => 7, expires => 8, discard => 9, rest => 10 ); my @canonic = (); my %cookie = @$cookie; while(my($field, $value) = each %cookie) { $canonic[$fields{$field}] = $value; } # convert rest part from array ref to hash ref $canonic[10] = { @{$canonic[10]} } if defined $canonic[10]; return \@canonic; } # transform cookie specified using deprecated format to the canonic # representation (a list expected by HTTP::Cookie::set_cookie) sub transform_cookie_deprecated { my $self = shift; my $cookie = shift; # make a copy of cookie (missing fields are set to undef) my @canonic = @$cookie[0 .. NCOOKIE_REFORMAT - 1]; # replace '' with undef @canonic = map +(defined($_) and $_ eq '') ? (undef) : $_, @canonic; # collect all additional attributes (name, value pairs) my @extra = @$cookie[ NCOOKIE_REFORMAT .. @$cookie - 1]; push @canonic, { @extra }; return \@canonic; }
1;