Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit f6847841780775b92b9b411239ea122a711f4b5a 1 parent 3922382
@nfriedly authored
Showing with 137 additions and 1 deletion.
  1. +27 −1 README.md
  2. +27 −0 index.js
  3. +19 −0 package.json
  4. +64 −0 spec/methodOverride_spec.js
View
28 README.md
@@ -1,4 +1,30 @@
connect-method-override-get
===========================
-Like Connect.js's method override, except it supports GET requests also.
+Like (Connect's)[http://www.senchalabs.org/connect/] (methodOverride)[http://www.senchalabs.org/connect/methodOverride.html], except it supports GET requests also.
+
+Usage with a Connect app
+-------------------------
+
+ var app = connect();
+ app.use(connect.bodyParser());
+ app.use(require('methodOverrideGet')({key: '_method'));
+
+Usage with an Express app
+-------------------------
+
+ var app = express();
+ app.use(express.bodyParser());
+ app.use(require('methodOverrideGet')({key: '_method'));
+
+
+MIT License
+------------
+
+Copyright (c) 2010 by (Nathan Friedly)[http://nfriedly.com]
+
+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.
View
27 index.js
@@ -0,0 +1,27 @@
+module.exports = function(key) {
+ key = key || "_method";
+ return function methodOverride(req, res, next) {
+ req.originalMethod = req.originalMethod || req.method;
+
+ // req.body
+ if (req.body && key in req.body) {
+ req.method = req.body[key].toUpperCase();
+ delete req.body[key];
+
+ // req.query
+ } else if (req.query && key in req.query) {
+ req.method = req.query[key].toUpperCase();
+ delete req.query[key]
+ if (req.method != "GET") {
+ req.body = req.query;
+ // delete req.query; // can't do this because the jsonp doce depends on req.query
+ }
+
+ // check X-HTTP-Method-Override
+ } else if (req.headers['x-http-method-override']) {
+ req.method = req.headers['x-http-method-override'].toUpperCase();
+ }
+
+ next();
+ };
+}
View
19 package.json
@@ -0,0 +1,19 @@
+{
+ "name": "connect-method-override-get",
+ "description": "Like Connect's methodOverride, except it supports GET requests also. 100% Express.js comaptible",
+ "version": "1.0.0",
+ "author": "Nathan Friedly - http://nfriedly.com/",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/nfriedly/connect-method-override-get"
+ },
+ "main": "index.js",
+ "keywords": [ "Connect", "Express", "methodOverride", "GET", "change method", "override method" ],
+ "license": "MIT",
+ "devDependencies": {
+ "jasmine-node": "1.0.26"
+ },
+ "scripts": {
+ "test": "node node_modules/jasmine-node/lib/jasmine-node/cli.js ./"
+ }
+}
View
64 spec/methodOverride_spec.js
@@ -0,0 +1,64 @@
+var methodOverride = require('../index');
+
+describe('methodOverride', function() {
+ var req, res, next, mo;
+
+ beforeEach(function() {
+ req = {headers:{}, method: 'GET'};
+ res = {};
+ next = jasmine.createSpy('next');
+ mo = methodOverride();
+ });
+
+ afterEach(function() {
+ expect(next).toHaveBeenCalled();
+ });
+
+ it('should work in the notmal (non-ovrridden) case', function() {
+ mo(req, res, next);
+ });
+
+ it('should store the original method and override the req.method', function() {
+ req.query = query = {
+ _method: "asdf",
+ foo: "bar"
+ };
+ mo(req, res, next);
+ expect(req.originalMethod).toBe('GET');
+ expect(req.method).toBe('ASDF');
+ });
+
+ it('should work with with POST method overrides', function() {
+ req.method = 'POST';
+ var body = {
+ _method: "asdf",
+ foo: "bar"
+ };
+ req.body = body;
+ mo(req, res, next);
+ expect(req.method).toBe('ASDF');
+ expect(req.body._method).not.toBeDefined();
+ });
+ it('should work with with GET method overrides', function() {
+ var query = {
+ _method: "asdf",
+ foo: "bar"
+ };
+ req.query = query;
+ mo(req, res, next);
+ expect(req.method).toBe('ASDF');
+ expect(req.body).toBe(query);
+ expect(req.body._method).not.toBeDefined();
+ });
+ it('should work with with header method overrides', function() {
+ req.method = 'POST';
+ var body = {
+ foo: "bar"
+ };
+ req.body = body;
+ req.headers['x-http-method-override'] = 'asdf';
+ mo(req, res, next);
+ expect(req.method).toBe('ASDF');
+ expect(req.body._method).not.toBeDefined();
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.