Skip to content

Commit

Permalink
lint: use standard style
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed May 21, 2016
1 parent f9226e9 commit d4a1ad2
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
@@ -0,0 +1,2 @@
coverage
node_modules
3 changes: 3 additions & 0 deletions .eslintrc
@@ -0,0 +1,3 @@
{
"extends": "standard"
}
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -15,7 +15,7 @@ cache:
- node_modules
before_install:
# Setup Node.js version-specific dependencies
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard istanbul"

# Update Node.js modules
- "test ! -d node_modules || npm prune"
Expand All @@ -24,5 +24,6 @@ script:
# Run test script, depending on istanbul install
- "test ! -z $(npm -ps ls istanbul) || npm test"
- "test -z $(npm -ps ls istanbul) || npm run-script test-travis"
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
after_script:
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
30 changes: 15 additions & 15 deletions index.js
Expand Up @@ -14,10 +14,10 @@
*/

var debug = require('debug')('method-override')
var methods = require('methods');
var parseurl = require('parseurl');
var querystring = require('querystring');
var vary = require('vary');
var methods = require('methods')
var parseurl = require('parseurl')
var querystring = require('querystring')
var vary = require('vary')

/**
* Method Override:
Expand All @@ -41,7 +41,7 @@ var vary = require('vary');
* @api public
*/

