Permalink
Browse files

FIX proper use of RS api; added unit tests;

  • Loading branch information...
imalberto committed Oct 17, 2012
1 parent 02ee8ab commit 7b2422005e730fbc60bcb512880b2429960b9022
@@ -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.getResources('client', {}, 'asset-ico-favicon');
+ resource = store.getResources('client', {}, {mojit: 'shared', id: 'asset-ico-favicon'});
}
if (!resource && (path === '/robots.txt')) {
- resource = store.getResources('client', {}, 'asset-txt-robots');
+ resource = store.getResources('client', {}, {mojit: 'shared', id: 'asset-txt-robots'});
}
if (!resource && (path === '/crossdomain.xml')) {
- resource = store.getResources('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])) {
@@ -68,7 +68,7 @@ YUI().use('mojito-test-extra', 'test', function(Y) {
};
},
getResources: function(env, ctx, filter) {
- return { filter: filter }
+ return [{ filter: filter }];
}
};
@@ -191,9 +191,12 @@ YUI().use('mojito-test-extra', 'test', function(Y) {
handler,
resourceContentCalled = false,
urls,
- i;
+ i,
+ getResourceContentFn,
+ callCount;
+ getResourceContentFn = store.getResourceContent;
urls = ['/robots.txt', '/crossdomain.xml', '/favicon.ico'];
ress = [
'asset-txt-robots',
@@ -214,7 +217,7 @@ YUI().use('mojito-test-extra', 'test', function(Y) {
for (i = 0; i < urls.length; i += 1) {
-
+ callCount = 0;
resourceContentCalled = false;
req.url = urls[i];
handler = factory({
@@ -224,17 +227,95 @@ YUI().use('mojito-test-extra', 'test', function(Y) {
log: function() {}
}
});
- // set our expectation
store.getResourceContent = function(resource, cb) {
OA.areEqual(urlRess[req.url], resource, 'wrong resource');
resourceContentCalled = true;
};
handler(req, res, function() {
+ callCount++;
});
+ A.isTrue(0 === callCount, 'next() handler should not have been called')

This comment has been minimized.

Show comment
Hide comment
@caridy

caridy Oct 17, 2012

why not A.areEqual? that will give us a better message, telling us the callCount number when failure.

@caridy

caridy Oct 17, 2012

why not A.areEqual? that will give us a better message, telling us the callCount number when failure.

A.isTrue(resourceContentCalled, 'getResourceContent was not called for url: ' + req.url);
}
- delete store.getResourceContent;
+ 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;

This comment has been minimized.

Show comment
Hide comment
@caridy

caridy Oct 17, 2012

Isn't cleaner to store and restore original methods using setUp/tearDown instead of doing the same over and over again in different tests?

@caridy

caridy Oct 17, 2012

Isn't cleaner to store and restore original methods using setUp/tearDown instead of doing the same over and over again in different tests?

This comment has been minimized.

Show comment
Hide comment
@imalberto

imalberto Oct 17, 2012

Owner

Only the newly added tests needed to customized the store instance with "wrappers". The other tests works with the "store" and "_handler" as-is, so these are setup in the setUp/tearDown.

@imalberto

imalberto Oct 17, 2012

Owner

Only the newly added tests needed to customized the store instance with "wrappers". The other tests works with the "store" and "_handler" as-is, so these are setup in the setUp/tearDown.

+ store.getResourceContent = getResourceContentFn;
+ store.getAllURLResources = getAllURLResourcesFn;
}
};

0 comments on commit 7b24220

Please sign in to comment.