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

To help you get started, we’ve selected a few @stoplight/prism-core 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 / mocker / negotiator / __tests__ / NegotiatorHelpers.spec.ts View on Github external
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);
        });
      });
    });
github stoplightio / prism / packages / http / src / mocker / negotiator / NegotiatorHelpers.ts View on Github external
): 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 {
github stoplightio / prism / packages / http / src / mocker / __tests__ / HttpMocker.spec.ts View on Github external
it('prefers the first example', () =>
                assertRight(eitherResponseWithMultipleExamples, responseWithMultipleExamples =>
                  expect(responseWithMultipleExamples.body).toHaveProperty('middlename', 'WW')
                ));
            });
github stoplightio / prism / packages / http / src / utils / __tests__ / serializeBody.spec.ts View on Github external
it('passes through', () => {
      assertRight(
        serializeBody(undefined),
        result => expect(result).toBeUndefined(),
      );
    })
  });
github stoplightio / prism / packages / http / src / validator / validators / __tests__ / query.spec.ts View on Github external
it('validates positively against schema', () => {
                assertRight(
                  httpQueryValidator.validate({ param: 'abc' }, [
                    {
                      name: 'param',
                      style: HttpParamStyles.Form,
                      schema: { type: 'string' },
                    },
                  ])
                );

                expect(validateAgainstSchemaModule.validateAgainstSchema).toReturnWith(O.none);
              });
            });
github stoplightio / prism / packages / http / src / mocker / __tests__ / functional.spec.ts View on Github external
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');
          });
        });
github stoplightio / prism / packages / http / src / mocker / __tests__ / HttpMocker.spec.ts View on Github external
it('should use such key', () => {
              assertRight(eitherResponse, response => expect(response.body).toHaveProperty('surname', 'Kent'));
            });
          });
github stoplightio / prism / packages / http / src / validator / __tests__ / functional.spec.ts View on Github external
it('does not return warnings', () => {
        assertRight(
          validateInput({
            resource,
            element: {
              method: 'get',
              url: {
                path: '/test',
                query: {},
              },
            },
          })
        );
      });
    });
github stoplightio / prism / packages / http / src / mocker / __tests__ / HttpMocker.spec.ts View on Github external
it('prefers the default', () =>
                assertRight(eitherResponseWithDefault, responseWithDefault =>
                  expect(responseWithDefault.body).toHaveProperty('middlename', 'JJ')
                ));
            });
github stoplightio / prism / packages / http / src / mocker / negotiator / __tests__ / NegotiatorHelpers.spec.ts View on Github external
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');
      });
    });
  });