Merge branch 'psgi' into WebGUI8
This commit is contained in:
commit
89d4f46a18
94 changed files with 2002 additions and 2269 deletions
|
|
@ -49,7 +49,7 @@ ok($output =~ m/true/, "process() - conditionals");
|
|||
ok($output =~ m/\b(?:XY){5}\b/, "process() - loops");
|
||||
|
||||
# See if template listens the Accept header
|
||||
$session->request->headers_in->{Accept} = 'application/json';
|
||||
$session->request->header('Accept' => 'application/json');
|
||||
|
||||
my $json = $template->process(\%var);
|
||||
my $andNowItsAPerlHashRef = eval { from_json( $json ) };
|
||||
|
|
|
|||
34
t/Auth.t
34
t/Auth.t
|
|
@ -38,52 +38,40 @@ plan tests => 3; # Increment this number for each test you create
|
|||
#----------------------------------------------------------------------------
|
||||
# Test createAccountSave and returnUrl together
|
||||
# Set up request
|
||||
$oldRequest = $session->request;
|
||||
$request = WebGUI::PseudoRequest->new;
|
||||
$request->setup_param({
|
||||
my $createAccountSession = WebGUI::Test->newSession(0, {
|
||||
returnUrl => 'REDIRECT_URL',
|
||||
});
|
||||
$session->{_request} = $request;
|
||||
|
||||
$auth = WebGUI::Auth->new( $session, $AUTH_METHOD );
|
||||
my $username = $session->id->generate;
|
||||
$auth = WebGUI::Auth->new( $createAccountSession, $AUTH_METHOD );
|
||||
my $username = $createAccountSession->id->generate;
|
||||
push @cleanupUsernames, $username;
|
||||
$output = $auth->createAccountSave( $username, { }, "PASSWORD" );
|
||||
$output = $auth->createAccountSave( $username, { }, "PASSWORD" );
|
||||
|
||||
is(
|
||||
$session->http->getRedirectLocation, 'REDIRECT_URL',
|
||||
$createAccountSession->http->getRedirectLocation, 'REDIRECT_URL',
|
||||
"returnUrl field is used to set redirect after createAccountSave",
|
||||
);
|
||||
|
||||
# Session Cleanup
|
||||
$session->{_request} = $oldRequest;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Test login and returnUrl together
|
||||
# Set up request
|
||||
$oldRequest = $session->request;
|
||||
$request = WebGUI::PseudoRequest->new;
|
||||
$request->setup_param({
|
||||
|
||||
my $loginSession = WebGUI::Test->newSession(0, {
|
||||
returnUrl => 'REDIRECT_LOGIN_URL',
|
||||
});
|
||||
$session->{_request} = $request;
|
||||
|
||||
$auth = WebGUI::Auth->new( $session, $AUTH_METHOD, 3 );
|
||||
my $username = $session->id->generate;
|
||||
$auth = WebGUI::Auth->new( $loginSession, $AUTH_METHOD, 3 );
|
||||
my $username = $loginSession->id->generate;
|
||||
push @cleanupUsernames, $username;
|
||||
$session->setting->set('showMessageOnLogin', 0);
|
||||
$output = $auth->login;
|
||||
$output = $auth->login;
|
||||
|
||||
is(
|
||||
$session->http->getRedirectLocation, 'REDIRECT_LOGIN_URL',
|
||||
$loginSession->http->getRedirectLocation, 'REDIRECT_LOGIN_URL',
|
||||
"returnUrl field is used to set redirect after login",
|
||||
);
|
||||
is $output, undef, 'login returns undef when showMessageOnLogin is false';
|
||||
|
||||
# Session Cleanup
|
||||
$session->{_request} = $oldRequest;
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
|
|
|
|||
85
t/Exception/app.t
Normal file
85
t/Exception/app.t
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
# Test what happens when the WebGUI PSGI app throws exceptions
|
||||
use strict;
|
||||
use FindBin;
|
||||
use lib "$FindBin::Bin/../../lib";
|
||||
use WebGUI;
|
||||
use Plack::Test;
|
||||
use Plack::Builder;
|
||||
use HTTP::Request::Common;
|
||||
use Test::More tests => 9;
|
||||
use HTTP::Exception;
|
||||
|
||||
my $wg = WebGUI->new;
|
||||
|
||||
my $regular_app = builder {
|
||||
enable '+WebGUI::Middleware::Session', config => $wg->config;
|
||||
$wg;
|
||||
};
|
||||
|
||||
my $generic_dead_app = builder {
|
||||
enable '+WebGUI::Middleware::Session', config => $wg->config;
|
||||
|
||||
# Pretend that WebGUI dies during request handling
|
||||
sub { die 'WebGUI died' }
|
||||
};
|
||||
|
||||
my $specific_dead_app = builder {
|
||||
enable '+WebGUI::Middleware::Session', config => $wg->config;
|
||||
|
||||
# Pretend that WebGUI throws a '501 - Not Implemented' HTTP error
|
||||
sub { HTTP::Exception::501->throw }
|
||||
};
|
||||
|
||||
my $fatal_app = builder {
|
||||
enable '+WebGUI::Middleware::Session', config => $wg->config;
|
||||
|
||||
# Pretend that WebGUI calls $session->log->fatal during request handling
|
||||
sub {
|
||||
my $env = shift;
|
||||
|
||||
$env->{'webgui.session'}->log->fatal('Fatally yours');
|
||||
}
|
||||
};
|
||||
|
||||
test_psgi $regular_app, sub {
|
||||
my $cb = shift;
|
||||
my $res = $cb->( GET "/" );
|
||||
like $res->content, qr/My Company/;
|
||||
};
|
||||
|
||||
# N.B. The die() is caught thanks to WebGUI::Middleware::HTTPExceptions,
|
||||
# but generates a warning to STDOUT - should perhaps be silenced?
|
||||
test_psgi $generic_dead_app, sub {
|
||||
my $cb = shift;
|
||||
my $res = $cb->( GET "/" );
|
||||
is $res->code, 500;
|
||||
is $res->content, 'Internal Server Error';
|
||||
};
|
||||
|
||||
test_psgi $specific_dead_app, sub {
|
||||
my $cb = shift;
|
||||
my $res = $cb->( GET "/" );
|
||||
is $res->code, 501;
|
||||
is $res->content, 'Not Implemented'; # how apt
|
||||
};
|
||||
|
||||
test_psgi $fatal_app, sub {
|
||||
my $cb = shift;
|
||||
my $res = $cb->( GET "/" );
|
||||
is $res->code, 500;
|
||||
|
||||
# WebGUI doesn't know who you are, so it displays the generic error page
|
||||
like $res->content, qr/Problem With Request/;
|
||||
};
|
||||
|
||||
test_psgi $fatal_app, sub {
|
||||
my $cb = shift;
|
||||
|
||||
local *WebGUI::Session::ErrorHandler::canShowDebug = sub {1};
|
||||
my $res = $cb->( GET "/" );
|
||||
is $res->code, 500;
|
||||
|
||||
# We canShowDebug, so WebGUI gives us more info
|
||||
like $res->content, qr/Fatally yours/;
|
||||
};
|
||||
|
||||
23
t/PSGI/default-site.t
Normal file
23
t/PSGI/default-site.t
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use Test::More tests => 4;
|
||||
|
||||
use Plack::Test;
|
||||
use Plack::Util;
|
||||
use HTTP::Request::Common;
|
||||
use WebGUI::Paths;
|
||||
|
||||
my $app = Plack::Util::load_psgi( WebGUI::Paths->defaultPSGI );
|
||||
|
||||
test_psgi $app, sub {
|
||||
my $cb = shift;
|
||||
|
||||
my $res = $cb->( GET "/" );
|
||||
is $res->code, 200;
|
||||
like $res->content, qr/My Company/;
|
||||
|
||||
$res = $cb->( GET "/?op=editSettings" );
|
||||
is $res->code, 401;
|
||||
like $res->content, qr/Administrative Function/;
|
||||
|
||||
};
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
|
||||
#-------------------------------------------------------------------
|
||||
# Please read the legal notices (docs/legal.txt) and the license
|
||||
# (docs/license.txt) that came with this distribution before using
|
||||
# this software.
|
||||
#-------------------------------------------------------------------
|
||||
# http://www.plainblack.com info@plainblack.com
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
use FindBin;
|
||||
use strict;
|
||||
use lib "$FindBin::Bin/../lib";
|
||||
|
||||
use WebGUI::Test;
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Session::Os;
|
||||
|
||||
my @testSets = (
|
||||
{
|
||||
os => 'Win',
|
||||
type => 'Windowsish',
|
||||
},
|
||||
{
|
||||
os => 'win32',
|
||||
type => 'Windowsish',
|
||||
},
|
||||
{
|
||||
os => 'MSWin32',
|
||||
type => 'Windowsish',
|
||||
},
|
||||
{
|
||||
os => 'Amiga OS',
|
||||
type => 'Linuxish',
|
||||
},
|
||||
);
|
||||
|
||||
use Test::More;
|
||||
|
||||
my $numTests = 2 * scalar @testSets;
|
||||
|
||||
plan tests => $numTests;
|
||||
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
foreach my $test (@testSets) {
|
||||
local $^O = $test->{os};
|
||||
my $os = WebGUI::Session::Os->new($session);
|
||||
is($os->get('name'), $test->{os}, "$test->{os}: name set");
|
||||
is($os->get('type'), $test->{type}, "$test->{os}: type set");
|
||||
}
|
||||
|
||||
|
|
@ -13,7 +13,6 @@ use strict;
|
|||
use lib "$FindBin::Bin/../lib";
|
||||
|
||||
use WebGUI::Test;
|
||||
use WebGUI::PseudoRequest;
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Asset;
|
||||
|
||||
|
|
@ -51,13 +50,10 @@ my @getRefererUrlTests = (
|
|||
);
|
||||
|
||||
use Test::More;
|
||||
use Test::MockObject::Extends;
|
||||
plan tests => 81 + scalar(@getRefererUrlTests);
|
||||
plan tests => 79 + scalar(@getRefererUrlTests);
|
||||
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
my $pseudoRequest = WebGUI::PseudoRequest->new();
|
||||
$session->{_request} = $pseudoRequest;
|
||||
my $request = $session->request;
|
||||
|
||||
#disable caching
|
||||
my $preventProxyCache = $session->setting->get('preventProxyCache');
|
||||
|
|
@ -140,17 +136,14 @@ $session->url->setSiteURL('http://webgui.org');
|
|||
is( $session->url->getSiteURL, 'http://webgui.org', 'override config setting with setSiteURL');
|
||||
|
||||
##Create a fake environment hash so we can muck with it.
|
||||
my %mockEnv = %ENV;
|
||||
my $env = $session->env;
|
||||
$env = Test::MockObject::Extends->new($env);
|
||||
$env->mock('get', sub { return $mockEnv{$_[1]} } );
|
||||
my $env = $session->request->env;
|
||||
|
||||
$mockEnv{HTTPS} = "on";
|
||||
$env->{'psgi.url_scheme'} = "https";
|
||||
$session->url->setSiteURL(undef);
|
||||
is( $session->url->getSiteURL, 'https://'.$sitename, 'getSiteURL from config as http_host with SSL');
|
||||
|
||||
$mockEnv{HTTPS} = "";
|
||||
$mockEnv{HTTP_HOST} = "devsite.com";
|
||||
$env->{'psgi.url_scheme'} = "http";
|
||||
$env->{HTTP_HOST} = "devsite.com";
|
||||
$session->url->setSiteURL(undef);
|
||||
is( $session->url->getSiteURL, 'http://'.$sitename, 'getSiteURL where requested host is not a configured site');
|
||||
|
||||
|
|
@ -194,26 +187,29 @@ is( $session->url->makeCompliant($url), $url2, 'language specific URL compliance
|
|||
#
|
||||
#######################################
|
||||
|
||||
my $originalRequest = $session->request; ##Save the original request object
|
||||
my $setUri = sub {
|
||||
$request->env->{PATH_INFO} = $_[0];
|
||||
};
|
||||
$session->{_request} = undef;
|
||||
|
||||
is($session->url->getRequestedUrl, undef, 'getRequestedUrl returns undef unless it has a request object');
|
||||
$session->{_request} = $originalRequest;
|
||||
|
||||
$pseudoRequest->uri('empty');
|
||||
is($session->request->uri, 'empty', 'Validate Mock Object operation');
|
||||
$session->{_request} = $request;
|
||||
|
||||
$pseudoRequest->uri('full');
|
||||
is($session->request->uri, 'full', 'Validate Mock Object operation #2');
|
||||
$setUri->('empty');
|
||||
is($session->request->uri, 'http://devsite.com/empty', 'Validate Mock Object operation');
|
||||
|
||||
$pseudoRequest->uri('/path1/file1');
|
||||
$setUri->('full');
|
||||
is($session->request->uri, 'http://devsite.com/full', 'Validate Mock Object operation #2');
|
||||
|
||||
$setUri->('/path1/file1');
|
||||
is($session->url->getRequestedUrl, 'path1/file1', 'getRequestedUrl, fetch');
|
||||
|
||||
$pseudoRequest->uri('/path2/file2');
|
||||
$setUri->('/path2/file2');
|
||||
is($session->url->getRequestedUrl, 'path1/file1', 'getRequestedUrl, check cache of previous result');
|
||||
|
||||
$session->url->{_requestedUrl} = undef; ##Manually clear cached value
|
||||
$pseudoRequest->uri('/path2/file2?param1=one;param2=two');
|
||||
$setUri->('/path2/file2?param1=one;param2=two');
|
||||
is($session->url->getRequestedUrl, 'path2/file2', 'getRequestedUrl, does not return params');
|
||||
|
||||
#######################################
|
||||
|
|
@ -226,7 +222,7 @@ my $sessionAsset = $session->asset;
|
|||
$session->asset(undef);
|
||||
|
||||
$session->url->{_requestedUrl} = undef; ##Manually clear cached value
|
||||
$pseudoRequest->uri('/path1/">file1');
|
||||
$setUri->('/path1/">file1');
|
||||
is($session->url->page, '/path1/%22%3Efile1', 'page with no args returns getRequestedUrl through gateway, escaping the requested URL for safety');
|
||||
|
||||
is($session->url->page('op=viewHelpTOC;topic=Article'), '/path1/%22%3Efile1?op=viewHelpTOC;topic=Article', 'page: pairs are appended');
|
||||
|
|
@ -256,12 +252,12 @@ $session->asset($sessionAsset);
|
|||
#
|
||||
#######################################
|
||||
|
||||
$mockEnv{'HTTP_REFERER'} = 'test';
|
||||
$env->{'HTTP_REFERER'} = 'test';
|
||||
|
||||
is($session->env->get('HTTP_REFERER'), 'test', 'testing overridden ENV');
|
||||
|
||||
foreach my $test (@getRefererUrlTests) {
|
||||
$mockEnv{HTTP_REFERER} = $test->{input};
|
||||
$env->{HTTP_REFERER} = $test->{input};
|
||||
is($session->url->getRefererUrl, $test->{output}, $test->{comment});
|
||||
}
|
||||
|
||||
|
|
@ -321,14 +317,10 @@ is($session->url->extras('/dir1/foo.html'), join('', $cdnCfg->{extrasCdn}, 'dir1
|
|||
is($session->url->extras('tinymce'), join('', $extras, 'tinymce'),
|
||||
'extras exclusion from CDN');
|
||||
# Note: env is already mocked above.
|
||||
$mockEnv{HTTPS} = 'on';
|
||||
$env->{'psgi.url_scheme'} = "https";
|
||||
is($session->url->extras('/dir1/foo.html'), join('', $cdnCfg->{extrasSsl}, 'dir1/foo.html'),
|
||||
'extras using extrasSsl with HTTPS');
|
||||
$mockEnv{HTTPS} = undef;
|
||||
$mockEnv{SSLPROXY} = 1;
|
||||
is($session->url->extras('/dir1/foo.html'), join('', $cdnCfg->{extrasSsl}, 'dir1/foo.html'),
|
||||
'extras using extrasSsl with SSLPROXY');
|
||||
delete $mockEnv{SSLPROXY};
|
||||
$env->{'psgi.url_scheme'} = "http";
|
||||
|
||||
$session->config->set('extrasURL', $origExtras);
|
||||
|
||||
|
|
@ -376,7 +368,7 @@ is($session->url->urlize('home/././here'), 'home/here', '... removes
|
|||
$sessionAsset = $session->asset;
|
||||
$session->{_asset} = undef;
|
||||
$session->url->{_requestedUrl} = undef; ##Manually clear cached value
|
||||
$pseudoRequest->uri('/goBackToTheSite');
|
||||
$setUri->('/goBackToTheSite');
|
||||
|
||||
is($session->url->getBackToSiteURL, '/goBackToTheSite', 'getBackToSiteURL: when session asset is undefined, the method falls back to using page');
|
||||
|
||||
|
|
@ -449,19 +441,12 @@ my $origSSLEnabled = $session->config->get('sslEnabled');
|
|||
##Test all the false cases, first
|
||||
|
||||
$session->config->set('sslEnabled', 0);
|
||||
$mockEnv{HTTPS} = 'not on';
|
||||
$mockEnv{SSLPROXY} = 0;
|
||||
$env->{'psgi.url_scheme'} = "http";
|
||||
ok( ! $session->url->forceSecureConnection(), 'sslEnabled must be 1 to force SSL');
|
||||
|
||||
$session->config->set('sslEnabled', 1);
|
||||
$mockEnv{HTTPS} = 'on';
|
||||
$mockEnv{SSLPROXY} = 0;
|
||||
$env->{'psgi.url_scheme'} = "https";
|
||||
ok( ! $session->url->forceSecureConnection(), 'HTTPS must not be "on" to force SSL');
|
||||
|
||||
$session->config->set('sslEnabled', 1);
|
||||
$mockEnv{HTTPS} = 'not on';
|
||||
$mockEnv{SSLPROXY} = 1;
|
||||
ok( ! $session->url->forceSecureConnection(), 'SSLPROXY must not be true to force SSL');
|
||||
ok( ! $session->url->forceSecureConnection('/test/url'), 'all conditions must be met, even if a URL is directly passed in');
|
||||
|
||||
##Validate the HTTP object state before we start
|
||||
|
|
@ -469,8 +454,7 @@ $session->http->setStatus('200', 'OK');
|
|||
is($session->http->getStatus, 200, 'http status is okay, 200');
|
||||
is($session->http->getRedirectLocation, undef, 'redirect location is empty');
|
||||
|
||||
$mockEnv{HTTPS} = 'not on';
|
||||
$mockEnv{SSLPROXY} = 0;
|
||||
$env->{'psgi.url_scheme'} = "http";
|
||||
|
||||
my $secureUrl = $session->url->getSiteURL . '/foo/bar/baz/buz';
|
||||
$secureUrl =~ s/http:/https:/;
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ END {
|
|||
|
||||
#----------------------------------------------------------------------------
|
||||
|
||||
=head2 newSession ( $noCleanup )
|
||||
=head2 newSession ( $noCleanup, [ $request ] )
|
||||
|
||||
Builds a WebGUI session object for testing.
|
||||
|
||||
|
|
@ -144,22 +144,60 @@ Builds a WebGUI session object for testing.
|
|||
|
||||
If true, the session won't be registered for automatic deletion.
|
||||
|
||||
=head3 $request
|
||||
|
||||
Either a HTTP::Request object to use for this session, or a hash ref of form parameters.
|
||||
|
||||
=cut
|
||||
|
||||
sub newSession {
|
||||
shift
|
||||
if eval { $_[0]->isa($CLASS) };
|
||||
my $noCleanup = shift;
|
||||
my $pseudoRequest = WebGUI::PseudoRequest->new;
|
||||
my $request = shift;
|
||||
require WebGUI::Session;
|
||||
my $session = WebGUI::Session->open( $CLASS->config );
|
||||
$session->{_request} = $pseudoRequest;
|
||||
my $session = WebGUI::Session->open( $CLASS->config, newEnv( $request ) );
|
||||
if ( ! $noCleanup ) {
|
||||
$CLASS->addToCleanup($session);
|
||||
}
|
||||
return $session;
|
||||
}
|
||||
|
||||
sub newEnv {
|
||||
shift
|
||||
if eval { $_[0]->isa($CLASS) };
|
||||
my $form = shift;
|
||||
|
||||
require HTTP::Message::PSGI;
|
||||
require HTTP::Request::Common;
|
||||
my $config = $CLASS->config;
|
||||
my $request;
|
||||
if ( try { $form->isa('HTTP::Request') } ) {
|
||||
$request = $form;
|
||||
}
|
||||
else {
|
||||
my $url = 'http://' . $config->get('sitename')->[0];
|
||||
$request = $form
|
||||
? HTTP::Request::Common::POST( $url, [ %$form ] )
|
||||
: HTTP::Request::Common::GET( $url )
|
||||
;
|
||||
}
|
||||
return $request->to_psgi;
|
||||
}
|
||||
|
||||
sub clientTest (&) {
|
||||
my $client = shift;
|
||||
local $ENV{WEBGUI_CONFIG} = $CLASS->file;
|
||||
my $test_psgi = Plack::Util::load_psgi(
|
||||
$CLASS->config->get('psgiFile')
|
||||
|| WebGUI::Paths->defaultPSGI,
|
||||
);
|
||||
Plack::Test::test_psgi(
|
||||
app => $test_psgi,
|
||||
client => $client,
|
||||
);
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
||||
=head2 interceptLogging
|
||||
|
|
@ -265,6 +303,9 @@ below.
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
# I think that getPage should be entirely replaced with calles to Plack::Test::test_psgi
|
||||
# - testing with the callback is better and it means we can run on any backend
|
||||
sub getPage {
|
||||
my $class = shift;
|
||||
my $actor = shift; # The actor to work on
|
||||
|
|
@ -288,9 +329,10 @@ sub getPage {
|
|||
|
||||
# Create a new request object
|
||||
my $oldRequest = $session->request;
|
||||
my $request = WebGUI::PseudoRequest->new;
|
||||
$request->setup_param($optionsRef->{formParams});
|
||||
my $request = WebGUI::Session::Request->new(newEnv($optionsRef->{formParams}));
|
||||
# $request->setup_param($optionsRef->{formParams});
|
||||
local $session->{_request} = $request;
|
||||
local $session->{_response} = $request->new_response( 200 );
|
||||
local $session->output->{_handle};
|
||||
|
||||
# Fill the buffer
|
||||
|
|
@ -315,7 +357,7 @@ sub getPage {
|
|||
$session->user({ user => $oldUser });
|
||||
|
||||
# Return the page's output
|
||||
return $request->get_output;
|
||||
return join '', @{$session->response->body};
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
@ -516,7 +558,7 @@ Example call:
|
|||
( $sql, @params ) = @$sql;
|
||||
}
|
||||
return sub {
|
||||
$db->write( $sql, {}, @params );
|
||||
$db->do( $sql, {}, @params );
|
||||
}
|
||||
},
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue