Rework WebGUI::Test::getPage2 to be a wrapper around WebGUI::Test::Mechanize; this is still just

a crutch for converting tests, though really, it's enough shorter that it probably should live on.
getPage should be deprecated and replaced with getPage2 which should then be renamed.  Joy.
Anyway, WebGUI::Test::Mechanize had a bit of chicken-and-egg going on with not being able to
modify things in its session until after a request with a valid session cookie was made.
It's now more forgiving.
Reworked t/Asset/AssetExportHtml.t slightly to use getPage2 as it currently stands.
This commit is contained in:
Scott Walters 2011-05-11 15:26:32 -04:00
parent d2c8670098
commit 72bac90f93
3 changed files with 34 additions and 41 deletions

View file

@ -45,12 +45,8 @@ use Scope::Guard;
use WebGUI::Paths -inc;
use namespace::clean;
use WebGUI::User;
use Plack::Test;
use Plack::Util;
use WebGUI::Test::Mechanize;
use HTTP::Request::Common;
use WebGUI::GUID;
our @EXPORT = qw(cleanupGuard addToCleanup);
our @EXPORT_OK = qw(session config collateral);
@ -440,12 +436,14 @@ sub getPage {
=head2 getPage2 ( request|url [, opts] )
Get the entire response from a page request using L<Plack::Test>.
Get the entire response from a page request using L<WebGUI::Test::Mechanize>.
This is a wrapper around L<WebGUI::Test::Mechanize> for the purpose of easing conversion of tests that use C<getPage>.
Accepts an L<HTTP::Request> object as an argument, which cooked up auth info will be added to.
An L<HTTP::Request> will be constructed from a simple relative URL such as C<home> if a string is passed instead.
Returns an L<HTTP::Response> object on which you may call C<< decoded_content() >> to get the body content as a string.
Returns a string containing the body of the requested page.
C<options> is a hash reference of options with keys outlined below.
user => A user object to set for this request
@ -469,7 +467,6 @@ sub getPage2 {
# precedence
die "not supported" if exists $optionsRef->{args};
die "not supported" if exists $optionsRef->{formParams};
die "not supported" if exists $optionsRef->{uploads};
my $session = $CLASS->session;
@ -478,48 +475,39 @@ sub getPage2 {
my $oldUser = $session->user;
my $oldRequest = $session->request;
my $mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->file );
# Set the appropriate user
if ($optionsRef->{user}) {
$session->user({ user => $optionsRef->{user} });
$mech->session->user({ user => $optionsRef->{user} });
}
elsif ($optionsRef->{userId}) {
$session->user({ userId => $optionsRef->{userId} });
$mech->session->user({ userId => $optionsRef->{userId} });
}
$session->user->uncache;
# Fake up a logged in session
my $wgSession = WebGUI::GUID->generate;
$session->db->write(qq{
replace into userSession (sessionId, expires, lastPageView, userId)
values (?, ?, ?, ?)
}, [
$wgSession, scalar time + 60 * 20, scalar time, $session->user->userId,
] ) or die;
$mech->session or die; # force a session to be created for the request
my $session_id = $mech->sessionId or die;
my $response;
# Create a new request object, or fix up the one given to us
# Build or fix up a request object
if( ! eval { $request->isa('HTTP::Request') } ) {
if( $optionsRef->{formParams} ) {
$request = HTTP::Request::Common::POST( "http://127.0.0.1/$request", [ %{ $optionsRef->{formParams} } ] ) or die;
} else {
}
else {
$request = HTTP::Request->new( GET => "http://127.0.0.1/$request" ) or die;
}
}
$request->header( 'Set-Cookie3' => qq{wgSession=$wgSession; path="/"; domain=127.0.0.1; path_spec; discard; version=0} );
}
$request->header( 'Set-Cookie3' => qq{wgSession=$session_id; path="/"; domain=127.0.0.1; path_spec; discard; version=0} );
my $app = Plack::Util::load_psgi( WebGUI::Paths->defaultPSGI ) or die;
test_psgi $app, sub {
my $cb = shift;
$response = $cb->( $request );
};
$mech->request( $request );
# Restore the former user and request
$session->user({ user => $oldUser });
$session->{_request} = $oldRequest; # dubious about this; if we're going to try to support requests inside of other requests, it should be tested and actually supported rather than just some optimistic arm waving done
return $response;
return $mech->response->decoded_content;
}
#----------------------------------------------------------------------------

View file

@ -58,10 +58,15 @@ sub new {
sub session {
my $self = shift;
if( @_ ) {
$self->{_webgui_session} = shift; # take session as an arg
$self->{_webgui_sessionId} ||= $self->{_webgui_session}->getId;
}
return $self->{_webgui_session}
if $self->{_webgui_session};
my $session = WebGUI::Session->open($self->{_webgui_config}, undef, $self->sessionId);
my $session = WebGUI::Session->open($self->{_webgui_config}, undef, $self->sessionId) or die;
$self->{_webgui_session} = $session;
$self->{_webgui_sessionId} ||= $session->getId; # sessionId() sets it from
return $session;
}
@ -78,7 +83,9 @@ sub sessionId {
}
});
if (! $sessionId) {
die "Unable to find session cookie!";
# die "Unable to find session cookie!";
# when called from session() above, there is no session yet and no sessionId; that's okay
return; # empty list; make WebGUI::Session generate one for us
}
$self->{_webgui_sessionId} = $sessionId;
return $sessionId;

View file

@ -392,12 +392,10 @@ is($@, '', "exportWriteFile works when creating exportPath");
ok(-e $parent->exportGetUrlAsPath->absolute->stringify, "exportWriteFile actually writes the file when creating exportPath");
# now make sure that it contains the correct content
eval {
$content = WebGUI::Test->getPage2(
$parent->get('url').'?func=exportHtml_view',
{ user => WebGUI::User->new($session, 1) },
)->decoded_content
};
$content = WebGUI::Test->getPage2(
$parent->get('url').'?func=exportHtml_view',
{ user => WebGUI::User->new($session, 1) },
);
is(scalar $parent->exportGetUrlAsPath->slurp, $content, "exportWriteFile puts the correct contents in exported parent");
@ -449,7 +447,7 @@ is($@, '', "exportWriteFile works for first_child");
ok(-e $firstChild->exportGetUrlAsPath->absolute->stringify, "exportWriteFile actually writes the first_child file");
# verify it has the correct contents
eval { $content = WebGUI::Test->getPage2( $firstChild->get('url').'?func=exportHtml_view', )->decoded_content };
eval { $content = WebGUI::Test->getPage2( $firstChild->get('url').'?func=exportHtml_view', ) };
is(scalar $firstChild->exportGetUrlAsPath->absolute->slurp, $content, "exportWriteFile puts the correct contents in exported first_child");
# and one more level. remove the export path to ensure directory creation keeps
@ -466,7 +464,7 @@ ok(-e $grandChild->exportGetUrlAsPath->absolute->stringify, "exportWriteFile act
# finally, check its contents
$session->style->sent(0);
eval { $content = WebGUI::Test->getPage2( $grandChild->get('url').'?func=exportHtml_view', )->decoded_content };
eval { $content = WebGUI::Test->getPage2( $grandChild->get('url').'?func=exportHtml_view', ) };
is(scalar $grandChild->exportGetUrlAsPath->absolute->slurp, $content, "exportWriteFile puts correct content in exported grandchild");
# test different extensions