Browse files

Merge pull request #12 from ekashida/content-type

add ability to customize the content-type header
  • Loading branch information...
2 parents be374b9 + 823f75d commit d7d8345077f2264646e3d0724ec02423cc7252a3 @davglass committed Apr 22, 2013
Showing with 68 additions and 19 deletions.
  1. +35 −18 lib/scheme.js
  2. +1 −1 package.json
  3. +32 −0 tests/tests.js
View
53 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);
};
View
2 package.json
@@ -2,7 +2,7 @@
"name": "echoecho",
"description": "Simple testing echo responses to HTTP requests",
"author": "Dav Glass <davglass@gmail.com>",
- "version": "0.1.6",
+ "version": "0.1.7",
"devDependencies": {
"vows": "*",
"yui-lint": "~0.1.1",
View
32 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);

0 comments on commit d7d8345

Please sign in to comment.