Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import * as Ajv from 'ajv';
import { createLogger } from '@stoplight/prism-core';
import { httpOperations, httpRequests } from '../../__tests__/fixtures';
import { assertLeft, assertRight } from '@stoplight/prism-core/src/__tests__/utils';
import mock from '../index';
const logger = createLogger('TEST', { enabled: false });
describe('http mocker', () => {
describe('request is valid', () => {
describe('given only enforced content type', () => {
test('and that content type exists should first 200 static example', () => {
const response = mock({
resource: httpOperations[0],
input: httpRequests[0],
config: {
dynamic: false,
mediaTypes: ['text/plain'],
},
})(logger);
assertRight(response, result => expect(result).toMatchSnapshot());
});
import { createLogger, IPrism } from '@stoplight/prism-core';
import { IHttpOperation } from '@stoplight/types';
import { Scope as NockScope } from 'nock';
import * as nock from 'nock';
import { basename, resolve } from 'path';
import { createInstance, IHttpProxyConfig, IHttpRequest, IHttpResponse, ProblemJsonError } from '../';
import { getHttpOperationsFromResource } from '../getHttpOperations';
import { UNPROCESSABLE_ENTITY } from '../mocker/errors';
import { NO_PATH_MATCHED_ERROR, NO_SERVER_MATCHED_ERROR } from '../router/errors';
import { assertResolvesRight, assertResolvesLeft } from '@stoplight/prism-core/src/__tests__/utils';
const logger = createLogger('TEST', { enabled: false });
const fixturePath = (filename: string) => resolve(__dirname, 'fixtures', filename);
const noRefsPetstoreMinimalOas2Path = fixturePath('no-refs-petstore-minimal.oas2.json');
const petStoreOas2Path = fixturePath('petstore.oas2.yaml');
const staticExamplesOas2Path = fixturePath('static-examples.oas2.json');
const serverValidationOas2Path = fixturePath('server-validation.oas2.json');
const serverValidationOas3Path = fixturePath('server-validation.oas3.json');
const { version: prismVersion } = require('../../package.json');
type Prism = IPrism;
type NockResWithInterceptors = NockScope & { interceptors: Array<{ req: { headers: string[] } }> };
async function checkUserAgent(
config: IHttpProxyConfig,
prism: Prism,
import {
IHttpOperation,
IHttpOperationResponse,
IMediaTypeContent,
INodeExample,
INodeExternalExample,
} from '@stoplight/types';
import { Chance } from 'chance';
import * as E from 'fp-ts/lib/Either';
import { left, right } from 'fp-ts/lib/ReaderEither';
import { assertRight, assertLeft } from '@stoplight/prism-core/src/__tests__/utils';
import helpers from '../NegotiatorHelpers';
import { IHttpNegotiationResult, NegotiationOptions } from '../types';
const chance = new Chance();
const logger = createLogger('TEST', { enabled: false });
const assertPayloadlessResponse = (actualResponse: E.Either) => {
assertRight(actualResponse, response => {
expect(response).not.toHaveProperty('bodyExample');
expect(response).not.toHaveProperty('mediaType');
expect(response).not.toHaveProperty('schema');
expect(response).toHaveProperty('code');
expect(response).toHaveProperty('headers');
});
};
function anHttpOperation(givenHttpOperation?: IHttpOperation) {
const httpOperation = givenHttpOperation || {
method: chance.string(),
path: chance.url(),
import { createLogger } from '@stoplight/prism-core';
import { IHttpOperation } from '@stoplight/types';
import * as fastify from 'fastify';
import { createServer } from '../';
import { IPrismHttpServer } from '../types';
const logger = createLogger('TEST', { enabled: false });
function instantiatePrism2(operations: IHttpOperation[]) {
return createServer(operations, {
components: { logger },
cors: true,
config: {
checkSecurity: true,
validateRequest: true,
validateResponse: true,
mock: { dynamic: false },
},
errors: false,
});
}
describe('body params validation', () => {
import { createLogger } from '@stoplight/prism-core';
import { getHttpOperationsFromResource } from '@stoplight/prism-http';
import { resolve } from 'path';
import { createServer } from '../';
import { IPrismHttpServer } from '../types';
const logger = createLogger('TEST', { enabled: false });
function checkErrorPayloadShape(payload: string) {
const parsedPayload = JSON.parse(payload);
expect(parsedPayload).toHaveProperty('type');
expect(parsedPayload).toHaveProperty('title');
expect(parsedPayload).toHaveProperty('status');
expect(parsedPayload).toHaveProperty('detail');
}
async function instantiatePrism(specPath: string) {
const operations = await getHttpOperationsFromResource(specPath);
return createServer(operations, {
components: { logger },
config: {
checkSecurity: true,
import { IHttpOperation, INodeExample, DiagnosticSeverity } from '@stoplight/types';
import { right } from 'fp-ts/lib/ReaderEither';
import * as E from 'fp-ts/lib/Either';
import { flatMap } from 'lodash';
import mock from '../../mocker';
import * as JSONSchemaGenerator from '../../mocker/generator/JSONSchema';
import { IHttpRequest, JSONSchema } from '../../types';
import helpers from '../negotiator/NegotiatorHelpers';
import { assertRight } from '@stoplight/prism-core/src/__tests__/utils';
import { runCallback } from '../callback/callbacks';
jest.mock('../callback/callbacks', () => ({
runCallback: jest.fn(() => () => () => undefined),
}));
const logger = createLogger('TEST', { enabled: false });
describe('mocker', () => {
afterEach(() => jest.restoreAllMocks());
describe('mock()', () => {
const mockSchema: JSONSchema = {
type: 'object',
properties: {
name: { type: 'string' },
surname: { type: 'string', format: 'email' },
},
required: ['name', 'email'],
};
const mockResource: IHttpOperation = {
id: 'id',
async function createMultiProcessPrism(options: CreateBaseServerOptions) {
if (cluster.isMaster) {
cluster.setupMaster({ silent: true });
signale.await({ prefix: chalk.bgWhiteBright.black('[CLI]'), message: 'Starting Prism…' });
const worker = cluster.fork();
if (worker.process.stdout) {
pipeOutputToSignale(worker.process.stdout);
}
return;
} else {
const logInstance = createLogger('CLI', cliSpecificLoggerOptions);
try {
return await createPrismServerWithLogger(options, logInstance);
} catch (e) {
logInstance.fatal(e.message);
cluster.worker.kill();
}
}
}
async function createSingleProcessPrism(options: CreateBaseServerOptions) {
signale.await({ prefix: chalk.bgWhiteBright.black('[CLI]'), message: 'Starting Prism…' });
const logStream = new PassThrough();
const logInstance = createLogger('CLI', cliSpecificLoggerOptions, logStream);
pipeOutputToSignale(logStream);
try {
return await createPrismServerWithLogger(options, logInstance);
} catch (e) {
logInstance.fatal(e.message);
}
}