From 823f75d0b1c5bdc226a73906fac3784f7b0d386a Mon Sep 17 00:00:00 2001 From: Eugene Kashida Date: Mon, 22 Apr 2013 05:24:13 -0700 Subject: [PATCH] add ability to customize the content-type header --- lib/scheme.js | 53 +++++++++++++++++++++++++++++++++----------------- package.json | 2 +- tests/tests.js | 32 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 19 deletions(-) diff --git a/lib/scheme.js b/lib/scheme.js index 998b052..3849e12 100644 --- a/lib/scheme.js +++ b/lib/scheme.js @@ -12,6 +12,11 @@ var headers = { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'text/plain' }; +var CONTENT_TYPE = { + js: 'application/javascript', + css: 'text/css', + json: 'application/json' +}; var RE_DELAY_RANGE = /(\d+)-(\d+)/; @@ -25,6 +30,21 @@ function mix(reciever, sender) { return reciever; } +function done(req, res, code, headers, body) { + var query = parse(req.url).query, + parsed = qs.parse(query), + type = parsed.type; + + if (CONTENT_TYPE[type]) { + headers = mix({ + 'Content-Type': CONTENT_TYPE[type] + }, headers); + } + + res.writeHead(code, headers); + res.end(body); +} + var rand = exports.rand = function (min, max) { return Math.random() * (max - min) + min; }; @@ -44,17 +64,17 @@ exports.status = function (code, res) { }; exports.get = function (req, res, body) { - var code = ((req.method === 'GET' || req.method === 'HEAD' || req.method === 'OPTIONS') ? 200 : 403); + var code = ((req.method === 'GET' || req.method === 'HEAD' || req.method === 'OPTIONS') ? 200 : 403), + content = body || parse(req.url).query || ''; - res.writeHead(code, headers); - res.end(body || parse(req.url).query || ''); + done(req, res, code, headers, content); }; exports['delete'] = function (req, res, body) { - var code = ((req.method === 'DELETE') ? 200 : 403); + var code = ((req.method === 'DELETE') ? 200 : 403), + content = body || parse(req.url).query || ''; - res.writeHead(code, headers); - res.end(body || parse(req.url).query || ''); + done(req, res, code, headers, content); }; var post = function (method, req, res, body) { @@ -79,8 +99,7 @@ var post = function (method, req, res, body) { sent = true; - res.writeHead(code, headers); - res.end(body); + done(req, res, code, headers, body); } // Check if we have data, and aid Express' `req` object. @@ -136,8 +155,8 @@ exports.delay = function (req, res, body) { if (scheme && exports[scheme]) { exports[scheme](req, res, body); } else { - res.writeHead(200, headers); - res.end('waited for ' + seconds + ' seconds'); + done(req, res, 200, headers, 'waited for ' + + seconds + ' seconds'); } }, seconds * 1000); }; @@ -160,10 +179,9 @@ exports.json = function (req, res, body) { json = '{ "echo": true }'; } - res.writeHead(200, mix({ - 'Content-Type': 'application/json' - }, headers)); - res.end(json); + done(req, res, 200, mix({ + 'Content-Type': CONTENT_TYPE.json + }, headers), json); }; exports.jsonp = function (req, res) { @@ -208,8 +226,7 @@ exports.jsonp = function (req, res) { json = callback + '(' + JSON.stringify(content) + ');'; } - res.writeHead(code, mix({ - 'Content-Type': 'application/json' - }, headers)); - res.end(json); + done(req, res, code, mix({ + 'Content-Type': CONTENT_TYPE.json + }, headers), json); }; diff --git a/package.json b/package.json index 4fc5b42..5854b04 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "echoecho", "description": "Simple testing echo responses to HTTP requests", "author": "Dav Glass ", - "version": "0.1.6", + "version": "0.1.7", "devDependencies": { "vows": "*", "yui-lint": "~0.1.1", diff --git a/tests/tests.js b/tests/tests.js index ce8326c..7b00e98 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -902,5 +902,37 @@ testRoutes.forEach(function (route) { = assertDelayedRoute(route.name, route.method); }); +// -- `type` query parameter tests -------------------------------------------- + +function assertContentType(route, method) { + var path = '/foo/bar/baz/echo/' + route + '?type=css'; + context = { + topic: function() { + fetch({ + method: method, + path: path + }, this.callback); + } + }; + + context[route + ' route should respond with specified content type'] = function (res) { + assert.equal(res.headers['content-type'], 'text/css'); + } + + return context; +} + +tests['should be loaded'] + ['and should load paths'] + ['and should respond with specified content type'] = {}; + +testRoutes.forEach(function (route) { + tests['should be loaded'] + ['and should load paths'] + ['and should respond with specified content type'] + ['for route ' + route.name] + = assertContentType(route.name, route.method); +}); + vows.describe('echoecho').addBatch(tests).export(module);