module.exports = function methodOverride(getter, options){
module.exports = function methodOverride (getter, options) {
options = options || {}

// get the getter fn
Expand All @@ -54,7 +54,7 @@ module.exports = function methodOverride(getter, options){
? ['POST']
: options.methods

return function methodOverride(req, res, next) {
return function methodOverride (req, res, next) {
var method
var val

Expand Down Expand Up @@ -84,7 +84,7 @@ module.exports = function methodOverride(getter, options){
* Create a getter for the given string.
*/

function createGetter(str) {
function createGetter (str) {
if (str.substr(0, 2).toUpperCase() === 'X-') {
// header getter
return createHeaderGetter(str)
Expand All @@ -97,8 +97,8 @@ function createGetter(str) {
* Create a getter for the given query key name.
*/

function createQueryGetter(key) {
return function(req, res) {
function createQueryGetter (key) {
return function (req, res) {
var url = parseurl(req)
var query = querystring.parse(url.query || '')
return query[key]
Expand All @@ -109,10 +109,10 @@ function createQueryGetter(key) {
* Create a getter for the given header name.
*/

function createHeaderGetter(str) {
function createHeaderGetter (str) {
var header = str.toLowerCase()

return function(req, res) {
return function (req, res) {
// set appropriate Vary header
vary(res, str)

Expand All @@ -125,8 +125,8 @@ function createHeaderGetter(str) {
* Check if node supports `method`.
*/

function supports(method) {
return method
&& typeof method === 'string'
&& methods.indexOf(method.toLowerCase()) !== -1
function supports (method) {
return method &&
typeof method === 'string' &&
methods.indexOf(method.toLowerCase()) !== -1
}
5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -15,6 +15,10 @@
"vary": "~1.1.0"
},
"devDependencies": {
"eslint": "2.10.2",
"eslint-config-standard": "5.3.1",
"eslint-plugin-promise": "1.1.0",
"eslint-plugin-standard": "1.3.2",
"istanbul": "0.4.3",
"mocha": "2.4.5",
"supertest": "1.1.0"
Expand All @@ -28,6 +32,7 @@
"node": ">= 0.8.0"
},
"scripts": {
"lint": "eslint **/*.js",
"test": "mocha --check-leaks --reporter spec --bail test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --check-leaks --reporter dot test/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --check-leaks --reporter spec test/"
Expand Down
5 changes: 5 additions & 0 deletions test/.eslintrc
@@ -0,0 +1,5 @@
{
"env": {
"mocha": true
}
}
54 changes: 27 additions & 27 deletions test/test.js
Expand Up @@ -3,24 +3,24 @@ var http = require('http')
var methodOverride = require('..')
var request = require('supertest')

describe('methodOverride(getter)', function(){
it('should not touch the method by default', function(done){
describe('methodOverride(getter)', function () {
it('should not touch the method by default', function (done) {
var server = createServer()
request(server)
.get('/')
.expect('X-Got-Method', 'GET', done)
})

it('should use X-HTTP-Method-Override by default', function(done){
it('should use X-HTTP-Method-Override by default', function (done) {
var server = createServer()
request(server)
.post('/')
.set('X-HTTP-Method-Override', 'DELETE')
.expect('X-Got-Method', 'DELETE', done)
})

describe('with query', function(){
it('should work missing query', function(done){
describe('with query', function () {
it('should work missing query', function (done) {
var server = createServer('_method')

request(server)
Expand All @@ -29,7 +29,7 @@ describe('methodOverride(getter)', function(){
.expect('X-Got-Method', 'POST', done)
})

it('should be case in-sensitive', function(done){
it('should be case in-sensitive', function (done) {
var server = createServer('_method')

request(server)
Expand All @@ -38,7 +38,7 @@ describe('methodOverride(getter)', function(){
.expect('X-Got-Method', 'DELETE', done)
})

it('should ignore invalid methods', function(done){
it('should ignore invalid methods', function (done) {
var server = createServer('_method')

request(server)
Expand All @@ -47,7 +47,7 @@ describe('methodOverride(getter)', function(){
.expect('X-Got-Method', 'POST', done)
})

it('should handle key referencing array', function(done){
it('should handle key referencing array', function (done) {
var server = createServer('_method')

var test = request(server).post('/')
Expand All @@ -56,7 +56,7 @@ describe('methodOverride(getter)', function(){
test.expect('X-Got-Method', 'DELETE', done)
})

it('should only work with POST', function(done){
it('should only work with POST', function (done) {
var server = createServer('_method')

request(server)
Expand All @@ -66,44 +66,44 @@ describe('methodOverride(getter)', function(){
})
})

describe('with header', function(){
describe('with header', function () {
var server
before(function () {
server = createServer('X-HTTP-Method-Override')
})

it('should work missing header', function(done){
it('should work missing header', function (done) {
request(server)
.post('/')
.set('Content-Type', 'application/json')
.expect('X-Got-Method', 'POST', done)
})

it('should be case in-sensitive', function(done){
it('should be case in-sensitive', function (done) {
request(server)
.post('/')
.set('Content-Type', 'application/json')
.set('X-HTTP-Method-Override', 'DELete')
.expect('X-Got-Method', 'DELETE', done)
})

it('should ignore invalid methods', function(done){
it('should ignore invalid methods', function (done) {
request(server)
.post('/')
.set('Content-Type', 'application/json')
.set('X-HTTP-Method-Override', 'BOGUS')
.expect('X-Got-Method', 'POST', done)
})

it('should handle multiple headers', function(done){
it('should handle multiple headers', function (done) {
request(server)
.post('/')
.set('Content-Type', 'application/json')
.set('X-HTTP-Method-Override', 'DELETE, PUT')
.expect('X-Got-Method', 'DELETE', done)
})

it('should set Vary header', function(done){
it('should set Vary header', function (done) {
request(server)
.post('/')
.set('Content-Type', 'application/json')
Expand All @@ -112,7 +112,7 @@ describe('methodOverride(getter)', function(){
.expect('X-Got-Method', 'DELETE', done)
})

it('should set Vary header even with no override', function(done){
it('should set Vary header even with no override', function (done) {
request(server)
.post('/')
.set('Content-Type', 'application/json')
Expand All @@ -121,30 +121,30 @@ describe('methodOverride(getter)', function(){
})
})

describe('with function', function(){
describe('with function', function () {
var server
before(function () {
server = createServer(function(req){
server = createServer(function (req) {
return req.headers['x-method-override'] || 'PaTcH'
})
})

it('should work missing header', function(done){
it('should work missing header', function (done) {
request(server)
.post('/')
.set('Content-Type', 'application/json')
.expect('X-Got-Method', 'PATCH', done)
})

it('should be case in-sensitive', function(done){
it('should be case in-sensitive', function (done) {
request(server)
.post('/')
.set('Content-Type', 'application/json')
.set('X-Method-Override', 'DELete')
.expect('X-Got-Method', 'DELETE', done)
})

it('should ignore invalid methods', function(done){
it('should ignore invalid methods', function (done) {
request(server)
.post('/')
.set('Content-Type', 'application/json')
Expand All @@ -153,8 +153,8 @@ describe('methodOverride(getter)', function(){
})
})

describe('given "options.methods"', function(){
it('should allow other methods', function(done){
describe('given "options.methods"', function () {
it('should allow other methods', function (done) {
var server = createServer('X-HTTP-Method-Override', { methods: ['POST', 'PATCH'] })
request(server)
.patch('/')
Expand All @@ -163,7 +163,7 @@ describe('methodOverride(getter)', function(){
.expect('X-Got-Method', 'DELETE', done)
})

it('should allow all methods', function(done){
it('should allow all methods', function (done) {
var server = createServer('X-HTTP-Method-Override', { methods: null })
request(server)
.patch('/')
Expand All @@ -172,8 +172,8 @@ describe('methodOverride(getter)', function(){
.expect('X-Got-Method', 'DELETE', done)
})

it('should not call getter when method not allowed', function(done){
var server = createServer(function(req){ return 'DELETE' })
it('should not call getter when method not allowed', function (done) {
var server = createServer(function (req) { return 'DELETE' })
request(server)
.patch('/')
.set('Content-Type', 'application/json')
Expand All @@ -182,7 +182,7 @@ describe('methodOverride(getter)', function(){
})
})

function createServer(getter, opts, fn) {
function createServer (getter, opts, fn) {
var _override = methodOverride(getter, opts)
return http.createServer(function (req, res) {
fn && fn(req, res)
Expand Down

0 comments on commit d4a1ad2

Please sign in to comment.