diff --git a/lib/WebGUI/Session/Url.pm b/lib/WebGUI/Session/Url.pm index dc36fe8dc..0b03a2deb 100644 --- a/lib/WebGUI/Session/Url.pm +++ b/lib/WebGUI/Session/Url.pm @@ -158,15 +158,22 @@ Name value pairs to add to the URL in the form of: name1=value1;name2=value2;name3=value3 +=head3 skipPreventProxyCache + +If preventing proxy caching is enabled in the settings, then if +skipPreventProxyCache is a 1 will override that and prevent the +noCache param from being added to the URL. + =cut sub gateway { my $self = shift; my $pageUrl = shift; my $pairs = shift; + my $skipPreventProxyCache = shift; my $url = $self->session->config->get("gateway").'/'.$pageUrl; $url =~ s/\/+/\//g; - if ($self->session->setting->get("preventProxyCache") == 1) { + if ($self->session->setting->get("preventProxyCache") == 1 and !$skipPreventProxyCache) { $url = $self->append($url,"noCache=".randint(0,1000).','.$self->session->datetime->time()); } if ($pairs) { @@ -267,7 +274,8 @@ The base URL to use. This defaults to current page url. sub makeAbsolute { my $self = shift; my $url = shift; - my $baseURL = shift || $self->page(); + my $baseURL = shift; + $baseURL = $self->page() unless $baseURL; return URI->new_abs($url,$baseURL); } @@ -326,9 +334,10 @@ If set to "1" we'll use the full site URL rather than the script (gateway) URL. =head3 skipPreventProxyCache -If preventing proxy caching is enabled in the settings, then if skipPreventProxyCache -is a 1 it will prevent the code that prevents proxy caching. If that doesn't make -your head hurt then you'll understand the rest of wG just fine. +If preventing proxy caching is enabled in the settings, then if +skipPreventProxyCache is a 1 it will prevent the code that prevents +proxy caching from being added. If that doesn't make your head hurt +then you'll understand the rest of wG just fine. =cut @@ -341,13 +350,9 @@ sub page { if ($useFullUrl) { $url = $self->getSiteURL(); } - $url .= $self->gateway($self->session->asset ? $self->session->asset->get("url") : $self->getRequestedUrl); - if ($self->session->setting->get("preventProxyCache") == 1 && !$skipPreventProxyCache) { - $url = $self->append($url,"noCache=".randint(0,1000).','.$self->session->datetime->time()); - } - if ($pairs) { - $url = $self->append($url,$pairs); - } + my $path = $self->session->asset ? $self->session->asset->get("url") : $self->getRequestedUrl; + $url .= $self->gateway($path, $pairs, $skipPreventProxyCache); + return $url; } diff --git a/t/Session/Stow.t b/t/Session/Stow.t index 101bff02a..147e7b2ac 100644 --- a/t/Session/Stow.t +++ b/t/Session/Stow.t @@ -15,7 +15,7 @@ use lib "$FindBin::Bin/../lib"; use WebGUI::Test; use WebGUI::Session; -use Test::More tests => 29; # increment this value for each test you create +use Test::More tests => 32; # increment this value for each test you create my $session = WebGUI::Test->session; @@ -25,6 +25,9 @@ my $stow = $session->stow; my $count = 0; my $maxCount = 20; +my $disableCache = $session->config->get('disableCache'); +$session->config->set('disableCache',0); + for (my $count = 1; $count <= $maxCount; $count++){ $stow->set("Test$count",$count); } @@ -38,6 +41,13 @@ is($stow->get("Test1"), undef, "delete()"); $stow->deleteAll; is($stow->get("Test2"), undef, "deleteAll()"); +$session->config->set('disableCache', 1); +is($stow->get('Test2'), undef, 'get: when config->disableCache is set get returns undef'); +$session->config->set('disableCache', 0); + +is($session->stow->set('', 'null string'), undef, 'set returns undef when name is empty string'); +is($session->stow->set(0, 'zero'), undef, 'set returns undef when name is zero'); + my @list = qw/alpha delta tango charlie omicron zero/; my @orig_list = qw/alpha delta tango charlie omicron zero/; @@ -65,3 +75,7 @@ is($stow->delete('noSuchKey'), undef, 'deleting non-existant variable returns un $stow->set('countedKey', 5); is($stow->delete('countedKey'), 5, 'delete method returns what was deleted'); + +END { + $session->config->set('disableCache',$disableCache); +} diff --git a/t/Session/Url.t b/t/Session/Url.t index 2a6816837..6e039312e 100644 --- a/t/Session/Url.t +++ b/t/Session/Url.t @@ -52,11 +52,11 @@ my @getRefererUrlTests = ( use Test::More; use Test::MockObject::Extends; use Test::MockObject; -plan tests => 43 + scalar(@getRefererUrlTests); +plan tests => 46 + scalar(@getRefererUrlTests); my $session = WebGUI::Test->session; -#Enable caching +#disable caching my $preventProxyCache = $session->setting->get('preventProxyCache'); $session->setting->set('preventProxyCache', 0) if ($preventProxyCache); @@ -76,26 +76,35 @@ is( $url2, $url.'?a=b', 'append first pair'); $url2 = $session->url->append($url2,'c=d'); is( $url2, $url.'?a=b;c=d', 'append second pair'); +####################################### +# +# gateway +# +####################################### + $session->config->{_config}->{'gateway'} = '/'; -is ( $session->config->get('gateway'), '/', 'Set gateway for downstream tests'); +is( $session->config->get('gateway'), '/', 'Set gateway for downstream tests'); $url2 = $session->url->gateway; -is ( $url2, '/', 'gateway method, no args'); +is( $url2, '/', 'gateway: args'); $url2 = $session->url->gateway('/home'); -is ( $url2, '/home', 'gateway method, pageUrl with leading slash'); +is( $url2, '/home', 'gateway: with leading slash'); $url2 = $session->url->gateway('home'); -is ( $url2, '/home', 'gateway method, pageUrl without leading slash'); +is( $url2, '/home', 'gateway: without leading slash'); #Disable caching $session->setting->set(preventProxyCache => 1); -is ( 1, $session->setting->get('preventProxyCache'), 'disable proxy caching'); +is( 1, $session->setting->get('preventProxyCache'), 'gateway: disable proxy caching'); $url2 = $session->url->gateway('home'); -like ( $url2, qr{/home\?noCache=\d+,\d+$}, 'check proxy prevention setting'); +like( $url2, qr{/home\?noCache=\d+,\d+$}, 'gateway: check proxy prevention setting'); + +$url2 = $session->url->gateway('home','',1); +is( $url2, '/home', 'gateway: skipPreventProxyCache'); #Enable caching $session->setting->set(preventProxyCache => 0); @@ -107,7 +116,6 @@ is( $url2, '/home?a=b', 'append one pair via gateway'); #Restore original proxy cache setting so downstream tests work with no surprises $session->setting->set(preventProxyCache => $preventProxyCache ); - ####################################### # # setSiteUrl and getSiteUrl @@ -118,14 +126,14 @@ $session->setting->set(preventProxyCache => $preventProxyCache ); my $setting_hostToUse = $session->setting->get('hostToUse'); $session->setting->set('hostToUse', 'HTTP_HOST'); my $sitename = $session->config->get('sitename')->[0]; -is ( $session->url->getSiteURL, 'http://'.$sitename, 'getSiteURL from config as http_host'); +is( $session->url->getSiteURL, 'http://'.$sitename, 'getSiteURL from config as http_host'); my $config_port; if ($session->config->get('webServerPort')) { $config_port = $session->config->get('webServerPort'); } $session->url->setSiteURL('http://webgui.org'); -is ( $session->url->getSiteURL, 'http://webgui.org', 'override config setting with setSiteURL'); +is( $session->url->getSiteURL, 'http://webgui.org', 'override config setting with setSiteURL'); ##Create a fake environment hash so we can muck with it. our %mockEnv = %ENV; @@ -133,32 +141,32 @@ $session->{_env}->{_env} = \%mockEnv; $mockEnv{HTTPS} = "on"; $session->url->setSiteURL(undef); -is ( $session->url->getSiteURL, 'https://'.$sitename, 'getSiteURL from config as http_host with SSL'); +is( $session->url->getSiteURL, 'https://'.$sitename, 'getSiteURL from config as http_host with SSL'); $mockEnv{HTTPS} = ""; $mockEnv{HTTP_HOST} = "devsite.com"; $session->url->setSiteURL(undef); -is ( $session->url->getSiteURL, 'http://'.$sitename, 'getSiteURL where requested host is not a configured site'); +is( $session->url->getSiteURL, 'http://'.$sitename, 'getSiteURL where requested host is not a configured site'); my @config_sitename = @{ $session->config->get('sitename') }; $session->config->addToArray('sitename', 'devsite.com'); $session->url->setSiteURL(undef); -is ( $session->url->getSiteURL, 'http://devsite.com', 'getSiteURL where requested host is not the first configured site'); +is( $session->url->getSiteURL, 'http://devsite.com', 'getSiteURL where requested host is not the first configured site'); $session->setting->set('hostToUse', 'sitename'); $session->url->setSiteURL(undef); -is ( $session->url->getSiteURL, 'http://'.$sitename, 'getSiteURL where illegal host has been requested'); +is( $session->url->getSiteURL, 'http://'.$sitename, 'getSiteURL where illegal host has been requested'); $session->config->set('webServerPort', 80); $session->url->setSiteURL(undef); -is ( $session->url->getSiteURL, 'http://'.$sitename.':80', 'getSiteURL with a port'); +is( $session->url->getSiteURL, 'http://'.$sitename.':80', 'getSiteURL with a port'); $session->config->set('webServerPort', 8880); $session->url->setSiteURL(undef); -is ( $session->url->getSiteURL, 'http://'.$sitename.':8880', 'getSiteURL with a non-standard port'); +is( $session->url->getSiteURL, 'http://'.$sitename.':8880', 'getSiteURL with a non-standard port'); $session->url->setSiteURL('http://'.$sitename); -is ( $session->url->getSiteURL, 'http://'.$sitename, 'restore config setting'); +is( $session->url->getSiteURL, 'http://'.$sitename, 'restore config setting'); $session->config->set('sitename', \@config_sitename); $session->setting->set('hostToUse', $setting_hostToUse); if ($config_port) { @@ -171,7 +179,7 @@ else { $url = 'level1 /level2/level3 '; $url2 = 'level1-/level2/level3'; -is ( $session->url->makeCompliant($url), $url2, 'language specific URL compliance'); +is( $session->url->makeCompliant($url), $url2, 'language specific URL compliance'); ####################################### @@ -182,7 +190,7 @@ is ( $session->url->makeCompliant($url), $url2, 'language specific URL complianc my $originalRequest = $session->request; ##Save the original request object -is ($session->url->getRequestedUrl, undef, 'getRequestedUrl returns undef unless it has a request object'); +is($session->url->getRequestedUrl, undef, 'getRequestedUrl returns undef unless it has a request object'); my $newRequest = Test::MockObject->new; my $requestedUrl = 'empty'; @@ -191,20 +199,20 @@ $session->{_request} = $newRequest; ##Validate new MockObject -is ($session->request->uri, 'empty', 'Validate Mock Object operation'); +is($session->request->uri, 'empty', 'Validate Mock Object operation'); $requestedUrl = 'full'; -is ($session->request->uri, 'full', 'Validate Mock Object operation #2'); +is($session->request->uri, 'full', 'Validate Mock Object operation #2'); $requestedUrl = '/path1/file1'; -is ($session->url->getRequestedUrl, 'path1/file1', 'getRequestedUrl, fetch'); +is($session->url->getRequestedUrl, 'path1/file1', 'getRequestedUrl, fetch'); $requestedUrl = '/path2/file2'; -is ($session->url->getRequestedUrl, 'path1/file1', 'getRequestedUrl, check cache of previous result'); +is($session->url->getRequestedUrl, 'path1/file1', 'getRequestedUrl, check cache of previous result'); $session->url->{_requestedUrl} = undef; ##Manually clear cached value $requestedUrl = '/path2/file2?param1=one;param2=two'; -is ($session->url->getRequestedUrl, 'path2/file2', 'getRequestedUrl, does not return params'); +is($session->url->getRequestedUrl, 'path2/file2', 'getRequestedUrl, does not return params'); ####################################### # @@ -224,19 +232,17 @@ is($session->url->page('op=viewHelpTOC;topic=Article'), $requestedUrl.'?op=viewH $url2 = 'http://'.$session->config->get('sitename')->[0].$requestedUrl; is($session->url->page('',1), $url2, 'page: withFullUrl includes method and sitename'); -my $settingsPreventProxyCache = $session->setting->get('preventProxyCache'); $session->setting->set('preventProxyCache', 0); is($session->url->page('','',1), $requestedUrl, 'page: skipPreventProxyCache is a no-op with preventProxyCache off in settings'); $session->setting->set('preventProxyCache', 1); -my $uncacheableUrl = $session->url->page('','',1); -diag($uncacheableUrl); -like($uncacheableUrl, qr{^$requestedUrl}, 'page: skipPreventProxyCache does not change url'); -like($uncacheableUrl, qr/\?noCache=\d{0,4},\d+$/, 'page: skipPreventProxyCache adds noCache param to url'); +my $cacheableUrl = $session->url->page('','',1); +is($cacheableUrl, $requestedUrl, 'page: skipPreventProxyCache does not change url'); -is($session->url->page('','',0), $requestedUrl, 'page: skipPreventProxyCache does not change url'); +like($session->url->page('','',0), qr(^$requestedUrl\?noCache=\d{0,4},\d+$), 'page: noCache added'); -$session->setting->set('preventProxyCache', $settingsPreventProxyCache); +##Restore original setting +$session->setting->set('preventProxyCache', $preventProxyCache); my $defaultAsset = WebGUI::Asset->getDefault($session); $session->asset($defaultAsset); @@ -263,13 +269,9 @@ foreach my $test (@getRefererUrlTests) { # ####################################### -TODO: { - local $TODO = "makeAbsolute TODO's"; - ok(0, 'go back and refigure out how the page method works to test makeAbsoluate with default params'); -} - -is($session->url->makeAbsolute('page1', '/layer1/layer2/'), '/layer1/layer2/page1', 'use a different root'); -is($session->url->makeAbsolute('page1', '/layer1/page2'), '/layer1/page1', 'use a second root that is one level shallower'); +is($session->url->makeAbsolute('page1', '/layer1/layer2/'), '/layer1/layer2/page1', 'makeAbsolute: use a different root'); +is($session->url->makeAbsolute('page1', '/layer1/page2'), '/layer1/page1', 'makeAbsolute: use a second root that is one level shallower'); +is($session->url->makeAbsolute('page1'), '/page1', 'makeAbsolute: default baseUrl from session->asset'); ####################################### # @@ -296,11 +298,24 @@ my $unEscapedString = $session->url->unescape($escapeString); is($escapedString, '10%25%20is%20enough!', 'escape method'); is($unEscapedString, '10% is enough!', 'unescape method'); +####################################### +# +# urlize +# part of urlize is calling makeCompliant, which is tested elsewhere. +# these tests will just make sure that it was called correctly and +# check other urlize behavior +# +####################################### + +is($session->url->urlize('HOME/PATH1'), 'home/path1', 'urlize: urls are lower cased'); +is($session->url->urlize('home/'), 'home', 'urlize: trailing slashes removed'); +is($session->url->urlize('home is where the heart is'), 'home-is-where-the-heart-is', 'urlize: makeCompliant translates spaces to dashes'); + END { ##Always clean-up $session->asset($sessionAsset); $session->config->set('sitename', \@config_sitename); $session->setting->set('hostToUse', $setting_hostToUse); - $session->setting->set('preventProxyCache', $settingsPreventProxyCache); + $session->setting->set('preventProxyCache', $preventProxyCache); if ($config_port) { $session->config->set($config_port); }