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.
test: adds granular unit tests for "validateBody"
- Loading branch information
1 parent
9c50d2f
commit 8630403
Showing
10 changed files
with
320 additions
and
8 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
lib/api/test/unit/validateBody.test.js → lib/api/test/unit/units/validateBody.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
48 changes: 48 additions & 0 deletions
48
lib/api/test/unit/units/validateBody/getBodySchemaType.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,48 @@ | ||
const { assert } = require('chai'); | ||
const mediaTyper = require('media-typer'); | ||
const { getBodySchemaType } = require('../../../../units/validateBody'); | ||
|
||
describe('getBodySchemaType', () => { | ||
describe('when given non-string schema', () => { | ||
// ... | ||
}); | ||
|
||
describe('when given json schema', () => { | ||
describe('that contains object', () => { | ||
const res = getBodySchemaType('{ "foo": "bar" }'); | ||
|
||
it('returns no errors', () => { | ||
assert.isNull(res[0]); | ||
}); | ||
|
||
it('returns "application/schema+json" media type', () => { | ||
assert.deepEqual(res[1], mediaTyper.parse('application/schema+json')); | ||
}); | ||
}); | ||
|
||
describe('that contains array', () => { | ||
const res = getBodySchemaType('[1, 2, 3]'); | ||
|
||
it('returns no errors', () => { | ||
assert.isNull(res[0]); | ||
}); | ||
|
||
it('returns "application/schema+json" media type', () => { | ||
assert.deepEqual(res[1], mediaTyper.parse('application/schema+json')); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when given non-json schema', () => { | ||
const res = getBodySchemaType('foo'); | ||
|
||
it('returns parsing error', () => { | ||
assert.match( | ||
res[0], | ||
/^Can't validate. Expected body JSON Schema is not a parseable JSON:/ | ||
); | ||
}); | ||
|
||
it('returns no media type', () => [assert.isNull(res[1])]); | ||
}); | ||
}); |
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,78 @@ | ||
const { assert } = require('chai'); | ||
const mediaTyper = require('media-typer'); | ||
const { getBodyType } = require('../../../../units/validateBody'); | ||
|
||
const jsonTypes = ['application/json', 'application/schema+json']; | ||
const nonJsonTypes = ['text/plain']; | ||
|
||
describe('getBodyType', () => { | ||
describe('when given json-like content type', () => { | ||
jsonTypes.forEach((jsonType) => { | ||
describe(`when given "${jsonType}" content type`, () => { | ||
describe('and parsable json body', () => { | ||
const res = getBodyType('{ "foo": "bar" }', jsonType); | ||
|
||
it('returns no errors', () => { | ||
assert.isNull(res[0]); | ||
}); | ||
|
||
it(`returns "${jsonType}" media type`, () => { | ||
assert.deepEqual(res[1], mediaTyper.parse(jsonType)); | ||
}); | ||
}); | ||
|
||
describe('and non-parsable json body', () => { | ||
const res = getBodyType('abc', jsonType); | ||
|
||
it('returns parsing error', () => { | ||
assert.match( | ||
res[0], | ||
new RegExp( | ||
/* eslint-disable no-useless-escape */ | ||
`^Real body 'Content-Type' header is \'${jsonType.replace( | ||
/(\/|\+)/g, | ||
'\\$1' | ||
)}\' but body is not a parseable JSON:` | ||
/* eslint-enable no-useless-escape */ | ||
) | ||
); | ||
}); | ||
|
||
it('uses "text/plain" as fallback media type', () => { | ||
assert.deepEqual(res[1], mediaTyper.parse('text/plain')); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when given non-json content type', () => { | ||
nonJsonTypes.forEach((nonJsonType) => { | ||
describe(`when given "${nonJsonType}" content type`, () => { | ||
describe('and parsable json body', () => { | ||
const res = getBodyType('{ "foo": "bar" }', nonJsonType); | ||
|
||
it('returns no errors', () => { | ||
assert.isNull(res[0]); | ||
}); | ||
|
||
it('coerces to "application/json" media type', () => { | ||
assert.deepEqual(res[1], mediaTyper.parse('application/json')); | ||
}); | ||
}); | ||
|
||
describe('and a non-json body', () => { | ||
const res = getBodyType('abc', nonJsonType); | ||
|
||
it('returns no errors', () => { | ||
assert.isNull(res[0]); | ||
}); | ||
|
||
it(`returns "${nonJsonType}" media type`, () => { | ||
assert.deepEqual(res[1], mediaTyper.parse(nonJsonType)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
81 changes: 81 additions & 0 deletions
81
lib/api/test/unit/units/validateBody/getBodyValidator.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,81 @@ | ||
const { assert } = require('chai'); | ||
const mediaTyper = require('media-typer'); | ||
const { getBodyValidator } = require('../../../../units/validateBody'); | ||
|
||
const getMediaTypes = (real, expected) => { | ||
return [real, expected].map(mediaTyper.parse); | ||
}; | ||
|
||
describe('getBodyValidator', () => { | ||
const knownContentTypes = [ | ||
/* Known content types */ | ||
{ | ||
contentTypes: ['application/json', 'application/json'], | ||
expectedValidator: 'JsonExample' | ||
}, | ||
{ | ||
contentTypes: ['application/json', 'application/schema+json'], | ||
expectedValidator: 'JsonSchema' | ||
}, | ||
{ | ||
contentTypes: ['text/plain', 'text/plain'], | ||
expectedValidator: 'TextDiff' | ||
} | ||
]; | ||
|
||
const unknownContentTypes = [ | ||
{ | ||
contentTypes: ['application/xml', 'application/xml'] | ||
}, | ||
{ | ||
contentTypes: ['text/html', 'application/xml'] | ||
} | ||
]; | ||
|
||
describe('when given known media type', () => { | ||
knownContentTypes.forEach(({ contentTypes, expectedValidator }) => { | ||
const [realContentType, expectedContentType] = contentTypes; | ||
const [real, expected] = getMediaTypes( | ||
realContentType, | ||
expectedContentType | ||
); | ||
|
||
describe(`${realContentType} + ${expectedContentType}`, () => { | ||
const [error, validator] = getBodyValidator(real, expected); | ||
|
||
it('returns no error', () => { | ||
assert.isNull(error); | ||
}); | ||
|
||
it(`returns "${expectedValidator}" validator`, () => { | ||
assert.equal(validator.name, expectedValidator); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when given unknown media type', () => { | ||
unknownContentTypes.forEach(({ contentTypes }) => { | ||
const [realContentType, expectedContentType] = contentTypes; | ||
const [real, expected] = getMediaTypes( | ||
realContentType, | ||
expectedContentType | ||
); | ||
|
||
describe(`${realContentType} + ${expectedContentType}`, () => { | ||
const [error, validator] = getBodyValidator(real, expected); | ||
|
||
it('has explanatory error', () => { | ||
assert.equal( | ||
error, | ||
`Can't validate real media type '${realContentType}' against expected media type '${expectedContentType}'.` | ||
); | ||
}); | ||
|
||
it('returns null validator', () => { | ||
assert.isNull(validator); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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,29 @@ | ||
const { assert } = require('chai'); | ||
const mediaTyper = require('media-typer'); | ||
const { isJson } = require('../../../../units/validateBody'); | ||
|
||
describe('isJson', () => { | ||
describe('returns true', () => { | ||
it('when given valid json media type', () => { | ||
assert.isTrue(isJson(mediaTyper.parse('application/json'))); | ||
}); | ||
|
||
it('when given json-compatible media type', () => { | ||
assert.isTrue(isJson(mediaTyper.parse('application/schema+json'))); | ||
}); | ||
}); | ||
|
||
describe('returns false', () => { | ||
it('when given non-json media type', () => { | ||
assert.isFalse(isJson(mediaTyper.parse('text/plain'))); | ||
}); | ||
|
||
it('when given rubbish', () => { | ||
assert.isFalse(isJson('abc')); | ||
}); | ||
|
||
it('when given null', () => { | ||
assert.isFalse(isJson(null)); | ||
}); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
lib/api/test/unit/units/validateBody/isJsonContentType.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,28 @@ | ||
const { assert } = require('chai'); | ||
const { isJsonContentType } = require('../../../../units/validateBody'); | ||
|
||
describe('isJsonContentType', () => { | ||
describe('returns true', () => { | ||
const jsonTypes = ['application/json', 'application/schema+json']; | ||
|
||
jsonTypes.forEach((contentType) => { | ||
it(`when given ${contentType}`, () => { | ||
assert.isTrue(isJsonContentType(contentType)); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('returns false', () => { | ||
const nonJsonTypes = ['application/xml', 'text/plain']; | ||
|
||
nonJsonTypes.forEach((contentType) => { | ||
it(`when given ${contentType}`, () => { | ||
assert.isFalse(isJsonContentType(contentType)); | ||
}); | ||
}); | ||
|
||
it('when given rubbish', () => { | ||
assert.isFalse(isJsonContentType('foo')); | ||
}); | ||
}); | ||
}); |
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 { assert } = require('chai'); | ||
const mediaTyper = require('media-typer'); | ||
const { isJsonSchema } = require('../../../../units/validateBody'); | ||
|
||
describe('isJsonSchema', () => { | ||
describe('returns true', () => { | ||
it('when given a json schema media type', () => { | ||
assert.isTrue(isJsonSchema(mediaTyper.parse('application/schema+json'))); | ||
}); | ||
}); | ||
|
||
describe('returns false', () => { | ||
it('when given json media type', () => { | ||
assert.isFalse(isJsonSchema(mediaTyper.parse('application/json'))); | ||
}); | ||
|
||
it('when given rubbish', () => { | ||
assert.isFalse(isJsonSchema('abc')); | ||
}); | ||
|
||
it('when given null', () => { | ||
assert.isFalse(isJsonSchema(null)); | ||
}); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
lib/api/test/unit/validateHeaders.test.js → ...i/test/unit/units/validateHeaders.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
2 changes: 1 addition & 1 deletion
2
lib/api/test/unit/validateStatusCode.test.js → ...est/unit/units/validateStatusCode.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
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
Hmm. I'm thinking - if we already have a content-type mismatch, is it worth diffing the body? At this point Gavel is 100% sure the two things it got to compare are not matching. It can fail fast.