Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit f4aa0cfe323fbaa72dda2f03b65169462963e646 @jaredhanson committed Jul 15, 2012
Showing with 160 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +15 −0 .npmignore
  3. +20 −0 LICENSE
  4. +8 −0 Makefile
  5. +3 −0 README.md
  6. +1 −0 index.js
  7. +36 −0 lib/response-redirect.js
  8. +28 −0 package.json
  9. +44 −0 test/response-redirect-test.js
5 .gitignore
@@ -0,0 +1,5 @@
+# Mac OS X
+.DS_Store
+# Node.js
+node_modules
+npm-debug.log
15 .npmignore
@@ -0,0 +1,15 @@
+# Project
+README.md
+Makefile
+docs/
+examples/
+test/
+
+# Mac OS X
+.DS_Store
+# Node.js
+.npmignore
+node_modules/
+npm-debug.log
+# Git
+.git*
20 LICENSE
@@ -0,0 +1,20 @@
+(The MIT License)
+
+Copyright (c) 2012 Jared Hanson
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 Makefile
@@ -0,0 +1,8 @@
+NODE = node
+TEST = ./node_modules/.bin/vows
+TESTS ?= test/*-test.js
+
+test:
+ @NODE_ENV=test NODE_PATH=lib $(TEST) $(TEST_FLAGS) $(TESTS)
+
+.PHONY: test
3 README.md
@@ -0,0 +1,3 @@
+# response-redirect
+
+ `response.redirect()` prototype method.
1 index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/response-redirect');
36 lib/response-redirect.js
@@ -0,0 +1,36 @@
+/**
+ * Module dependencies.
+ */
+
+var http = require('http')
+ , statusCodes = http.STATUS_CODES;
+
+/**
+ * Redirect to the given `url` with optional response `status`
+ * defaulting to 302.
+ *
+ * Examples:
+ *
+ * res.redirect('http://example.com');
+ * res.redirect(301, 'http://example.com');
+ *
+ * @param {Number} code
+ * @param {String} url
+ * @api public
+ */
+
+exports = module.exports = function(url){
+ var status = 302;
+
+ // allow status / url
+ if (2 == arguments.length) {
+ status = url;
+ url = arguments[1];
+ }
+
+ // Respond
+ this.statusCode = status;
+ this.setHeader('Location', url);
+ this.setHeader('Content-Length', '0');
+ this.end();
+};
28 package.json
@@ -0,0 +1,28 @@
+{
+ "name": "response-redirect",
+ "version": "0.0.0",
+ "description": "response.redirect() prototype method",
+ "keywords": ["http", "connect", "express"],
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/jaredhanson/node-response-redirect.git"
+ },
+ "bugs": {
+ "url": "http://github.com/jaredhanson/node-response-redirect/issues"
+ },
+ "author": { "name": "Jared Hanson", "email": "jaredhanson@gmail.com", "url": "http://www.jaredhanson.net/" },
+ "licenses": [ {
+ "type": "MIT",
+ "url": "http://www.opensource.org/licenses/MIT"
+ } ],
+ "main": "index",
+ "dependencies": {
+ },
+ "devDependencies": {
+ "vows": "0.6.x"
+ },
+ "scripts": {
+ "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
+ },
+ "engines": { "node": ">= 0.4.0" }
+}
44 test/response-redirect-test.js
@@ -0,0 +1,44 @@
+var vows = require('vows');
+var assert = require('assert');
+var util = require('util');
+var redirect = require('response-redirect');
+
+
+function MockResponse() {
+ this._headers = {};
+}
+
+MockResponse.prototype.setHeader = function(name, value) {
+ this._headers[name] = value;
+}
+
+MockResponse.prototype.end = function(data, encoding) {
+ this._data = data;
+}
+
+
+vows.describe('redirect').addBatch({
+
+ 'should redirect' : function() {
+ var res = new MockResponse();
+ res.redirect = redirect;
+
+ res.redirect('http://example.com');
+
+ assert.equal(res.statusCode, 302);
+ assert.equal(res._headers['Location'], 'http://example.com');
+ assert.equal(res._headers['Content-Length'], '0');
+ },
+
+ 'should redirect with status' : function() {
+ var res = new MockResponse();
+ res.redirect = redirect;
+
+ res.redirect(301, 'http://example.com');
+
+ assert.equal(res.statusCode, 301);
+ assert.equal(res._headers['Location'], 'http://example.com');
+ assert.equal(res._headers['Content-Length'], '0');
+ },
+
+}).export(module);

0 comments on commit f4aa0cf

Please sign in to comment.