Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Sep 15, 2019
1 parent a66982f commit 005e757
Showing 1 changed file with 92 additions and 27 deletions.
119 changes: 92 additions & 27 deletions test/response.validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,72 @@ describe(packageJson.name, () => {
expect(r.body.error).to.be.not.null;
}));

it.only('should if additional properties are provided when set false', async () => {
// TODO
it.only('should always return valid for non-JSON responses', async () => {
const v = new ResponseValidator(apiSpec);
const responses = {
'200': {
description: 'pet response',
content: {
'application/json': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
},
},
},
},
default: {
description: 'unexpected error',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error',
},
},
},
},
};
const responses = petsResponseSchema();
const statusCode = 200;

try {
v._validate({
body: { id: 1, name: 'test', tag: 'tag' },
responses,
statusCode,
});
} catch (e) {
expect(e).to.be.not.null;
expect(e.message).to.contain('should be array');
}
});

// TODO
it.only('should validate the error object', async () => {});

it.only('should return an error if field type is invalid', async () => {
const v = new ResponseValidator(apiSpec);
const responses = petsResponseSchema();
const statusCode = 200;

try {
v._validate({
body: [{ id: 'bad-id', name: 'test', tag: 'tag' }],
responses,
statusCode,
});
} catch (e) {
expect(e).to.be.not.null;
expect(e.message).to.contain('should be integer');
expect(e.message).to.not.contain('additional properties');
}

try {
v._validate({
body: { id: 1, name: 'test', tag: 'tag' },
responses,
statusCode,
});
} catch (e) {
expect(e).to.be.not.null;
expect(e.message).to.contain('should be array');
}

try {
v._validate({
body: [{ id: 1, name: [], tag: 'tag' }],
responses,
statusCode,
});
} catch (e) {
expect(e).to.be.not.null;
expect(e.message).to.contain('should be string');
}
});

// TODO may not be possible to fix
// https://github.com/epoberezkin/ajv/issues/837
it.skip('should if additional properties are provided when set false', async () => {
const v = new ResponseValidator(apiSpec);
const responses = petsResponseSchema();
const statusCode = 200;
const body = [
{
Expand All @@ -84,6 +122,7 @@ describe(packageJson.name, () => {
];
try {
v._validate({ body, responses, statusCode });
expect('here').to.be.null;
} catch (e) {
// TODO include params.additionalProperty: value in error message
// TODO maybe params should be in the response
Expand All @@ -96,4 +135,30 @@ describe(packageJson.name, () => {
});
});

function schemaFromResponseDef(d) {}
function petsResponseSchema() {
return {
'200': {
description: 'pet response',
content: {
'application/json': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
},
},
},
},
default: {
description: 'unexpected error',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error',
},
},
},
},
};
}

0 comments on commit 005e757

Please sign in to comment.