Permalink
Browse files

FIX static-handler middleware for well known files

  • Loading branch information...
imalberto committed Oct 16, 2012
1 parent 312a021 commit 02ee8ab6bd5d5dfedb32c5324d778b5e70aa8b96
Showing with 113 additions and 36 deletions.
  1. +3 −3 lib/app/middleware/mojito-handler-static.js
  2. +110 −33 tests/unit/lib/app/middleware/test-handler-static.js
@@ -227,13 +227,13 @@ 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', {}, 'asset-ico-favicon');
}
if (!resource && (path === '/robots.txt')) {
- resource = store.getResource('client', {}, 'asset-txt-robots');
+ resource = store.getResources('client', {}, 'asset-txt-robots');
}
if (!resource && (path === '/crossdomain.xml')) {
- resource = store.getResource('client', {}, 'asset-xml-crossdomain');
+ resource = store.getResources('client', {}, 'asset-xml-crossdomain');
}
if (!resource) {
@@ -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,59 @@ 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;
+
+
+ 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) {
+
+ resourceContentCalled = false;
+ req.url = urls[i];
+ handler = factory({
+ context: {},
+ store: store,
+ logger: {
+ log: function() {}
+ }
+ });
+ // set our expectation
+ store.getResourceContent = function(resource, cb) {
+ OA.areEqual(urlRess[req.url], resource, 'wrong resource');
+ resourceContentCalled = true;
+ };
+ handler(req, res, function() {
+ });
+ A.isTrue(resourceContentCalled, 'getResourceContent was not called for url: ' + req.url);
+ }
+
+ delete store.getResourceContent;

This comment has been minimized.

Show comment Hide comment
@caridy

caridy Oct 17, 2012

Why are we deleting a member here?

@caridy

caridy Oct 17, 2012

Why are we deleting a member here?

This comment has been minimized.

Show comment Hide comment
@imalberto

imalberto Oct 17, 2012

Owner

This was fixed in the next commit (7b24220)

@imalberto

imalberto Oct 17, 2012

Owner

This was fixed in the next commit (7b24220)

}
};

0 comments on commit 02ee8ab

Please sign in to comment.