How to use @stoplight/prism-http - 10 common examples

To help you get started, we’ve selected a few @stoplight/prism-http examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stoplightio / prism / packages / http / src / router / __tests__ / index.spec.ts View on Github external
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();
      });
github stoplightio / prism / packages / http / src / router / __tests__ / index.spec.ts View on Github external
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
          }
        });
github stoplightio / prism / packages / http / src / mocker / negotiator / __tests__ / HttpOperationOptionsNegotiator.spec.ts View on Github external
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));
github stoplightio / prism / packages / http / src / router / __tests__ / index.spec.ts View on Github external
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
          }
        });
github stoplightio / prism / packages / http / src / router / __tests__ / index.spec.ts View on Github external
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
          }
        });
github stoplightio / prism / packages / http / src / router / __tests__ / index.spec.ts View on Github external
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();
      });
    });
github stoplightio / prism / packages / http-server / src / __tests__ / server.oas.spec.ts View on Github external
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,
  });
}
github stoplightio / prism / examples / library-example / index.ts View on Github external
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);
}
github stoplightio / prism / packages / http-server / src / server.ts View on Github external
} 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,
      },
github stoplightio / prism / packages / cli / src / util / runner.ts View on Github external
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;