Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('should not match if no server defined', async () => {
const resource = await router.route({
resources: [{
id: chance.guid(),
method: pickOneHttpMethod(),
path: randomPath(),
responses: [],
servers: []
}],
input: {
method: pickOneHttpMethod(),
url: randomUrl()
}
});
expect(resource).toBeNull();
});
test('given a concrete matching server and unmatched methods should not match', async () => {
const url = randomUrl();
const [ resourceMethod, requestMethod ] = pickSetOfHttpMethods(2);
const resource = await router.route({
resources: [{
id: chance.guid(),
method: resourceMethod,
path: randomPath(),
responses: [],
servers: [{
url: url.toString(),
}]
}],
input: {
method: requestMethod,
url
}
});
describe('negotiate()', () => {
const httpOperationConfig = {
code: chance.string(),
mediaType: chance.string(),
exampleKey: chance.string(),
dynamic: chance.bool()
};
const httpRequest: IHttpRequest = {
method: 'get',
path: '',
host: ''
};
const desiredConfig = {};
const httpOperation = anHttpOperation().instance();
function getOpts(resource: any, input: any, config: any) {
return { resource, input, config };
}
describe('given valid input', () => {
it('and negotiations succeed should return config', async () => {
jest.spyOn(helpers, 'negotiateOptionsForValidRequest').mockReturnValue(httpOperationConfig);
const result = await negotiator.negotiate(getOpts(httpOperation, {
data: httpRequest,
validations: {
input: []
}
}, desiredConfig));
test('given a concrete matching server and matched methods and unmatched path should not match', async () => {
const url = randomUrl();
const method = pickOneHttpMethod();
const resource = await router.route({
resources: [{
id: chance.guid(),
method,
path: randomPath(),
responses: [],
servers: [{
url: url.toString(),
}]
}],
input: {
method,
url
}
});
test('given a concrete matching server and unmatched methods should not match', async () => {
const url = randomUrl();
const [ resourceMethod, requestMethod ] = pickSetOfHttpMethods(2);
const resource = await router.route({
resources: [{
id: chance.guid(),
method: resourceMethod,
path: randomPath(),
responses: [],
servers: [{
url: url.toString(),
}]
}],
input: {
method: requestMethod,
url
}
});
test('given a concrete matching server and matched methods and unmatched path should not match', async () => {
const url = randomUrl();
const method = pickOneHttpMethod();
const resource = await router.route({
resources: [{
id: chance.guid(),
method,
path: randomPath(),
responses: [],
servers: [{
url: url.toString(),
}]
}],
input: {
method,
url
}
});
expect(resource).toBeNull();
});
});
async function instantiatePrism(specPath: string) {
const operations = await getHttpOperationsFromResource(specPath);
return createServer(operations, {
components: { logger },
config: {
checkSecurity: true,
validateRequest: true,
validateResponse: true,
mock: { dynamic: false },
},
errors: false,
cors: true,
});
}
async function init() {
const prism = createInstance();
await prism.load({ path: relative(process.cwd(), process.argv[2]) });
const server = fastify();
server.all('*', createRequestHandler(prism));
return server.listen(PORT);
}
} catch (e) {
return done(e);
}
}
if (typeIs(req, ['application/x-www-form-urlencoded'])) {
return done(null, body);
}
const error: Error & { status?: number } = new Error(`Unsupported media type.`);
error.status = 415;
Error.captureStackTrace(error);
return done(error);
});
const prism = createInstance(config, components);
const replyHandler: fastify.RequestHandler = (request, reply) => {
const {
req: { method, url },
body,
headers,
query,
} = request;
const input = {
method: (method ? method.toLowerCase() : 'get') as HttpMethod,
url: {
path: (url || '/').split('?')[0],
query,
baseUrl: query.__server,
},
watcher.on('change', () => {
server.fastify.log.info('Restarting Prism...');
getHttpOperationsFromResource(options.document)
.then(operations => {
if (operations.length === 0) {
server.fastify.log.info(
'No operations found in the current file, continuing with the previously loaded spec.'
);
} else {
return server.fastify
.close()
.then(() => {
server.fastify.log.info('Loading the updated operations...');
return createPrism(options);
})
.then(newServer => {
if (newServer) {
server = newServer;