Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor for improved encapsulation, smaller payload, testing

  • Loading branch information...
commit 705aa55816253185ab5e6b312c9c1ca4ca7e6b56 1 parent 6d5268f
@Trott Trott authored
View
2  root/assets/js/core/server.js
@@ -1 +1 @@
-mwf.server=new function(){var b=mwf.site,d=mwf.classification,c=mwf.userAgent,a=mwf.screen;this.cookieNameLocal=b.cookie.prefix+"server";this.mustRedirect=false;this.mustReload=false;this.init=function(){if(!mwf.capability.cookie()){return}if(/^(\?|.*&)no_server_init([\=\&].*)?$/.test(window.location.search)){return}var e=d.generateCookieContent();if(!b.cookie.exists(d.cookieName)||b.cookie.classification!=e){this.setCookie(d.cookieName,e)}if(!b.cookie.exists(c.cookieName)){this.setCookie(c.cookieName,c.generateCookieContent())}if(!b.cookie.exists(a.cookieName)){this.setCookie(a.cookieName,a.generateCookieContent())}if(this.mustReload&&!mwf.override.isRedirecting){var f=window.location.href;if(f.indexOf("?")==-1){f+="?"}if(f.indexOf("?")<f.length-1){f+="&"}f+="no_server_init";b.redirect(f)}else{if(this.mustRedirect&&!mwf.override.isRedirecting){b.redirect(b.asset.root+"/passthru.php?return="+encodeURIComponent(window.location)+"&mode="+mwf.browser.getMode())}}};this.setCookie=function(g,e){var f=b.local.isSameOrigin();if(f){document.cookie=g+"="+encodeURIComponent(e)+";path=/";this.mustReload=true}else{this.mustRedirect=true}}};mwf.server.init();
+mwf.server=new function(){var c=mwf.site,g=mwf.classification,f=mwf.userAgent,b=mwf.screen,e=false,a=false;var d=function(j,h){var i=c.local.isSameOrigin();if(i){document.cookie=j+"="+encodeURIComponent(h)+";path=/";a=true}else{e=true}};this.init=function(){if(!mwf.capability.cookie()){return}if(/^(\?|.*&)no_server_init([\=\&].*)?$/.test(window.location.search)){return}var h=g.generateCookieContent();if(!c.cookie.exists(g.cookieName)||c.cookie.classification!=h){d(g.cookieName,h)}if(!c.cookie.exists(f.cookieName)){d(f.cookieName,f.generateCookieContent())}if(!c.cookie.exists(b.cookieName)){d(b.cookieName,b.generateCookieContent())}if(a&&!mwf.override.isRedirecting){var i=window.location.href;if(i.indexOf("?")==-1){i+="?"}if(i.indexOf("?")<i.length-1){i+="&"}i+="no_server_init";c.redirect(i)}else{if(e&&!mwf.override.isRedirecting){c.redirect(c.asset.root+"/passthru.php?return="+encodeURIComponent(window.location)+"&mode="+mwf.browser.getMode())}}}};mwf.server.init();
View
107 root/assets/js/core/server.src.js
@@ -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();
View
36 root/assets/test/js_unit/core/server.js
@@ -4,7 +4,7 @@
* @author trott
* @copyright Copyright (c) 2010-11 UC Regents
* @license http://mwf.ucla.edu/license
- * @version 20111214
+ * @version 20120415
*
* @requires mwf
* @requires qunit
@@ -12,28 +12,32 @@
*/
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()
View
6 root/assets/test/js_unit/index.php
@@ -0,0 +1,6 @@
+<?php
+$past = time() - 3600;
+foreach ($_COOKIE as $key => $value) {
+ setcookie($key, FALSE, $past, '/');
+}
+header('Location: results.html');
View
17 root/assets/test/js_unit/index.html → root/assets/test/js_unit/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>
View
2  test/features/step_definitions/js_unit_tests_steps.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.