Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('fails with proper message', () => {
const body = { x: {} };
body.x = { y: body };
assertLeft(serializeBody(body));
})
});
test('and that content type does not exist should return a 406 error', () => {
const mockResult = mock({
resource: httpOperations[0],
input: httpRequests[0],
config: {
dynamic: false,
mediaTypes: ['text/funky'],
},
})(logger);
assertLeft(mockResult, e => expect(e).toHaveProperty('status', 406));
});
});
it('returns validation errors', () => {
assertLeft(
httpBodyValidator.validate(
encodeURI('key[a][ab]=str'),
[
{
mediaType: 'application/x-www-form-urlencoded',
encodings: [{ property: 'key', style: HttpParamStyles.DeepObject }],
schema: {
type: 'object',
properties: {
key: {
type: 'object',
properties: {
a: {
type: 'object',
properties: { aa: { type: 'string' } },
required: ['aa'],
it('returns only query validation errors', () => {
assertLeft(
validateInput({
resource: httpOperations[2],
element: BAD_INPUT,
}),
error =>
expect(error).toContainEqual({
code: 'pattern',
message: 'should match pattern "^(yes|no)$"',
path: ['query', 'overwrite'],
severity: DiagnosticSeverity.Error,
})
);
});
});
test('given that status code is not defined should throw an error', () => {
const rejection = mock({
resource: httpOperations[0],
input: httpRequests[0],
config: {
dynamic: false,
code: '205',
},
})(logger);
assertLeft(rejection, e => expect(e).toHaveProperty('message', 'The server cannot find the requested content'));
});
it('and example not exist should throw an error', () => {
const exampleKey = chance.string();
const partialOptions = {
code: '200',
exampleKey,
dynamic: chance.bool(),
};
const httpContent: IMediaTypeContent = {
mediaType: chance.string(),
examples: [],
encodings: [],
};
const negotiationResult = helpers.negotiateByPartialOptionsAndHttpContent(partialOptions, httpContent);
assertLeft(negotiationResult, e => {
expect(e.message).toBe('The server cannot find the requested content');
});
});
});
it('returns deprecation warning', () => {
assertLeft(
httpHeadersValidator.validate({ 'x-test-header': 'abc' }, [
{
name: 'x-test-header',
deprecated: true,
style: HttpParamStyles.Simple,
},
]),
error => expect(error).toContainEqual(expect.objectContaining({ severity: DiagnosticSeverity.Warning }))
);
});
});
it('given no 2xx response should throw exception', () => {
const desiredOptions = { dynamic: false };
jest.spyOn(helpers, 'negotiateOptionsBySpecificResponse');
const negotiationResult = helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions)(logger);
assertLeft(negotiationResult, e => {
expect(e.message).toBe('No 2** response defined, cannot mock');
});
});
});
const validationResult = validator.validateInput({
resource: Object.assign(
{
method: 'get',
path: '/',
id: '1',
request: {},
responses: [{ code: '200' }],
},
resourceExtension
),
element: Object.assign({ method: 'get', url: { path: '/', query: {} } }, inputExtension),
});
length === 0
? assertRight(validationResult)
: assertLeft(validationResult, error => expect(error).toHaveLength(length));
};
const httpResponseSchema: IHttpOperationResponse = {
code: '200',
contents: [
{
mediaType: 'text/plain',
},
],
};
const actualResponse = helpers.negotiateOptionsBySpecificResponse(
{ ...httpOperation, responses: [{ code: '200', contents: [{ mediaType: 'text/plain ' }] }] },
desiredOptions,
httpResponseSchema
)(logger);
assertLeft(actualResponse, e =>
expect(e.message).toBe('The server cannot produce a representation for your accept header')
);
});
});