diff --git a/lib/api/test/integration/validateMessage.test.js b/lib/api/test/integration/validateMessage.test.js index d71bef8b..bb01a895 100644 --- a/lib/api/test/integration/validateMessage.test.js +++ b/lib/api/test/integration/validateMessage.test.js @@ -90,15 +90,16 @@ describe('validateMessage', () => { }); describe('method', () => { + // See https://github.com/apiaryio/gavel.js/issues/158 it.skip('compares methods'); }); - describe.skip('headers', () => { - it('has no validator set', () => { - assert.propertyVal(result.headers, 'validator', null); + describe('headers', () => { + it('has "HeadersJsonExample" validator', () => { + assert.propertyVal(result.headers, 'validator', 'HeadersJsonExample'); }); - it('has "application/vnd.apiary.http-headers+json" as real headers type', () => { + it('has "application/vnd.apiary.http-headers+json" as real type', () => { assert.propertyVal( result.headers, 'realType', @@ -106,31 +107,37 @@ describe('validateMessage', () => { ); }); - it('has no expected headers type', () => { - assert.propertyVal(result.headers, 'expectedType', null); + it('has "application/vnd.apiary.http-headers+json" expcted type', () => { + assert.propertyVal( + result.headers, + 'expectedType', + 'application/vnd.apiary.http-headers+json' + ); }); - describe('produces an error', () => { - it('exactly one error', () => { - assert.lengthOf(result.headers.results, 1); - }); - - it('has "error" severity', () => { - assert.propertyVal(result.headers.results[0], 'severity', 'error'); - }); - - it('has explanatory message', () => { - assert.propertyVal( - result.headers.results[0], - 'message', - `\ -No validator found for real data media type -"application/vnd.apiary.http-headers+json" -and expected data media type -"null".` - ); - }); + it('has no errors', () => { + assert.lengthOf(result.headers.results, 0); }); + + // describe('produces an error', () => { + // it('exactly one error', () => {}); + + // it('has "error" severity', () => { + // assert.propertyVal(result.headers.results[0], 'severity', 'error'); + // }); + + // it('has explanatory message', () => { + // assert.propertyVal( + // result.headers.results[0], + // 'message', + // `\ + // No validator found for real data media type + // "application/vnd.apiary.http-headers+json" + // and expected data media type + // "null".` + // ); + // }); + // }); }); describe('body', () => { diff --git a/lib/api/test/unit/units/validateBody.test.js b/lib/api/test/unit/units/validateBody.test.js index 9b8b7518..6f6346b9 100644 --- a/lib/api/test/unit/units/validateBody.test.js +++ b/lib/api/test/unit/units/validateBody.test.js @@ -12,14 +12,14 @@ describe('validateBody', () => { ]; scenarios.forEach(({ name, value }) => { - it(`errors when given ${name}`, () => { + it(`throws when given ${name}`, () => { assert.throw(() => validateBody({ body: value }, { body: value })); }); }); }); describe('when given supported body type', () => { - describe('with explicit Content-Type header', () => { + describe('with explicit "Content-Type" header', () => { describe('application/json', () => { describe('with matching body type', () => { const res = validateBody( @@ -27,19 +27,21 @@ describe('validateBody', () => { body: '{ "foo": "bar" }', headers: { 'content-type': 'application/json' } }, - { body: '{ "foo": "bar" }' } + { + body: '{ "foo": "bar" }' + } ); - it('has application/json real type', () => { - assert.propertyVal(res, 'realType', 'application/json'); + it('has "JsonExample" validator', () => { + assert.propertyVal(res, 'validator', 'JsonExample'); }); - it('has application/json expected type', () => { - assert.propertyVal(res, 'expectedType', 'application/json'); + it('has "application/json" real type', () => { + assert.propertyVal(res, 'realType', 'application/json'); }); - it('has JsonExample validator', () => { - assert.propertyVal(res, 'validator', 'JsonExample'); + it('has "application/json" expected type', () => { + assert.propertyVal(res, 'expectedType', 'application/json'); }); it('has no errors', () => { @@ -56,16 +58,20 @@ describe('validateBody', () => { { body: '{ "foo": "bar" }' } ); - it('fallbacks to text/plain real type', () => { + it('has no validator', () => { + assert.propertyVal(res, 'validator', null); + }); + + it('fallbacks to "text/plain" real type', () => { assert.propertyVal(res, 'realType', 'text/plain'); }); - it('has application/json expected type', () => { + it('has "application/json" expected type', () => { assert.propertyVal(res, 'expectedType', 'application/json'); }); describe('produces content-type error', () => { - it('has proper severity', () => { + it('has "error" severity', () => { assert.propertyVal(res.results[0], 'severity', 'error'); }); @@ -76,10 +82,6 @@ describe('validateBody', () => { ); }); }); - - it('has no validator', () => { - assert.propertyVal(res, 'validator', null); - }); }); }); @@ -97,16 +99,16 @@ describe('validateBody', () => { } ); - it('has application/hal+json real type', () => { - assert.propertyVal(res, 'realType', 'application/hal+json'); + it('has "JsonExample" validator', () => { + assert.propertyVal(res, 'validator', 'JsonExample'); }); - it('has application/json expected type', () => { - assert.propertyVal(res, 'expectedType', 'application/json'); + it('has "application/hal+json" real type', () => { + assert.propertyVal(res, 'realType', 'application/hal+json'); }); - it('has JsonExample validator', () => { - assert.propertyVal(res, 'validator', 'JsonExample'); + it('has "application/json" expected type', () => { + assert.propertyVal(res, 'expectedType', 'application/json'); }); it('has no errors', () => { @@ -127,11 +129,15 @@ describe('validateBody', () => { } ); - it('fallbacks to text/plain real type', () => { + it('has no validator', () => { + assert.propertyVal(res, 'validator', null); + }); + + it('fallbacks to "text/plain" real type', () => { assert.propertyVal(res, 'realType', 'text/plain'); }); - it('has text/plain expected type', () => { + it('has "text/plain" expected type', () => { assert.propertyVal(res, 'expectedType', 'text/plain'); }); @@ -139,6 +145,7 @@ describe('validateBody', () => { it('has "error" severity', () => { assert.propertyVal(res.results[0], 'severity', 'error'); }); + it('has explanatory message', () => { assert.match( res.results[0].message, @@ -146,31 +153,27 @@ describe('validateBody', () => { ); }); }); - - it('has no validator', () => { - assert.propertyVal(res, 'validator', null); - }); }); }); }); - describe('without explicit Content-Type header', () => { + describe('without explicit "Content-Type" header', () => { describe('text/plain', () => { describe('with matching bodies', () => { const res = validateBody({ body: 'foo' }, { body: 'foo' }); + it('has "TextDiff" validator', () => { + assert.propertyVal(res, 'validator', 'TextDiff'); + }); + it('has text/plain real type', () => { assert.propertyVal(res, 'realType', 'text/plain'); }); - it('has text/plain expected type', () => { + it('has "text/plain" expected type', () => { assert.propertyVal(res, 'expectedType', 'text/plain'); }); - it('has TextDiff validator', () => { - assert.propertyVal(res, 'validator', 'TextDiff'); - }); - it('has no errors', () => { assert.lengthOf(res.results, 0); }); @@ -179,16 +182,16 @@ describe('validateBody', () => { describe('with non-matching bodies', () => { const res = validateBody({ body: 'foo ' }, { body: 'bar' }); - it('has text/plain real type', () => { - assert.propertyVal(res, 'realType', 'text/plain'); + it('has "TextDiff" validator', () => { + assert.propertyVal(res, 'validator', 'TextDiff'); }); - it('has text/plain expected type', () => { - assert.propertyVal(res, 'expectedType', 'text/plain'); + it('has "text/plain" real type', () => { + assert.propertyVal(res, 'realType', 'text/plain'); }); - it('has TextDiff validator', () => { - assert.propertyVal(res, 'validator', 'TextDiff'); + it('has "text/plain" expected type', () => { + assert.propertyVal(res, 'expectedType', 'text/plain'); }); describe('produces validation error', () => { @@ -219,16 +222,16 @@ describe('validateBody', () => { { body: '{ "foo": "bar" }' } ); - it('has application/json real type', () => { - assert.propertyVal(res, 'realType', 'application/json'); + it('has "JsonExample" validator', () => { + assert.propertyVal(res, 'validator', 'JsonExample'); }); - it('has application/json expected type', () => { - assert.propertyVal(res, 'expectedType', 'application/json'); + it('has "application/json" real type', () => { + assert.propertyVal(res, 'realType', 'application/json'); }); - it('has JsonExample validator', () => { - assert.propertyVal(res, 'validator', 'JsonExample'); + it('has "application/json" expected type', () => { + assert.propertyVal(res, 'expectedType', 'application/json'); }); it('has no errors', () => { @@ -242,16 +245,16 @@ describe('validateBody', () => { { body: '{ "bar": null }' } ); - it('has application/json real type', () => { - assert.propertyVal(res, 'realType', 'application/json'); + it('has "JsonExample" validator', () => { + assert.propertyVal(res, 'validator', 'JsonExample'); }); - it('has application/json expected type', () => { - assert.propertyVal(res, 'expectedType', 'application/json'); + it('has "application/json" real type', () => { + assert.propertyVal(res, 'realType', 'application/json'); }); - it('has JsonExample validator', () => { - assert.propertyVal(res, 'validator', 'JsonExample'); + it('has "application/json" expected type', () => { + assert.propertyVal(res, 'expectedType', 'application/json'); }); describe('produces validation errors', () => { diff --git a/lib/api/test/unit/units/validateBody/getBodySchemaType.test.js b/lib/api/test/unit/units/validateBody/getBodySchemaType.test.js index edd400db..adc102e2 100644 --- a/lib/api/test/unit/units/validateBody/getBodySchemaType.test.js +++ b/lib/api/test/unit/units/validateBody/getBodySchemaType.test.js @@ -4,45 +4,68 @@ const { getBodySchemaType } = require('../../../../units/validateBody'); describe('getBodySchemaType', () => { describe('when given non-string schema', () => { - // ... - }); + const values = [ + ['number', 123], + ['object', { foo: 'bar' }], + ['array', [1, 2, 3]], + ['null', null], + ['undefined', undefined] + ]; - describe('when given json schema', () => { - describe('that contains object', () => { - const res = getBodySchemaType('{ "foo": "bar" }'); + values.forEach(([name, value]) => { + describe(`which is ${name}`, () => { + const [error, mediaType] = getBodySchemaType(value); - it('returns no errors', () => { - assert.isNull(res[0]); - }); + it('returns "application/schema+json" media type', () => { + assert.deepEqual( + mediaType, + mediaTyper.parse('application/schema+json') + ); + }); - it('returns "application/schema+json" media type', () => { - assert.deepEqual(res[1], mediaTyper.parse('application/schema+json')); + it('has no errors', () => { + assert.isNull(error); + }); }); }); + }); - describe('that contains array', () => { - const res = getBodySchemaType('[1, 2, 3]'); + describe('when given JSON', () => { + const jsonSchemas = [ + ['object', '{ "foo": "bar" }'], + ['array', '[1, 2, 3]'] + ]; - it('returns no errors', () => { - assert.isNull(res[0]); - }); + jsonSchemas.forEach(([name, jsonSchema]) => { + describe(`that contains ${name}`, () => { + const [error, mediaType] = getBodySchemaType(jsonSchema); - it('returns "application/schema+json" media type', () => { - assert.deepEqual(res[1], mediaTyper.parse('application/schema+json')); + it('returns "application/schema+json" media type', () => { + assert.deepEqual( + mediaType, + mediaTyper.parse('application/schema+json') + ); + }); + + it('returns no errors', () => { + assert.isNull(error); + }); }); }); }); - describe('when given non-json schema', () => { - const res = getBodySchemaType('foo'); + describe('when given non-JSON string', () => { + const [error, mediaType] = getBodySchemaType('foo'); + + it('returns no media type', () => { + assert.isNull(mediaType); + }); it('returns parsing error', () => { assert.match( - res[0], + error, /^Can't validate: expected body JSON Schema is not a parseable JSON:/ ); }); - - it('returns no media type', () => [assert.isNull(res[1])]); }); }); diff --git a/lib/api/test/unit/units/validateBody/getBodyType.test.js b/lib/api/test/unit/units/validateBody/getBodyType.test.js index 4d58786a..d9ba8dd3 100644 --- a/lib/api/test/unit/units/validateBody/getBodyType.test.js +++ b/lib/api/test/unit/units/validateBody/getBodyType.test.js @@ -6,10 +6,10 @@ const jsonTypes = ['application/json', 'application/schema+json']; const nonJsonTypes = ['text/plain']; describe('getBodyType', () => { - describe('when given json-like content type', () => { + describe('when given JSON-like content type', () => { jsonTypes.forEach((jsonType) => { describe(`when given "${jsonType}" content type`, () => { - describe('and parsable json body', () => { + describe('and parsable JSON body', () => { const res = getBodyType('{ "foo": "bar" }', jsonType, 'real'); it('returns no errors', () => { @@ -21,7 +21,7 @@ describe('getBodyType', () => { }); }); - describe('and non-parsable json body', () => { + describe('and non-parsable JSON body', () => { const res = getBodyType('abc', jsonType, 'real'); it('returns parsing error', () => { @@ -46,7 +46,7 @@ describe('getBodyType', () => { }); }); - describe('when given non-json content type', () => { + describe('when given non-JSON content type', () => { nonJsonTypes.forEach((nonJsonType) => { describe(`when given "${nonJsonType}" content type`, () => { describe('and parsable json body', () => { @@ -61,7 +61,7 @@ describe('getBodyType', () => { }); }); - describe('and a non-json body', () => { + describe('and a non-JSON body', () => { const res = getBodyType('abc', nonJsonType, 'real'); it('returns no errors', () => { diff --git a/lib/api/test/unit/units/validateBody/getBodyValidator.test.js b/lib/api/test/unit/units/validateBody/getBodyValidator.test.js index 9e0bfa57..16bf3d2e 100644 --- a/lib/api/test/unit/units/validateBody/getBodyValidator.test.js +++ b/lib/api/test/unit/units/validateBody/getBodyValidator.test.js @@ -43,4 +43,24 @@ describe('getBodyValidator', () => { }); }); }); + + // describe('when given unknown media type', () => { + // const unknownContentTypes = [['text/html', 'text/xml']]; + + // unknownContentTypes.forEach((contentTypes) => { + // const [realContentType, expectedContentType] = contentTypes; + // const [real, expected] = getMediaTypes( + // realContentType, + // expectedContentType + // ); + + // describe(`${realContentType} + ${expectedContentType}`, () => { + // const [error, validator] = getBodyValidator(real, expected); + + // it('...', () => { + // console.log({ error, validator }); + // }); + // }); + // }); + // }); }); diff --git a/lib/api/test/unit/units/validateBody/isJson.test.js b/lib/api/test/unit/units/validateBody/isJson.test.js index 38519801..54ec2008 100644 --- a/lib/api/test/unit/units/validateBody/isJson.test.js +++ b/lib/api/test/unit/units/validateBody/isJson.test.js @@ -4,17 +4,17 @@ const { isJson } = require('../../../../units/validateBody'); describe('isJson', () => { describe('returns true', () => { - it('when given valid json media type', () => { + it('when given valid JSON media type', () => { assert.isTrue(isJson(mediaTyper.parse('application/json'))); }); - it('when given json-compatible media type', () => { + 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', () => { + it('when given non-JSON media type', () => { assert.isFalse(isJson(mediaTyper.parse('text/plain'))); }); diff --git a/lib/api/test/unit/units/validateBody/isJsonSchema.test.js b/lib/api/test/unit/units/validateBody/isJsonSchema.test.js index a2a4bb15..09f8ff86 100644 --- a/lib/api/test/unit/units/validateBody/isJsonSchema.test.js +++ b/lib/api/test/unit/units/validateBody/isJsonSchema.test.js @@ -4,7 +4,7 @@ const { isJsonSchema } = require('../../../../units/validateBody'); describe('isJsonSchema', () => { describe('returns true', () => { - it('when given a json schema media type', () => { + it('when given a JSON schema media type', () => { assert.isTrue(isJsonSchema(mediaTyper.parse('application/schema+json'))); }); }); diff --git a/lib/api/test/unit/units/validateStatusCode.test.js b/lib/api/test/unit/units/validateStatusCode.test.js index e22cfbbe..2c9592ef 100644 --- a/lib/api/test/unit/units/validateStatusCode.test.js +++ b/lib/api/test/unit/units/validateStatusCode.test.js @@ -12,15 +12,15 @@ describe('validateStatusCode', () => { } ); - it('has proper validator', () => { + it('has "TextDiff" validator', () => { assert.propertyVal(res, 'validator', 'TextDiff'); }); - it('has proper expected type', () => { + it('has "text/vnd.apiary.status-code" expected type', () => { assert.propertyVal(res, 'expectedType', 'text/vnd.apiary.status-code'); }); - it('has proper real type', () => { + it('has "text/vnd.apiary.status-code" real type', () => { assert.propertyVal(res, 'realType', 'text/vnd.apiary.status-code'); }); @@ -39,25 +39,34 @@ describe('validateStatusCode', () => { } ); - it('has proper validator', () => { + it('has "TextDiff" validator', () => { assert.propertyVal(res, 'validator', 'TextDiff'); }); - it('has proper expected type', () => { + it('has "text/vnd.apiary.status-code" expected type', () => { assert.propertyVal(res, 'expectedType', 'text/vnd.apiary.status-code'); }); - it('has proper real type', () => { + it('has "text/vnd.apiary.status-code" real type', () => { assert.propertyVal(res, 'realType', 'text/vnd.apiary.status-code'); }); - it('has errors', () => { - assert.deepPropertyVal(res, 'results', [ - { - message: 'Real and expected data does not match.', - severity: 'error' - } - ]); + describe('produces error', () => { + it('exactly one error', () => { + assert.lengthOf(res.results, 1); + }); + + it('has "error" severity', () => { + assert.propertyVal(res.results[0], 'severity', 'error'); + }); + + it('has explanatory message', () => { + assert.propertyVal( + res.results[0], + 'message', + 'Real and expected data does not match.' + ); + }); }); }); });