diff --git a/lib/WebGUI/Test.pm b/lib/WebGUI/Test.pm index 2eb15b6ed..b708f39de 100644 --- a/lib/WebGUI/Test.pm +++ b/lib/WebGUI/Test.pm @@ -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. +Get the entire response from a page request using L. +This is a wrapper around L for the purpose of easing conversion of tests that use C. Accepts an L object as an argument, which cooked up auth info will be added to. An L will be constructed from a simple relative URL such as C if a string is passed instead. -Returns an L 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 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; + } #---------------------------------------------------------------------------- diff --git a/lib/WebGUI/Test/Mechanize.pm b/lib/WebGUI/Test/Mechanize.pm index 9330683e2..e6161d657 100644 --- a/lib/WebGUI/Test/Mechanize.pm +++ b/lib/WebGUI/Test/Mechanize.pm @@ -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; diff --git a/t/Asset/AssetExportHtml.t b/t/Asset/AssetExportHtml.t index 94914c6df..48ff437dc 100644 --- a/t/Asset/AssetExportHtml.t +++ b/t/Asset/AssetExportHtml.t @@ -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