This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds "method" field validation
- Loading branch information
1 parent
71a02bc
commit b575e93
Showing
8 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
lib/next/test/unit/units/normalize/normalizeMethod.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const { assert } = require('chai'); | ||
const { | ||
normalizeMethod | ||
} = require('../../../../units/normalize/normalizeMethod'); | ||
|
||
describe('normalizeMethod', () => { | ||
it('removes trailing spaces', () => { | ||
assert.equal(normalizeMethod(' POST '), 'POST'); | ||
}); | ||
|
||
it('converts to uppercase', () => { | ||
assert.equal(normalizeMethod('pUt'), 'PUT'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const { assert } = require('chai'); | ||
const { validateMethod } = require('../../../units/validateMethod'); | ||
|
||
describe('validateMethod', () => { | ||
describe('given matching methods', () => { | ||
const result = validateMethod({ method: 'GET' }, { method: 'GET' }); | ||
|
||
it('has "isValid" as "true"', () => { | ||
assert.propertyVal(result, 'isValid', true); | ||
}); | ||
|
||
it('has "null" validator', () => { | ||
assert.isNull(result.validator); | ||
}); | ||
|
||
it('has "text/vnd.apiary.method" real type', () => { | ||
assert.propertyVal(result, 'realType', 'text/vnd.apiary.method'); | ||
}); | ||
|
||
it('has "text/vnd.apiary.method" expected type', () => { | ||
assert.propertyVal(result, 'expectedType', 'text/vnd.apiary.method'); | ||
}); | ||
|
||
it('has no errors', () => { | ||
assert.lengthOf(result.errors, 0); | ||
}); | ||
}); | ||
|
||
describe('given non-matching methods', () => { | ||
const result = validateMethod({ method: 'GET' }, { method: 'POST' }); | ||
|
||
it('returns "isValid" as "false"', () => { | ||
assert.propertyVal(result, 'isValid', false); | ||
}); | ||
|
||
it('has "null" validator', () => { | ||
assert.propertyVal(result, 'validator', null); | ||
}); | ||
|
||
it('has "text/vnd.apiary.method" real type', () => { | ||
assert.propertyVal(result, 'realType', 'text/vnd.apiary.method'); | ||
}); | ||
|
||
it('has "text/vnd.apiary.method" expected type', () => { | ||
assert.propertyVal(result, 'expectedType', 'text/vnd.apiary.method'); | ||
}); | ||
|
||
describe('produces an error', () => { | ||
it('exactly one error', () => { | ||
assert.lengthOf(result.errors, 1); | ||
}); | ||
|
||
it('has "error" severity', () => { | ||
assert.propertyVal(result.errors[0], 'severity', 'error'); | ||
}); | ||
|
||
it('has explanatory message', () => { | ||
assert.propertyVal( | ||
result.errors[0], | ||
'message', | ||
'Expected "method" field to equal "POST", but got "GET".' | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('given expected, but no real method', () => { | ||
const result = validateMethod({ method: '' }, { method: 'PATCH' }); | ||
|
||
it('returns "isValid" as "false"', () => { | ||
assert.propertyVal(result, 'isValid', false); | ||
}); | ||
|
||
it('has "null" validator', () => { | ||
assert.propertyVal(result, 'validator', null); | ||
}); | ||
|
||
it('has "text/vnd.apiary.method" real type', () => { | ||
assert.propertyVal(result, 'realType', 'text/vnd.apiary.method'); | ||
}); | ||
|
||
it('has "text/vnd.apiary.method" expected type', () => { | ||
assert.propertyVal(result, 'expectedType', 'text/vnd.apiary.method'); | ||
}); | ||
|
||
describe('produces an error', () => { | ||
it('exactly one error', () => { | ||
assert.lengthOf(result.errors, 1); | ||
}); | ||
|
||
it('has "error" severity', () => { | ||
assert.propertyVal(result.errors[0], 'severity', 'error'); | ||
}); | ||
|
||
it('has explanatory message', () => { | ||
assert.propertyVal( | ||
result.errors[0], | ||
'message', | ||
'Expected "method" field to equal "PATCH", but got "".' | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Normalizes given HTTP message method. | ||
* @param {string} method | ||
*/ | ||
const normalizeMethod = (method) => { | ||
return method.trim().toUpperCase(); | ||
}; | ||
|
||
module.exports = { normalizeMethod }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const APIARY_METHOD_TYPE = 'text/vnd.apiary.method'; | ||
|
||
function validateMethod(real, expected) { | ||
const { method: realMethod } = real; | ||
const { method: expectedMethod } = expected; | ||
const errors = []; | ||
const isValid = realMethod === expectedMethod; | ||
|
||
if (!isValid) { | ||
errors.push({ | ||
message: `Expected "method" field to equal "${expectedMethod}", but got "${realMethod}".`, | ||
severity: 'error' | ||
}); | ||
} | ||
|
||
return { | ||
isValid, | ||
validator: null, | ||
realType: APIARY_METHOD_TYPE, | ||
expectedType: APIARY_METHOD_TYPE, | ||
errors | ||
}; | ||
} | ||
|
||
module.exports = { validateMethod }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters