Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
test: polishes the test suits
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed May 17, 2019
1 parent 0cfab27 commit e260a4c
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 122 deletions.
59 changes: 33 additions & 26 deletions lib/api/test/integration/validateMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,47 +90,54 @@ 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',
'application/vnd.apiary.http-headers+json'
);
});

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', () => {
Expand Down
107 changes: 55 additions & 52 deletions lib/api/test/unit/units/validateBody.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,36 @@ 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(
{
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', () => {
Expand All @@ -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');
});

Expand All @@ -76,10 +82,6 @@ describe('validateBody', () => {
);
});
});

it('has no validator', () => {
assert.propertyVal(res, 'validator', null);
});
});
});

Expand All @@ -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', () => {
Expand All @@ -127,50 +129,51 @@ 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');
});

describe('produces error', () => {
it('has "error" severity', () => {
assert.propertyVal(res.results[0], 'severity', 'error');
});

it('has explanatory message', () => {
assert.match(
res.results[0].message,
/^Can't validate: real body 'Content-Type' header is 'application\/hal\+json' but body is not a parseable JSON:/
);
});
});

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);
});
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
Loading

0 comments on commit e260a4c

Please sign in to comment.