Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

FIX static-handler middleware for well known files #638

Merged
merged 4 commits into from Oct 17, 2012
@@ -227,18 +227,24 @@ function staticProvider(store, logger) {
// TODO: these adjustments should really be done by addons/rs/url
if (!resource && (path === '/favicon.ico')) {
- resource = store.getResource('client', {}, 'asset-ico-favicon');
+ resource = store.getResources('client', {}, {mojit: 'shared', id: 'asset-ico-favicon'});
}
if (!resource && (path === '/robots.txt')) {
- resource = store.getResource('client', {}, 'asset-txt-robots');
+ resource = store.getResources('client', {}, {mojit: 'shared', id: 'asset-txt-robots'});
}
if (!resource && (path === '/crossdomain.xml')) {
- resource = store.getResource('client', {}, 'asset-xml-crossdomain');
+ resource = store.getResources('client', {}, {mojit: 'shared', id: 'asset-xml-crossdomain'});
}
if (!resource) {
return next();
}
+ if (Array.isArray(resource)) {
+ if (resource.length === 0) {
+ return next();
+ }
+ resource = resource[0];
+ }
// Cache hit
//if (cache && !conditionalGET(req) && (hit = _cache[path])) {
@@ -5,48 +5,72 @@
*/
YUI().use('mojito-test-extra', 'test', function(Y) {
var A = Y.Assert,
+ OA = Y.ObjectAssert,
cases = {},
+ store,
+ urlRess,
factory = require(Y.MOJITO_DIR + 'lib/app/middleware/mojito-handler-static');
+ urlRess = {
+ "/compiled.css": {
+ mime: {
+ type: 'text/css',
+ charset: 'UTF-8'
+ }
+ },
+ "/favicon.ico": {
+ mime: {
+ type: 'image/vnc.microsoft.com'
+ }
+ },
+ "/robots.txt": {
+ mime: {
+ type: 'text/plain',
+ charset: 'UTF-8'
+ }
+ },
+ "/crossdomain.xml": {
+ mime: {
+ type: 'text/xml',
+ charset: 'UTF-8'
+ }
+ }
+ };
cases = {
name: 'static handler tests',
_handler: null,
setUp: function() {
- var store = {
- getAppConfig: function() { return { obj: 'appConfig' }; },
- getAllURLResources: function () {
- return {
- "/compiled.css": {
- mime: {
- type: 'text/css',
- charset: 'UTF-8'
- }
- }
- };
- },
- getResourceContent: function (args, callback) {
- var content, stat;
- content = new Buffer('1234567890');
- stat = {
- mtime: new Date(),
- ctime: new Date(),
- // this size is different from the data.length since it is suppose to be
- // the original size of the compiled buffer
- size: 5
- };
- callback(undefined, content, stat);
- },
- getStaticAppConfig: function() {
- return {
- staticHandling: {
- cache: false,
- maxAge: null
- }
- };
- }
- };
+ store = {
+ getAppConfig: function() { return { obj: 'appConfig' }; },
+ getAllURLResources: function () {
+ return urlRess;
+ },
+ getResourceContent: function (args, callback) {
+ var content, stat;
+ content = new Buffer('1234567890');
+ stat = {
+ mtime: new Date(),
+ ctime: new Date(),
+ // this size is different from the data.length since it is suppose to be
+ // the original size of the compiled buffer
+ size: 5
+ };
+ callback(undefined, content, stat);
+ },
+ getStaticAppConfig: function() {
+ return {
+ staticHandling: {
+ cache: false,
+ maxAge: null
+ }
+ };
+ },
+ getResources: function(env, ctx, filter) {
+ return [{ filter: filter }];
+ }
+ };
this._handler = factory({
context: {},
@@ -158,6 +182,140 @@ YUI().use('mojito-test-extra', 'test', function(Y) {
A.areEqual(0, callCount, 'next() handler should have not been called');
A.isTrue(end, 'res.end() should be called after serving a compiled response.');
A.areEqual(10, resHeader['Content-Length'], 'the buffer header should dictate the content-length');
+ },
+
+ 'handler detects well known files': function() {
+
+ var req,
+ res,
+ handler,
+ resourceContentCalled = false,
+ urls,
+ i,
+ getResourceContentFn,
+ callCount;
+
+
+ getResourceContentFn = store.getResourceContent;
+ urls = ['/robots.txt', '/crossdomain.xml', '/favicon.ico'];
+ ress = [
+ 'asset-txt-robots',
+ 'asset-xml-crossdomain',
+ 'asset-ico-favicon'
+ ];
+ req = {
+ url: '/robots.txt',
+ method: 'GET',
+ headers: {}
+ };
+ res = {
+ writeHead: function(code, header) {
+ },
+ end: function() {
+ }
+ }
+
+
+ for (i = 0; i < urls.length; i += 1) {
+ callCount = 0;
+ resourceContentCalled = false;
+ req.url = urls[i];
+ handler = factory({
+ context: {},
+ store: store,
+ logger: {
+ log: function() {}
+ }
+ });
+ store.getResourceContent = function(resource, cb) {
+ OA.areEqual(urlRess[req.url], resource, 'wrong resource');
+ resourceContentCalled = true;
+ };
+ handler(req, res, function() {
+ callCount++;
+ });
+ A.areEqual(0, callCount, 'next() handler should not have been called')
+ A.isTrue(resourceContentCalled, 'getResourceContent was not called for url: ' + req.url);
+ }
+
+ store.getResourceContent = getResourceContentFn;
+ },
+
+ 'handler deals with resources correctly': function() {
+ var req,
+ resp,
+ getResourcesFn,
+ getAllURLResourcesFn,
+ getResourceContentFn,
+ handler,
+ mockResources;
+
+ mockResources = {
+ "/robots.txt": {
+ mime: { type: 'text/html' }
+ }
+ };
+ getResourceContentFn = store.getResourceContent;
+ getAllURLResourcesFn = store.getAllURLResources;
+ getResourcesFn = store.getResources;
+
+ req = {
+ url: '/robots.txt',
+ method: 'GET',
+ headers: {}
+ };
+ resp = {
+ writeHeader: function() { },
+ end: function() { }
+ }
+
+ //
+ // handle res of type obj
+ store.getAllURLResources = function() {
+ return mockResources;
+ };
+ store.getResources = function() {
+ return [];
+ };
+ store.getResourceContent = function(res, cb) {
+ OA.areEqual(mockResources["/robots.txt"], res, 'wrong resource');
+ };
+
+ handler = factory({
+ store: store,
+ context: {},
+ logger: { log: function() {} }
+ });
+
+ handler(req, resp, function() {
+ A.fail('next() handler 1 should not have been called');
+ });
+
+ //
+ // handle res of type array
+ store.getAllURLResources = function() {
+ return {};
+ };
+ store.getResources = function() {
+ return [mockResources["/robots.txt"]];
+ };
+ store.getResourceContent = function(res, cb) {
+ OA.areEqual(mockResources["/robots.txt"], res, 'wrong resource');
+ };
+
+ handler = factory({
+ store: store,
+ context: {},
+ logger: { log: function() {} }
+ });
+
+ handler(req, resp, function() {
+ A.fail('next() handler 2 should not have been called');
+ });
+
+ store.getResources = getResourcesFn;
+ store.getResourceContent = getResourceContentFn;
+ store.getAllURLResources = getAllURLResourcesFn;
}
};