Skip to content

Commit

Permalink
add ability to customize the content-type header
Browse files Browse the repository at this point in the history
  • Loading branch information
ekashida committed Apr 22, 2013
1 parent be374b9 commit 823f75d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
53 changes: 35 additions & 18 deletions lib/scheme.js
Expand Up @@ -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+)/;

Expand All @@ -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;
};
Expand All @@ -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) {
Expand All @@ -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.
Expand Down Expand Up @@ -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);
};
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
32 changes: 32 additions & 0 deletions tests/tests.js
Expand Up @@ -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 823f75d

Please sign in to comment.