Permalink
Browse files

Combo bug fixes.

- Switch testing server to connect
- Handle the case where all combo scripts are cached
- If the date header is malformed, ignore max-age
- If no caching headers are present, a resource is stale
  • Loading branch information...
1 parent f1888f1 commit bc9c7fe1d9f5bbc34f33d9e15fca71344cc41560 @johnboxall johnboxall committed Jun 25, 2012
View
@@ -185,10 +185,9 @@ var ccDirectives = /^\s*(public|private|no-cache|no-store)\s*$/
, expires = headers.expires
, now = Date.now()
- // If `max-age` is present and no other cache directives exist, then
- // we are stale if we are older.
- if (cacheControl && date) {
- date = Date.parse(date);
+ // If `max-age` and `date` are present, and no other no other cache
+ // directives exist, then we are stale if we are older.
+ if (cacheControl && (date = Date.parse(date))) {
cacheControl = ccParse(cacheControl);
if ((cacheControl['max-age']) &&
@@ -206,7 +205,7 @@ var ccDirectives = /^\s*(public|private|no-cache|no-store)\s*$/
}
// Otherwise, we are stale.
- return false;
+ return true;
}
};
@@ -255,6 +254,8 @@ var $ = Mobify.$
if (uncached.length) {
bootstrap.src = getURL(uncached, defaults.loadCallback);
+ } else {
+ bootstrap.innerHTML = defaults.loadCallback + '();';
}
$scripts = $(bootstrap).add($scripts);
@@ -309,7 +310,7 @@ var $ = Mobify.$
/**
* Returns a URL suitable for use with the combo service. Sorted to generate
- * cacheable URLs.
+ * consistent URLs.
*/
, getURL = function(urls, callback) {
return defaults.endpoint + callback + '/' + JSONURIencode(urls.slice().sort());
View
@@ -4,7 +4,7 @@
"description": "The Mobify.js client side adaptation library.",
"author": "Mobify <dev@mobify.com>",
"devDependencies": {
- "node-static": "0.5.8"
+ "connect": "2.3.4"
},
"engine": "node 0.6.2",
"repository" : {
View
@@ -5,6 +5,9 @@
<script src="qunit/qunit.js"></script>
</head>
<body>
+ <!-- Usage: -->
+
+
<h1 id="qunit-header">QUnit Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
@@ -33,25 +36,39 @@ <h2 id="qunit-userAgent"></h2>
<script>
var $ = Mobify.$
- , httpCache = Mobify.httpCache;
+ , httpCache = Mobify.httpCache
+ // Time offsets in seconds
+ , TWO_WEEKS = 14 * 24 * 60 * 60
+ , TEN_MINUTES = 600
+ , UTC_TWO_WEEKS_FROM_NOW = (new Date(Date.now() + (TWO_WEEKS * 1000))).toUTCString()
+ // UTC dates
+ , UTC_TWO_WEEKS_FROM_NOW = (new Date(Date.now() + (TWO_WEEKS * 1000))).toUTCString()
+ , UTC_TWO_WEEKS_AGO = (new Date(Date.now() - (TWO_WEEKS * 1000))).toUTCString()
+ , UTC_NOW = (new Date()).toUTCString()
+
+ // I don't fit in `localStorage` in Chrome or Safari...
+ // But I do seem to fit in FF.
+ , LONG_STRING = Array(5000000).join('1');
+
module('Mobify.cssURL')
test('It exists', function() {
ok(Mobify.cssURL({}))
})
+
module('$.fn.combineScripts')
asyncTest('Warm cache', 1, function() {
var cache = {
'http://127.0.0.1:1337/tests/push.js': {
- headers: {}
- , status: 'ready'
- , url: 'http://127.0.0.1:1337/tests/1.js'
- , body: 'var scripts = [].slice.call(document.getElementsByTagName("script"));'
- + 'a.push(scripts[scripts.length - 2].innerHTML.split("#")[1][0]);'
- , text: true
+ 'headers': {'expires': UTC_TWO_WEEKS_FROM_NOW}
+ , 'status': 'ready'
+ , 'url': 'http://127.0.0.1:1337/tests/1.js'
+ , 'body': 'var scripts = [].slice.call(document.getElementsByTagName("script"));'
+ + 'a.push(scripts[scripts.length - 2].innerHTML.split("#")[1][0]);'
+ , 'text': true
}
}
, $scripts = $($('#combineScripts-1').text())
@@ -137,20 +154,17 @@ <h2 id="qunit-userAgent"></h2>
$(iframeEl).on('load', load);
}
});
+
module('httpCache')
- // I don't fit in `localStorage` in Chrome or Safari...
- // But I do seem to fit in FF.
- var longString = Array(5000000).join('1');
-
asyncTest('Eviction on an unused resource', 2, function() {
httpCache.reset();
httpCache.save();
- httpCache.set('unused', {headers: {}, body: longString})
+ httpCache.set('unused', {'headers': {'expires': UTC_TWO_WEEKS_FROM_NOW}, body: LONG_STRING})
- httpCache.set('used', {headers: {}, body: 'used'})
+ httpCache.set('used', {'headers': {'expires': UTC_TWO_WEEKS_FROM_NOW}, body: 'used'})
httpCache.get('used', true)
httpCache.save(function(err) {
@@ -170,11 +184,11 @@ <h2 id="qunit-userAgent"></h2>
httpCache.reset();
httpCache.save();
- httpCache.set('old', {headers: {}, body: longString})
+ httpCache.set('old', {'headers': {'expires': UTC_TWO_WEEKS_FROM_NOW}, body: LONG_STRING})
httpCache.get('old', true)
setTimeout(function() {
- httpCache.set('new', {headers: {}, body: 'new'})
+ httpCache.set('new', {'headers': {'expires': UTC_TWO_WEEKS_FROM_NOW}, body: 'new'})
httpCache.get('new', true)
httpCache.save(function(err) {
@@ -194,24 +208,16 @@ <h2 id="qunit-userAgent"></h2>
module('httpCache.utils')
- test('isStale', 4, function() {
+ test('isStale', 6, function() {
var isStale = httpCache.utils.isStale
- // Time offsets in seconds
- , TWO_WEEKS = 14 * 24 * 60 * 60
- , TEN_MINUTES = 600
- // UTC dates
- , UTC_TWO_WEEKS_FROM_NOW = (new Date(Date.now() + (TWO_WEEKS * 1000))).toUTCString()
- , UTC_TWO_WEEKS_AGO = (new Date(Date.now() - (TWO_WEEKS * 1000))).toUTCString()
- , UTC_NOW = (new Date()).toUTCString()
-
, resources = {
'stale-expires': {
'headers': {
'expires': UTC_TWO_WEEKS_AGO
}
}
- , 'state-cache-control': {
+ , 'stale-cache-control': {
'headers': {
'date': UTC_TWO_WEEKS_AGO
, 'cache-control': 'public,max-age=' + TEN_MINUTES
@@ -230,12 +236,25 @@ <h2 id="qunit-userAgent"></h2>
, 'cache-control': 'public,max-age=' + TEN_MINUTES
}
}
+
+ , 'no-headers': {
+ 'headers': {}
+ }
+
+ , 'invalid-date': {
+ 'headers': {
+ 'date': 'invalid'
+ , 'cache-control': 'public,max-age=' + TEN_MINUTES
+ }
+ }
};
ok(isStale(resources['stale-expires']));
- ok(isStale(resources['state-cache-control']));
+ ok(isStale(resources['stale-cache-control']));
ok(!isStale(resources['fresh-far-future']));
- ok(!isStale(resources['fresh-cache-control']));
+ ok(!isStale(resources['fresh-cache-control']));
+ ok(isStale(resources['no-headers']), 'No cache headers is stale');
+ ok(isStale(resources['invalid-date']), 'Invalid date is stale');
});
</script>
@@ -1,4 +0,0 @@
-function one() {
- combo = combo || []
- combo.push('e1');
-}
@@ -1,4 +0,0 @@
-function two() {
- combo = combo || []
- combo.push('e2');
-}
@@ -1,4 +0,0 @@
-function three() {
- combo = combo || []
- combo.push('e3');
-}
@@ -1,10 +0,0 @@
-<!DOCTYPE html>
-<html>
- <head>
- </head>
- <body>
- <div id="test-combineScripts">
- <script x-src="/tests/fixtures/combo/combo-async.js"></script>
- </div>
- </body>
-</html>
@@ -1 +0,0 @@
-combo = "async!";
@@ -1,17 +0,0 @@
-<!DOCTYPE html>
-<html>
- <head>
- </head>
- <body>
- <div id="test-combineScripts">
- <!-- Absolute to play nice w/ combo. -->
- <script type="text/test">combo = []</script>
- <script x-src="/tests/fixtures/combo/combo-1.js"></script>
- <script type="text/test">one();</script>
- <script x-src="/tests/fixtures/combo/combo-2.js"></script>
- <script type="text/test">two();</script>
- <script x-src="/tests/fixtures/combo/combo-3.js"></script>
- <script type="text/test">three();</script>
- </div>
- </body>
-</html>
View
@@ -1,10 +1,6 @@
-var Http = require('http')
- , serve = new (require('node-static').Server)(process.cwd(), {cache: false})
- , handler = function(req, rsp) {
- console.log(req.url);
- return serve.serve(req, rsp);
- }
+var Connect = require('connect')
+ , server = new Connect().use(Connect.static(process.cwd()))
, PORT = 1337
-Http.createServer(handler).listen(PORT);
+server.listen(PORT);
console.log('Mobify.js Testing Server @ http://127.0.0.1:' + PORT);

0 comments on commit bc9c7fe

Please sign in to comment.