Permalink
Browse files

Refactor for improved encapsulation, smaller payload, testing

  • Loading branch information...
1 parent 6d5268f commit 705aa55816253185ab5e6b312c9c1ca4ca7e6b56 @Trott Trott committed Apr 16, 2012

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -8,7 +8,7 @@
* @author ebollens
* @copyright Copyright (c) 2010-12 UC Regents
* @license http://mwf.ucla.edu/license
- * @version 20120414
+ * @version 20120415
*
* @requires mwf
* @requires mwf.site
@@ -33,13 +33,51 @@ mwf.server = new function(){
var site = mwf.site,
classification = mwf.classification,
userAgent = mwf.userAgent,
- screen = mwf.screen;
-
- //@todo: These properties are visible outside mwf.server. Should they be?
- this.cookieNameLocal = site.cookie.prefix+'server';
- this.mustRedirect = false;
- this.mustReload = false;
+ screen = mwf.screen,
+ mustRedirect = false,
+ mustReload = false;
+
+ var setCookie = function(cookieName, cookieContent) {
+
+ /**
+ * Function to generate a cookie on the service provider, specifying a
+ * domain if this is a cross
+ */
+
+ var isSameOrigin = site.local.isSameOrigin();
+
+ /**
+ * If not cross-domain or this is the first load and third party is
+ * supported, then attempt to write the cookie to the SP directly.
+ */
+
+ if(isSameOrigin){
+
+ /**
+ * Write the cookie with the proper suffix for service provider.
+ */
+
+ document.cookie = cookieName + '=' + encodeURIComponent(cookieContent)+';path=/';
+
+ /**
+ * Must reload the page to propagate the cookie to SP.
+ */
+
+ mustReload = true;
+
+ /**
+ * If third-party cookies aren't supported and this is cross domain,
+ * then redirect through the SP and then back to CP.
+ */
+
+ } else {
+
+ mustRedirect = true;
+
+ }
+ }
+
this.init = function(){
/**
@@ -56,7 +94,7 @@ mwf.server = new function(){
* redirect back to the originator.
*/
if (/^(\?|.*&)no_server_init([\=\&].*)?$/.test(window.location.search)) {
- return;
+ return;
}
var classificationCookie = classification.generateCookieContent();
@@ -68,21 +106,21 @@ mwf.server = new function(){
*/
if(!site.cookie.exists(classification.cookieName) || site.cookie.classification != classificationCookie)
- this.setCookie(classification.cookieName, classificationCookie);
+ setCookie(classification.cookieName, classificationCookie);
/**
* Set user agent cookie if it doesn't already exist on server.
*/
if(!site.cookie.exists(userAgent.cookieName))
- this.setCookie(userAgent.cookieName, userAgent.generateCookieContent());
+ setCookie(userAgent.cookieName, userAgent.generateCookieContent());
/**
* Set screen cookie if it doesn't already exist on server.
*/
if(!site.cookie.exists(screen.cookieName))
- this.setCookie(screen.cookieName, screen.generateCookieContent());
+ setCookie(screen.cookieName, screen.generateCookieContent());
/**
* If the service provider doesn't have cookies, either (1) reload
@@ -91,60 +129,17 @@ mwf.server = new function(){
* service provider already has cookies, then this isn't necessary.
*/
- if(this.mustReload && !mwf.override.isRedirecting){
+ if(mustReload && !mwf.override.isRedirecting){
var loc = window.location.href;
if(loc.indexOf('?') == -1) loc += "?";
if(loc.indexOf('?') < loc.length-1) loc += "&";
loc += "no_server_init";
site.redirect(loc);
- }else if(this.mustRedirect && !mwf.override.isRedirecting){
+ }else if(mustRedirect && !mwf.override.isRedirecting){
site.redirect(site.asset.root+'/passthru.php?return='+encodeURIComponent(window.location)+'&mode='+mwf.browser.getMode());
}
}
-
-
- //@todo: setCookie() is visible from outside the object. Is that what we really want?
- this.setCookie = function(cookieName, cookieContent) {
-
- /**
- * Function to generate a cookie on the service provider, specifying a
- * domain if this is a cross
- */
-
- var isSameOrigin = site.local.isSameOrigin();
-
- /**
- * If not cross-domain or this is the first load and third party is
- * supported, then attempt to write the cookie to the SP directly.
- */
-
- if(isSameOrigin){
-
- /**
- * Write the cookie with the proper suffix for service provider.
- */
-
- document.cookie = cookieName + '=' + encodeURIComponent(cookieContent)+';path=/';
-
- /**
- * Must reload the page to propagate the cookie to SP.
- */
-
- this.mustReload = true;
-
- /**
- * If third-party cookies aren't supported and this is cross domain,
- * then redirect through the SP and then back to CP.
- */
-
- } else {
-
- this.mustRedirect = true;
-
- }
-
- }
}
mwf.server.init();
@@ -4,36 +4,40 @@
* @author trott
* @copyright Copyright (c) 2010-11 UC Regents
* @license http://mwf.ucla.edu/license
- * @version 20111214
+ * @version 20120415
*
* @requires mwf
* @requires qunit
*
*/
module("core/server.js");
-
-test("mwf.server.setCookie() same origin", function()
-{
- var saveIsSameOrigin = mwf.site.local.isSameOrigin;
- mwf.site.local.isSameOrigin = function() {
- return true;
- }
- mwf.server.setCookie('mwf_test_cookie',';');
- ok(document.cookie.match(/mwf_test_cookie=%3B/), 'cookie should be set and values encoded');
- mwf.site.local.isSameOrigin = saveIsSameOrigin;
-});
-test("mwf.server.setCookie() not same origin", function()
+test("mwf.server.init() not same origin, redirects if cookie not set", function()
{
+ window.test_result = false;
var saveIsSameOrigin = mwf.site.local.isSameOrigin;
mwf.site.local.isSameOrigin = function() {
return false;
}
- mwf.server.setCookie('mwf_test_cookie',';');
- ok(mwf.server.mustRedirect,'if not same origin, setting cookie should result in redirect');
- mwf.site.local.isSameOrigin = saveIsSameOrigin;
+
+ var saveExists = mwf.site.cookie.exists;
+ mwf.site.cookie.exists = function(cookieName) {
+ return cookieName != mwf.screen.cookieName;
+ }
+
+ var saveRedirect = mwf.site.redirect;
+ mwf.site.redirect = function() {
+ window.test_result = true;
+ }
+
+ mwf.server.init();
+ ok(window.test_result,'if not same origin, setting cookie should result in redirect');
+ mwf.site.local.isSameOrigin = saveIsSameOrigin;
+ mwf.site.cookie.exists = saveExists;
+ mwf.site.redirect = saveRedirect;
+ delete window.test_result;
});
test("mwf.server.init() same origin sets cookies", function()
@@ -0,0 +1,6 @@
+<?php
+$past = time() - 3600;
+foreach ($_COOKIE as $key => $value) {
+ setcookie($key, FALSE, $past, '/');
+}
+header('Location: results.html');
@@ -5,13 +5,6 @@
<link rel="stylesheet" href="qunit/qunit.css" type="text/css"/>
<meta name="viewport" content="height=device-height,width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
- <!-- If no_server_init, reload without it. Its side effects can cause test failures. -->
- <script>
- if (/^(\?|.*&)no_server_init([\=\&].*)?$/.test(window.location.search)) {
- window.location.href = window.location.href.replace("no_server_init","");
- }
- </script>
-
<script src="../../js.php?standard_libs=geolocation+preferences&amp;full_libs=configurableMenu"></script>
<script src="qunit/qunit.js"></script>
@@ -36,6 +29,12 @@ <h1 id="qunit-header">MWF JS unit tests</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests">
- </ol>
+ </ol>
+ <!-- If no_server_init, reload without it. Its side effects can cause test failures. -->
+ <script>
+ if (/^(\?|.*&)no_server_init([\=\&].*)?$/.test(window.location.search)) {
+ window.location.href = window.location.href.replace("no_server_init","");
+ }
+ </script>
</body>
-</html>
+</html>
@@ -4,7 +4,7 @@
end
Given /^I am on the JS Unit Tests page filtered for (.*)$/ do |filter|
- visit "http://localhost/assets/test/js_unit/?filter=#{filter}"
+ visit "http://localhost/assets/test/js_unit/results.html?filter=#{filter}"
@results = find_by_id('qunit-testresult')
end

0 comments on commit 705aa55

Please sign in to comment.