Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simplify response logic with express response sugar

With unit tests!
  • Loading branch information...
commit 77f731ed3f12dae802767a7b074f58350c669cac 1 parent ff14787
@ekashida ekashida authored
View
7 lib/app/middleware/mojito-handler-tunnel-specs.js
@@ -51,12 +51,7 @@ module.exports = function (config) {
new Error('Error opening: ' + req.url + '\n' + err)
);
}
- // TODO: Use the express sugar method res.json([status],
- // [body]) after we rewrite the existing tests.
- res.writeHead(res.statusCode || 200, {
- 'content-type': 'application/json; charset="utf-8"'
- });
- res.end(JSON.stringify(data, null, 4));
+ res.send(200, data);
}
);
};
View
7 lib/app/middleware/mojito-handler-tunnel-type.js
@@ -42,12 +42,7 @@ module.exports = function (config) {
new Error('Error opening: ' + req.url + '\n' + err)
);
}
- // TODO: Use the express sugar method res.json([status],
- // [body]) after we rewrite the existing tests.
- res.writeHead(res.statusCode || 200, {
- 'content-type': 'application/json; charset="utf-8"'
- });
- res.end(JSON.stringify(data, null, 4));
+ res.send(200, data);
}
);
};
View
16 tests/unit/lib/app/middleware/middleware_test_descriptor.json
@@ -48,6 +48,22 @@
},
"group": "fw,unit,server"
},
+ "handler-tunnel-specs": {
+ "params": {
+ "lib": "$$config.lib$$/app/middleware/mojito-handler-tunnel-specs.js",
+ "test": "./test-handler-tunnel-specs.js",
+ "driver": "nodejs"
+ },
+ "group": "fw,unit,server"
+ },
+ "handler-tunnel-type": {
+ "params": {
+ "lib": "$$config.lib$$/app/middleware/mojito-handler-tunnel-type.js",
+ "test": "./test-handler-tunnel-type.js",
+ "driver": "nodejs"
+ },
+ "group": "fw,unit,server"
+ },
"router": {
"params": {
"lib": "$$config.lib$$/app/middleware/mojito-handler-tunnel.js,../../../../../lib/app/autoload/route-maker.common.js,../../../../../lib/app/autoload/util.common.js",
View
148 tests/unit/lib/app/middleware/test-handler-tunnel-specs.js
@@ -0,0 +1,148 @@
+/*
+ * Copyright (c) 2011-2013, Yahoo! Inc. All rights reserved.
+ * Copyrights licensed under the New BSD License.
+ * See the accompanying LICENSE file for terms.
+ */
+
+/*global YUI, require*/
+/*jslint nomen:true*/
+
+YUI().use('mojito-test-extra', 'test', function (Y) {
+ 'use strict';
+
+ var A = Y.Assert,
+ AA = Y.ArrayAssert,
+ OA = Y.ObjectAssert,
+
+ factory = require(Y.MOJITO_DIR + 'lib/app/middleware/mojito-handler-tunnel-specs'),
+ expandInstanceInvoked = false,
+ error,
+ req,
+ res,
+ sendData,
+ statusCode,
+ nextCallCount,
+ middleware,
+ store,
+ config;
+
+
+ Y.Test.Runner.add(new Y.Test.Case({
+ name: 'tunnel handler specs tests',
+
+ setUp: function () {
+ nextCallCount = 0;
+
+ store = {
+ expandInstanceForEnv: function (env, instance, context, callback) {
+ expandInstanceInvoked = true;
+ }
+ };
+
+ config = {
+ store: store
+ };
+
+ req = {
+ url: '/tunnel',
+ _tunnel: {
+ specsReq: {
+ type: 'MojitX',
+ name: 'default'
+ }
+ }
+ };
+
+ res = {
+ send: function (code, data) {
+ sendData = data;
+ statusCode = code;
+ }
+ };
+ },
+
+ tearDown: function () {
+ expandInstanceInvoked = false;
+
+ nextCallCount = 0;
+ sendData = undefined;
+ statusCode = undefined;
+ store = null;
+ config = null;
+ req = null;
+ res = null;
+ middleware = null;
+ error = undefined;
+ },
+
+ 'handler should exit early if not specs request': function () {
+ req._tunnel.specsReq = null;
+ middleware = factory(config);
+ middleware(req, res, function () {
+ nextCallCount += 1;
+ });
+
+ A.areSame(1, nextCallCount, 'next() handler should have been called');
+ A.isFalse(expandInstanceInvoked, 'should not have attempted to expand the instance');
+ },
+
+ 'handler should error if "type" is missing': function () {
+ req._tunnel.specsReq.type = null;
+ middleware = factory(config);
+ middleware(req, res, function (err) {
+ error = err;
+ nextCallCount += 1;
+ });
+
+ A.areSame(1, nextCallCount, 'next() handler should have been called');
+ A.isNotUndefined(error, 'next() handler should have received an error');
+ A.areSame(404, res.statusCode, 'status code should have been set to 404');
+ },
+
+ 'handler should error if "name" is missing': function () {
+ req._tunnel.specsReq.name = null;
+ middleware = factory(config);
+ middleware(req, res, function (err) {
+ error = err;
+ nextCallCount += 1;
+ });
+
+ A.areSame(1, nextCallCount, 'next() handler should have been called');
+ A.isNotUndefined(error, 'next() handler should have received an error');
+ A.areSame(404, res.statusCode, 'status code should have been set to 404');
+ },
+
+ 'handler should error if expandInstanceForEnv errors': function () {
+ config.store.expandInstanceForEnv = function (env, instance, context, callback) {
+ callback(new Error('you have 10 seconds to eat that tomato'));
+ };
+ middleware = factory(config);
+ middleware(req, res, function (err) {
+ error = err;
+ nextCallCount += 1;
+ });
+
+ A.areSame(1, nextCallCount, 'next() handler should have been called');
+ A.areSame(500, res.statusCode, 'status code should have been set to 500');
+ A.isNotUndefined(error, 'next() handler should have received an error');
+ A.isUndefined(sendData, 'data should not have been sent');
+ },
+
+ 'test handler response for success': function () {
+ var data = 'good job, here is your dessert!';
+ config.store.expandInstanceForEnv = function (env, instance, context, callback) {
+ callback(null, data);
+ };
+ middleware = factory(config);
+ middleware(req, res, function (err) {
+ error = err;
+ nextCallCount += 1;
+ });
+
+ A.areSame(0, nextCallCount, 'next() handler should not have been called');
+ A.areSame(200, statusCode, 'status code should have been set to 200');
+ A.isUndefined(error, 'next() handler should have received an error');
+ A.areSame(data, sendData, 'data should have been sent');
+ }
+ }));
+});
View
134 tests/unit/lib/app/middleware/test-handler-tunnel-type.js
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2011-2013, Yahoo! Inc. All rights reserved.
+ * Copyrights licensed under the New BSD License.
+ * See the accompanying LICENSE file for terms.
+ */
+
+/*global YUI, require*/
+/*jslint nomen:true*/
+
+YUI().use('mojito-test-extra', 'test', function (Y) {
+ 'use strict';
+
+ var A = Y.Assert,
+ AA = Y.ArrayAssert,
+ OA = Y.ObjectAssert,
+
+ factory = require(Y.MOJITO_DIR + 'lib/app/middleware/mojito-handler-tunnel-type'),
+ expandInstanceInvoked = false,
+ error,
+ req,
+ res,
+ sendData,
+ statusCode,
+ nextCallCount,
+ middleware,
+ store,
+ config;
+
+
+ Y.Test.Runner.add(new Y.Test.Case({
+ name: 'tunnel handler type tests',
+
+ setUp: function () {
+ nextCallCount = 0;
+
+ store = {
+ expandInstanceForEnv: function (env, instance, context, callback) {
+ expandInstanceInvoked = true;
+ }
+ };
+
+ config = {
+ store: store
+ };
+
+ req = {
+ url: '/tunnel',
+ _tunnel: {
+ typeReq: {
+ type: 'MojitX'
+ }
+ }
+ };
+
+ res = {
+ send: function (code, data) {
+ sendData = data;
+ statusCode = code;
+ }
+ };
+ },
+
+ tearDown: function () {
+ expandInstanceInvoked = false;
+
+ nextCallCount = 0;
+ sendData = undefined;
+ statusCode = undefined;
+ store = null;
+ config = null;
+ req = null;
+ res = null;
+ middleware = null;
+ error = undefined;
+ },
+
+ 'handler should exit early if not type request': function () {
+ req._tunnel.typeReq = null;
+ middleware = factory(config);
+ middleware(req, res, function () {
+ nextCallCount += 1;
+ });
+
+ A.areSame(1, nextCallCount, 'next() handler should have been called');
+ A.isFalse(expandInstanceInvoked, 'should not have attempted to expand the instance');
+ },
+
+ 'handler should error if "type" is missing': function () {
+ req._tunnel.typeReq.type = null;
+ middleware = factory(config);
+ middleware(req, res, function (err) {
+ error = err;
+ nextCallCount += 1;
+ });
+
+ A.areSame(1, nextCallCount, 'next() handler should have been called');
+ A.isNotUndefined(error, 'next() handler should have received an error');
+ A.areSame(404, res.statusCode, 'status code should have been set to 404');
+ },
+
+ 'handler should error if expandInstanceForEnv errors': function () {
+ config.store.expandInstanceForEnv = function (env, instance, context, callback) {
+ callback(new Error('you have 10 seconds to eat that tomato'));
+ };
+ middleware = factory(config);
+ middleware(req, res, function (err) {
+ error = err;
+ nextCallCount += 1;
+ });
+
+ A.areSame(1, nextCallCount, 'next() handler should have been called');
+ A.areSame(500, res.statusCode, 'status code should have been set to 500');
+ A.isNotUndefined(error, 'next() handler should have received an error');
+ A.isUndefined(sendData, 'data should not have been sent');
+ },
+
+ 'test handler response for success': function () {
+ var data = 'good job, here is your dessert!';
+ config.store.expandInstanceForEnv = function (env, instance, context, callback) {
+ callback(null, data);
+ };
+ middleware = factory(config);
+ middleware(req, res, function (err) {
+ error = err;
+ nextCallCount += 1;
+ });
+
+ A.areSame(0, nextCallCount, 'next() handler should not have been called');
+ A.areSame(200, statusCode, 'status code should have been set to 200');
+ A.isUndefined(error, 'next() handler should have received an error');
+ A.areSame(data, sendData, 'data should have been sent');
+ }
+ }));
+});
Please sign in to comment.
Something went wrong with that request. Please try again.