Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added ability to send data from status code methods

  • Loading branch information...
commit c565a0a1f49e4c0fee39306b007e76cccdaf7a1f 1 parent 76fb7a8
@caolan authored
Showing with 72 additions and 49 deletions.
  1. +31 −22 lib/quickresponse.js
  2. +41 −27 test/test-response.js
View
53 lib/quickresponse.js
@@ -1,14 +1,18 @@
module.exports = function(){
return function(req, res, next){
- res.status = function(code){
- res._status = code;
- return res;
- };
+ ///// default response settings /////
+
+ res._headers = {'Content-Type': 'text/html'};
+ res._status = 200;
+
+
+ ///// private helper methods /////
var withStatus = function(code){
- return function(){
- return res.status(code);
+ return function(data){
+ return data ? res.status(code).send(data):
+ res.status(code);
};
};
var redirection = function(code){
@@ -22,6 +26,23 @@ module.exports = function(){
return res.status(code);
};
}
+ var respondWithType = function(type){
+ return function(data){
+ if(!res._headers){
+ res._headers = {};
+ };
+ res._headers['Content-Type'] = type;
+ return res.send(data);
+ }
+ };
+
+ ///// exported methods /////
+
+ // use specific status code
+ res.status = function(code){
+ res._status = code;
+ return res;
+ };
// success
res.ok = withStatus(200);
@@ -45,16 +66,6 @@ module.exports = function(){
// server error
res.error = withStatus(500, 'error');
- var respondWithType = function(type){
- return function(data){
- if(!res._headers){
- res._headers = {};
- };
- res._headers['Content-Type'] = type;
- res.send(data);
- return null;
- }
- };
// mime types
res.text = respondWithType('text/plain');
@@ -72,18 +83,16 @@ module.exports = function(){
if(typeof data === 'object'){
data = JSON.stringify(data);
}
- if(!res._headers){
- res._headers = {};
- };
res._headers['Content-Type'] = 'application/json';
- res.send(data);
- return null;
+ return res.send(data);
};
+ // respond with given data using current header and status code
res.send = function(data){
- res.writeHead(res._status || 200, res._headers || {});
+ res.writeHead(res._status, res._headers);
res.write(data || '');
res.end();
+ return null;
};
// finished decorating the request object
View
68 test/test-response.js
@@ -1,7 +1,11 @@
+// TODO: add support for following styles:
+// req.text().notFound('not found');
+// req.headers({some:header}).ok('done');
+
var quickresponse = require('quickresponse');
-exports.testStatus = function(test){
+exports.status = function(test){
var res = {};
quickresponse()(null, res, function(){
test.same(res.status(200), res);
@@ -15,30 +19,40 @@ exports.testStatus = function(test){
var statusTest = function(code, name){
return function(test){
+ test.expect(6);
var res = {};
quickresponse()(null, res, function(){
test.same(res[name](), res);
test.equals(res._status, code);
});
+ var res2 = {};
+ quickresponse()(null, res2, function(){
+ res2.send = function(data){
+ test.equals(this._status, code);
+ test.same(this._headers, {'Content-Type':'text/html'});
+ test.equals(data, 'content');
+ };
+ test.equals(res2[name]('content'), null);
+ });
test.done();
};
};
// success codes
-exports.testOk = statusTest(200, 'ok');
-exports.testCreated = statusTest(201, 'created');
-exports.testAccepted= statusTest(202, 'accepted'); // remove this ???
+exports.ok = statusTest(200, 'ok');
+exports.created = statusTest(201, 'created');
+exports.accepted = statusTest(202, 'accepted'); // remove this ???
// client error code
-exports.testBadRequest = statusTest(400, 'badRequest');
-exports.testForbidden = statusTest(403, 'forbidden');
-exports.testNotFound = statusTest(404, 'notFound');
-exports.testConflict = statusTest(409, 'conflict');
-exports.testGone = statusTest(410, 'gone');
-exports.testNotAllowed = statusTest(405, 'notAllowed');
+exports.badRequest = statusTest(400, 'badRequest');
+exports.forbidden = statusTest(403, 'forbidden');
+exports.notFound = statusTest(404, 'notFound');
+exports.conflict = statusTest(409, 'conflict');
+exports.gone = statusTest(410, 'gone');
+exports.notAllowed = statusTest(405, 'notAllowed');
// server error codes
-exports.testError = statusTest(500, 'error');
+exports.error = statusTest(500, 'error');
var redirectionTest = function(code, name){
return function(test){
@@ -54,9 +68,9 @@ var redirectionTest = function(code, name){
};
// redirection codes
-exports.testNotModified = statusTest(304, 'notModified');
-exports.testMoved = redirectionTest(301, 'moved');
-exports.testRedirect = redirectionTest(302, 'redirect');
+exports.notModified = statusTest(304, 'notModified');
+exports.moved = redirectionTest(301, 'moved');
+exports.redirect = redirectionTest(302, 'redirect');
var mimeTypeTest = function(type, name){
return function(test){
@@ -74,20 +88,20 @@ var mimeTypeTest = function(type, name){
};
};
-exports.testText = mimeTypeTest('text/plain', 'plain');
-exports.testPlain = mimeTypeTest('text/plain', 'text');
-exports.testHtml = mimeTypeTest('text/html', 'html');
-exports.testXhtml = mimeTypeTest('application/xhtml+xml', 'xhtml');
-exports.testCss = mimeTypeTest('text/css', 'css');
-exports.testXml = mimeTypeTest('text/xml', 'xml');
-exports.testAtom = mimeTypeTest('application/atom+xml', 'atom');
-exports.testRss = mimeTypeTest('application/rss+xml', 'rss');
+exports.text = mimeTypeTest('text/plain', 'plain');
+exports.plain = mimeTypeTest('text/plain', 'text');
+exports.html = mimeTypeTest('text/html', 'html');
+exports.xhtml = mimeTypeTest('application/xhtml+xml', 'xhtml');
+exports.css = mimeTypeTest('text/css', 'css');
+exports.xml = mimeTypeTest('text/xml', 'xml');
+exports.atom = mimeTypeTest('application/atom+xml', 'atom');
+exports.rss = mimeTypeTest('application/rss+xml', 'rss');
// should this be text/javscript for better browser support?
-exports.testJavascript = mimeTypeTest('application/javascript', 'javascript');
-exports.testJson = mimeTypeTest('application/json', 'json');
+exports.javascript = mimeTypeTest('application/javascript', 'javascript');
+exports.json = mimeTypeTest('application/json', 'json');
-exports.testSend = function(test){
+exports.send = function(test){
test.expect(4);
var res = {
writeHead: function(code, headers){
@@ -109,11 +123,11 @@ exports.testSend = function(test){
test.done();
};
-exports.testSendDefaults = function(test){
+exports['send defaults'] = function(test){
test.expect(4);
var res = {
writeHead: function(code, headers){
- test.same(headers, {});
+ test.same(headers, {'Content-Type': 'text/html'});
test.equals(code, 200);
},
write: function(data){
Please sign in to comment.
Something went wrong with that request. Please try again.