Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
jest.spyOn(helpers, 'negotiateByPartialOptionsAndHttpContent').mockReturnValue(E.right(fakeOperationConfig));
const actualOperationConfig = helpers.negotiateDefaultMediaType(partialOptions, response);
expect(helpers.negotiateByPartialOptionsAndHttpContent).toHaveBeenCalledTimes(1);
expect(helpers.negotiateByPartialOptionsAndHttpContent).toHaveBeenCalledWith(
{
code,
dynamic: partialOptions.dynamic,
exampleKey: partialOptions.exampleKey,
},
contents[1] // Check that the */* has been requested
);
assertRight(actualOperationConfig, operationConfig => {
expect(operationConfig).toEqual(fakeOperationConfig);
});
});
});
): E.Either> {
const { mediaType } = httpContent;
if (exampleKey) {
// the user provided exampleKey - highest priority
const example = findExampleByKey(httpContent, exampleKey);
if (example) {
// example exists, return
return E.right({
code,
mediaType,
bodyExample: example,
});
} else {
return E.left(
ProblemJsonError.fromTemplate(
NOT_FOUND,
`Response for contentType: ${mediaType} and exampleKey: ${exampleKey} does not exist.`
)
);
}
} else if (dynamic === true) {
if (httpContent.schema) {
return E.right({
code,
mediaType,
schema: httpContent.schema,
});
} else {
return E.left(new Error(`Tried to force a dynamic response for: ${mediaType} but schema is not defined.`));
}
} else {
it('prefers the first example', () =>
assertRight(eitherResponseWithMultipleExamples, responseWithMultipleExamples =>
expect(responseWithMultipleExamples.body).toHaveProperty('middlename', 'WW')
));
});
it('passes through', () => {
assertRight(
serializeBody(undefined),
result => expect(result).toBeUndefined(),
);
})
});
it('validates positively against schema', () => {
assertRight(
httpQueryValidator.validate({ param: 'abc' }, [
{
name: 'param',
style: HttpParamStyles.Form,
schema: { type: 'string' },
},
])
);
expect(validateAgainstSchemaModule.validateAgainstSchema).toReturnWith(O.none);
});
});
test('return lowest 2xx response and the first example matching the media type', () => {
const response = mock({
config: { dynamic: false },
resource: httpOperations[1],
input: Object.assign({}, httpRequests[0], {
data: Object.assign({}, httpRequests[0].data, {
headers: { accept: 'application/xml' },
}),
}),
})(logger);
assertRight(response, result => {
expect(result.statusCode).toBe(200);
expect(result.headers).toHaveProperty('x-todos-publish');
});
});
it('should use such key', () => {
assertRight(eitherResponse, response => expect(response.body).toHaveProperty('surname', 'Kent'));
});
});
it('does not return warnings', () => {
assertRight(
validateInput({
resource,
element: {
method: 'get',
url: {
path: '/test',
query: {},
},
},
})
);
});
});
it('prefers the default', () =>
assertRight(eitherResponseWithDefault, responseWithDefault =>
expect(responseWithDefault.body).toHaveProperty('middlename', 'JJ')
));
});
it('and cannot find example and dynamic does not exist throw error', () => {
const partialOptions = {
dynamic: false,
code: '200',
};
const httpContent: IMediaTypeContent = {
mediaType: chance.string(),
examples: [],
encodings: [],
};
const proposedResponse = helpers.negotiateByPartialOptionsAndHttpContent(partialOptions, httpContent);
assertRight(proposedResponse, response => {
expect(response).toHaveProperty('code');
expect(response).toHaveProperty('mediaType');
});
});
});