Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

code so far

  • Loading branch information...
commit 76fb7a8a5ba1285a0d281e706c9b4b7f0f3246de 1 parent c67e782
@caolan authored
Showing with 248 additions and 0 deletions.
  1. +3 −0  index.js
  2. +93 −0 lib/quickresponse.js
  3. +22 −0 test.js
  4. +130 −0 test/test-response.js
View
3  index.js
@@ -0,0 +1,3 @@
+// This file is just added for convenience so this repository can be
+// directly checked out into a project's deps folder
+module.exports = require('./lib/quickresponse');
View
93 lib/quickresponse.js
@@ -0,0 +1,93 @@
+module.exports = function(){
+ return function(req, res, next){
+
+ res.status = function(code){
+ res._status = code;
+ return res;
+ };
+
+ var withStatus = function(code){
+ return function(){
+ return res.status(code);
+ };
+ };
+ var redirection = function(code){
+ return function(loc){
+ if(loc){
+ if(!res._headers){
+ res._headers = {};
+ };
+ res._headers.Location = loc;
+ }
+ return res.status(code);
+ };
+ }
+
+ // success
+ res.ok = withStatus(200);
+ res.created = withStatus(201);
+ res.accepted = withStatus(202);
+
+ // redirection
+ res.moved = redirection(301, 'moved');
+ res.notModified = withStatus(304);
+ res.redirect = redirection(302);
+
+ // client error
+ res.badRequest = withStatus(400);
+ res.unauthorized = withStatus(401);
+ res.forbidden = withStatus(403);
+ res.notFound = withStatus(404);
+ res.notAllowed = withStatus(405);
+ res.conflict = withStatus(409);
+ res.gone = withStatus(410);
+
+ // 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');
+ res.plain = res.text;
+
+ res.html = respondWithType('text/html');
+ res.xhtml = respondWithType('application/xhtml+xml');
+ res.css = respondWithType('text/css');
+ res.xml = respondWithType('text/xml');
+ res.atom = respondWithType('application/atom+xml');
+ res.rss = respondWithType('application/rss+xml');
+ res.javascript = respondWithType('application/javascript');
+
+ res.json = function(data){
+ if(typeof data === 'object'){
+ data = JSON.stringify(data);
+ }
+ if(!res._headers){
+ res._headers = {};
+ };
+ res._headers['Content-Type'] = 'application/json';
+ res.send(data);
+ return null;
+ };
+
+ res.send = function(data){
+ res.writeHead(res._status || 200, res._headers || {});
+ res.write(data || '');
+ res.end();
+ };
+
+ // finished decorating the request object
+ next();
+
+ };
+};
View
22 test.js
@@ -0,0 +1,22 @@
+#!/usr/local/bin/node
+
+require.paths.push(__dirname);
+require.paths.push(__dirname + '/deps');
+require.paths.push(__dirname + '/lib');
+
+try {
+ var testrunner = require('nodeunit').testrunner;
+}
+catch(e) {
+ var sys = require('sys');
+ sys.puts("Cannot find nodeunit module.");
+ sys.puts("You can download submodules for this project by doing:");
+ sys.puts("");
+ sys.puts(" git submodule init");
+ sys.puts(" git submodule update");
+ sys.puts("");
+ process.exit();
+}
+
+process.chdir(__dirname);
+testrunner.run(['test']);
View
130 test/test-response.js
@@ -0,0 +1,130 @@
+var quickresponse = require('quickresponse');
+
+
+exports.testStatus = function(test){
+ var res = {};
+ quickresponse()(null, res, function(){
+ test.same(res.status(200), res);
+ test.equals(res._status, 200);
+ test.same(res.status(404), res);
+ test.equals(res._status, 404);
+ });
+ test.done();
+};
+
+
+var statusTest = function(code, name){
+ return function(test){
+ var res = {};
+ quickresponse()(null, res, function(){
+ test.same(res[name](), res);
+ test.equals(res._status, code);
+ });
+ test.done();
+ };
+};
+
+// success codes
+exports.testOk = statusTest(200, 'ok');
+exports.testCreated = statusTest(201, 'created');
+exports.testAccepted= 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');
+
+// server error codes
+exports.testError = statusTest(500, 'error');
+
+var redirectionTest = function(code, name){
+ return function(test){
+ var res = {};
+ quickresponse()(null, res, function(){
+ test.same(res[name](), res);
+ test.equals(res._status, code);
+ res[name]('loc');
+ test.equals(res._headers.Location, 'loc');
+ });
+ test.done();
+ };
+};
+
+// redirection codes
+exports.testNotModified = statusTest(304, 'notModified');
+exports.testMoved = redirectionTest(301, 'moved');
+exports.testRedirect = redirectionTest(302, 'redirect');
+
+var mimeTypeTest = function(type, name){
+ return function(test){
+ test.expect(4);
+ var res = {};
+ quickresponse()(null, res, function(){
+ res.send = function(data){
+ test.ok(true, 'send called');
+ test.equals(this._headers['Content-Type'], type);
+ test.equals(data, 'content');
+ };
+ test.equals(res[name]('content'), null);
+ });
+ test.done();
+ };
+};
+
+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');
+// should this be text/javscript for better browser support?
+exports.testJavascript = mimeTypeTest('application/javascript', 'javascript');
+exports.testJson = mimeTypeTest('application/json', 'json');
+
+
+exports.testSend = function(test){
+ test.expect(4);
+ var res = {
+ writeHead: function(code, headers){
+ test.same(headers, {headers: 'test'});
+ test.equals(code, 404);
+ },
+ write: function(data){
+ test.equals(data, 'data');
+ },
+ end: function(){
+ test.ok(true, 'end called');
+ }
+ };
+ quickresponse()(null, res, function(){
+ res._headers = {headers: 'test'};
+ res._status = 404;
+ res.send('data');
+ });
+ test.done();
+};
+
+exports.testSendDefaults = function(test){
+ test.expect(4);
+ var res = {
+ writeHead: function(code, headers){
+ test.same(headers, {});
+ test.equals(code, 200);
+ },
+ write: function(data){
+ test.equals(data, '');
+ },
+ end: function(){
+ test.ok(true, 'end called');
+ }
+ };
+ quickresponse()(null, res, function(){
+ res.send();
+ });
+ test.done();
+};
Please sign in to comment.
Something went wrong with that request. Please try again.