Skip to content

Commit

Permalink
feat(mocker): remove httpRequest from method signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Miaskowski committed Oct 15, 2018
1 parent 85f1bc0 commit 5163835
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export default class HttpOperationConfigNegotiator implements IOperationConfigNe
let httpOperationConfig: IHttpOperationConfig;

if (input.validations.input.length > 0) {
httpOperationConfig = helpers.negotiateOptionsForInvalidRequest(resource.responses, httpRequest);
httpOperationConfig = helpers.negotiateOptionsForInvalidRequest(resource.responses);
} else {
httpOperationConfig = helpers.negotiateOptionsForValidRequest(resource, desiredConfig, httpRequest);
httpOperationConfig = helpers.negotiateOptionsForValidRequest(resource, desiredConfig);
}

return Promise.resolve({
Expand Down
24 changes: 12 additions & 12 deletions packages/http/src/mocker/negotiator/NegotiatorHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IHttpOperationConfig, IHttpRequest } from "@stoplight/prism-http/types";
import { IHttpOperationConfig } from "@stoplight/prism-http/types";
import { IHttpContent, IHttpResponse, IHttpOperation } from "@stoplight/types/http";

function findBestExample(httpContent: IHttpContent) {
Expand Down Expand Up @@ -93,7 +93,7 @@ const helpers = {
throw new Error('Could not generate response for provided content type or no content type provided. Tried to fallback to application/json, but no definition found.');
},

negotiateOptionsBySpecificResponse(httpOperation: IHttpOperation, desiredOptions: IHttpOperationConfig, httpRequest: IHttpRequest, response: IHttpResponse): IHttpOperationConfig {
negotiateOptionsBySpecificResponse(httpOperation: IHttpOperation, desiredOptions: IHttpOperationConfig, response: IHttpResponse): IHttpOperationConfig {
const { code } = response;
let { mediaType, dynamic, exampleKey } = desiredOptions;

Expand All @@ -120,39 +120,39 @@ const helpers = {
}, response);
},

negotiateOptionsForDefaultCode(httpOperation: IHttpOperation, desiredOptions: IHttpOperationConfig, httpRequest: IHttpRequest): IHttpOperationConfig {
negotiateOptionsForDefaultCode(httpOperation: IHttpOperation, desiredOptions: IHttpOperationConfig): IHttpOperationConfig {
const lowest2xxResponse = findLowest2xx(httpOperation.responses);
if (lowest2xxResponse) {
return helpers.negotiateOptionsBySpecificResponse(httpOperation, desiredOptions, httpRequest, lowest2xxResponse);
return helpers.negotiateOptionsBySpecificResponse(httpOperation, desiredOptions, lowest2xxResponse);
}
throw new Error('No 2** response defined, cannot mock');
},

negotiateOptionsBySpecificCode(httpOperation: IHttpOperation, desiredOptions: IHttpOperationConfig, httpRequest: IHttpRequest, code: string): IHttpOperationConfig {
negotiateOptionsBySpecificCode(httpOperation: IHttpOperation, desiredOptions: IHttpOperationConfig, code: string): IHttpOperationConfig {
// find response by provided status code
const responseByForcedStatusCode = findResponseByStatusCode(httpOperation.responses, code);
if (responseByForcedStatusCode) {
try {
// try to negotiate
return helpers.negotiateOptionsBySpecificResponse(httpOperation, desiredOptions, httpRequest, responseByForcedStatusCode);
return helpers.negotiateOptionsBySpecificResponse(httpOperation, desiredOptions, responseByForcedStatusCode);
} catch (error) {
// if negotiations fail try a default code
return helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions, httpRequest);
return helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions);
}
}
// if no response found under a status code, try to mock a default code
return helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions, httpRequest);
return helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions);
},

negotiateOptionsForValidRequest(httpOperation: IHttpOperation, desiredOptions: IHttpOperationConfig, httpRequest: IHttpRequest): IHttpOperationConfig {
negotiateOptionsForValidRequest(httpOperation: IHttpOperation, desiredOptions: IHttpOperationConfig): IHttpOperationConfig {
let { code } = desiredOptions;
if (code) {
return helpers.negotiateOptionsBySpecificCode(httpOperation, desiredOptions, httpRequest, code);
return helpers.negotiateOptionsBySpecificCode(httpOperation, desiredOptions, code);
}
return helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions, httpRequest);
return helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions);
},

negotiateOptionsForInvalidRequest(httpResponses: IHttpResponse[], httpRequest: IHttpRequest): IHttpOperationConfig {
negotiateOptionsForInvalidRequest(httpResponses: IHttpResponse[]): IHttpOperationConfig {
// currently only try to find a 400 response, but we may want to support other cases in the future
const code = '400';
const response = findResponseByStatusCode(httpResponses, code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('HttpOperationOptionsNegotiator', () => {
}, desiredConfig));

expect(helpers.negotiateOptionsForValidRequest).toHaveBeenCalledTimes(1);
expect(helpers.negotiateOptionsForValidRequest).toHaveBeenCalledWith(httpOperation, desiredConfig, httpRequest);
expect(helpers.negotiateOptionsForValidRequest).toHaveBeenCalledWith(httpOperation, desiredConfig);
expect(result).toEqual({
httpOperationConfig
});
Expand All @@ -67,7 +67,7 @@ describe('HttpOperationOptionsNegotiator', () => {
}, desiredConfig));

expect(helpers.negotiateOptionsForValidRequest).toHaveBeenCalledTimes(1);
expect(helpers.negotiateOptionsForValidRequest).toHaveBeenCalledWith(httpOperation, desiredConfig, httpRequest);
expect(helpers.negotiateOptionsForValidRequest).toHaveBeenCalledWith(httpOperation, desiredConfig);
expect(result).toEqual({
error
});
Expand All @@ -94,7 +94,7 @@ describe('HttpOperationOptionsNegotiator', () => {
}, desiredConfig));

expect(helpers.negotiateOptionsForInvalidRequest).toHaveBeenCalledTimes(1);
expect(helpers.negotiateOptionsForInvalidRequest).toHaveBeenCalledWith(httpOperation.responses, httpRequest);
expect(helpers.negotiateOptionsForInvalidRequest).toHaveBeenCalledWith(httpOperation.responses);
expect(result).toEqual({
httpOperationConfig
});
Expand All @@ -112,7 +112,7 @@ describe('HttpOperationOptionsNegotiator', () => {
}, desiredConfig));

expect(helpers.negotiateOptionsForInvalidRequest).toHaveBeenCalledTimes(1);
expect(helpers.negotiateOptionsForInvalidRequest).toHaveBeenCalledWith(httpOperation.responses, httpRequest);
expect(helpers.negotiateOptionsForInvalidRequest).toHaveBeenCalledWith(httpOperation.responses);
expect(result).toEqual({
error
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Chance } from 'chance';
import helpers from '@stoplight/prism-http/mocker/negotiator/NegotiatorHelpers';
import { IHttpRequest, IHttpOperationConfig } from '@stoplight/prism-http/types';
import { IHttpOperationConfig } from '@stoplight/prism-http/types';
import { IHttpOperation, IHttpResponse, IHttpContent, IExample } from '@stoplight/types';
import { anHttpOperation } from '@stoplight/prism-http/mocker/negotiator/__tests__/utils';

const chance = new Chance();

describe('NegotiatorHelpers', () => {
let httpRequest: IHttpRequest;
let httpOperation: IHttpOperation;

beforeEach(() => {
httpRequest = { method: 'get', path: chance.string(), host: chance.string() };
httpOperation = anHttpOperation().instance();
});

Expand All @@ -37,7 +35,7 @@ describe('NegotiatorHelpers', () => {
}]
}]).instance();

const actualConfig = helpers.negotiateOptionsForInvalidRequest(httpOperation.responses, httpRequest);
const actualConfig = helpers.negotiateOptionsForInvalidRequest(httpOperation.responses);
const expectedConfig = {
code: actualCode,
mediaType: actualMediaType,
Expand Down Expand Up @@ -65,7 +63,7 @@ describe('NegotiatorHelpers', () => {
}]
}]).instance();

const actualConfig = helpers.negotiateOptionsForInvalidRequest(httpOperation.responses, httpRequest);
const actualConfig = helpers.negotiateOptionsForInvalidRequest(httpOperation.responses);
const expectedConfig = {
code: actualCode,
mediaType: actualMediaType,
Expand All @@ -87,7 +85,7 @@ describe('NegotiatorHelpers', () => {
}]).instance();

expect(() => {
helpers.negotiateOptionsForInvalidRequest(httpOperation.responses, httpRequest);
helpers.negotiateOptionsForInvalidRequest(httpOperation.responses);
}).toThrow('Data corrupted');
});
});
Expand All @@ -96,7 +94,7 @@ describe('NegotiatorHelpers', () => {
describe('and no 400 response exists', () => {
test('should return an error', async () => {
expect(() => {
helpers.negotiateOptionsForInvalidRequest(httpOperation.responses, httpRequest);
helpers.negotiateOptionsForInvalidRequest(httpOperation.responses);
}).toThrow('No 400 response defined');
});
});
Expand All @@ -118,7 +116,7 @@ describe('NegotiatorHelpers', () => {
jest.spyOn(helpers, 'negotiateOptionsForDefaultCode').mockImplementation(() => { });
jest.spyOn(helpers, 'negotiateOptionsBySpecificCode').mockImplementation(() => { }).mockReturnValue(expectedResult);

const actualResult = helpers.negotiateOptionsForValidRequest(httpOperation, options, httpRequest);
const actualResult = helpers.negotiateOptionsForValidRequest(httpOperation, options);

expect(helpers.negotiateOptionsForDefaultCode).not.toHaveBeenCalled();
expect(helpers.negotiateOptionsBySpecificCode).toHaveBeenCalledTimes(1);
Expand All @@ -133,7 +131,7 @@ describe('NegotiatorHelpers', () => {
jest.spyOn(helpers, 'negotiateOptionsForDefaultCode').mockImplementation(() => { }).mockReturnValue(expectedResult);
jest.spyOn(helpers, 'negotiateOptionsBySpecificCode').mockImplementation(() => { });

const actualResult = helpers.negotiateOptionsForValidRequest(httpOperation, options, httpRequest);
const actualResult = helpers.negotiateOptionsForValidRequest(httpOperation, options);

expect(helpers.negotiateOptionsBySpecificCode).not.toHaveBeenCalled();
expect(helpers.negotiateOptionsForDefaultCode).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -162,10 +160,10 @@ describe('NegotiatorHelpers', () => {
negotiateOptionsBySpecificResponseMock.mockReturnValue(fakeOperationConfig);
httpOperation = anHttpOperation(httpOperation).withResponses([fakeResponse]).instance();

const actualOperationConfig = helpers.negotiateOptionsBySpecificCode(httpOperation, desiredOptions, httpRequest, code);
const actualOperationConfig = helpers.negotiateOptionsBySpecificCode(httpOperation, desiredOptions, code);

expect(negotiateOptionsBySpecificResponseMock).toHaveBeenCalledTimes(1);
expect(negotiateOptionsBySpecificResponseMock).toHaveBeenCalledWith(httpOperation, desiredOptions, httpRequest, fakeResponse);
expect(negotiateOptionsBySpecificResponseMock).toHaveBeenCalledWith(httpOperation, desiredOptions, fakeResponse);
expect(negotiateOptionsForDefaultCodeMock).not.toHaveBeenCalled();
expect(actualOperationConfig).toBe(fakeOperationConfig);
});
Expand All @@ -184,12 +182,12 @@ describe('NegotiatorHelpers', () => {
negotiateOptionsForDefaultCodeMock.mockReturnValue(fakeOperationConfig);
httpOperation = anHttpOperation(httpOperation).withResponses([fakeResponse]).instance();

const actualOperationConfig = helpers.negotiateOptionsBySpecificCode(httpOperation, desiredOptions, httpRequest, code);
const actualOperationConfig = helpers.negotiateOptionsBySpecificCode(httpOperation, desiredOptions, code);

expect(negotiateOptionsBySpecificResponseMock).toHaveBeenCalledTimes(1);
expect(negotiateOptionsBySpecificResponseMock).toHaveBeenCalledWith(httpOperation, desiredOptions, httpRequest, fakeResponse);
expect(negotiateOptionsBySpecificResponseMock).toHaveBeenCalledWith(httpOperation, desiredOptions, fakeResponse);
expect(negotiateOptionsForDefaultCodeMock).toHaveBeenCalledTimes(1);
expect(negotiateOptionsForDefaultCodeMock).toHaveBeenCalledWith(httpOperation, desiredOptions, httpRequest);
expect(negotiateOptionsForDefaultCodeMock).toHaveBeenCalledWith(httpOperation, desiredOptions);
expect(actualOperationConfig).toBe(fakeOperationConfig);
});

Expand All @@ -202,11 +200,11 @@ describe('NegotiatorHelpers', () => {
negotiateOptionsForDefaultCodeMock.mockReturnValue(fakeOperationConfig);
httpOperation = anHttpOperation(httpOperation).withResponses([]).instance();

const actualOperationConfig = helpers.negotiateOptionsBySpecificCode(httpOperation, desiredOptions, httpRequest, code);
const actualOperationConfig = helpers.negotiateOptionsBySpecificCode(httpOperation, desiredOptions, code);

expect(negotiateOptionsBySpecificResponseMock).not.toHaveBeenCalled();
expect(negotiateOptionsForDefaultCodeMock).toHaveBeenCalledTimes(1);
expect(negotiateOptionsForDefaultCodeMock).toHaveBeenCalledWith(httpOperation, desiredOptions, httpRequest);
expect(negotiateOptionsForDefaultCodeMock).toHaveBeenCalledWith(httpOperation, desiredOptions);
expect(actualOperationConfig).toBe(fakeOperationConfig);
});
});
Expand All @@ -224,7 +222,7 @@ describe('NegotiatorHelpers', () => {
jest.spyOn(helpers, 'negotiateOptionsBySpecificResponse').mockReturnValue(fakeOperationConfig);
httpOperation = anHttpOperation(httpOperation).withResponses([response]).instance();

const actualOperationConfig = helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions, httpRequest);
const actualOperationConfig = helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions);

expect(helpers.negotiateOptionsBySpecificResponse).toHaveBeenCalledTimes(1);
expect(actualOperationConfig).toBe(fakeOperationConfig);
Expand All @@ -250,7 +248,7 @@ describe('NegotiatorHelpers', () => {
}
]).instance();

const actualOperationConfig = helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions, httpRequest);
const actualOperationConfig = helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions);

expect(helpers.negotiateOptionsBySpecificResponse).toHaveBeenCalledTimes(1);
expect(actualOperationConfig).toBe(fakeOperationConfig);
Expand All @@ -261,7 +259,7 @@ describe('NegotiatorHelpers', () => {
jest.spyOn(helpers, 'negotiateOptionsBySpecificResponse');

expect(() => {
helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions, httpRequest);
helpers.negotiateOptionsForDefaultCode(httpOperation, desiredOptions);
}).toThrow('No 2** response defined, cannot mock');
});
});
Expand All @@ -285,7 +283,7 @@ describe('NegotiatorHelpers', () => {
jest.spyOn(helpers, 'negotiateByPartialOptionsAndHttpContent').mockReturnValue(fakeOperationConfig);
jest.spyOn(helpers, 'negotiateDefaultMediaType');

const actualOperationConfig = helpers.negotiateOptionsBySpecificResponse(httpOperation, desiredOptions, httpRequest, httpResponseSchema);
const actualOperationConfig = helpers.negotiateOptionsBySpecificResponse(httpOperation, desiredOptions, httpResponseSchema);

expect(helpers.negotiateByPartialOptionsAndHttpContent).toHaveBeenCalledTimes(1);
expect(helpers.negotiateByPartialOptionsAndHttpContent).toHaveBeenCalledWith({
Expand All @@ -312,7 +310,7 @@ describe('NegotiatorHelpers', () => {
jest.spyOn(helpers, 'negotiateByPartialOptionsAndHttpContent');
jest.spyOn(helpers, 'negotiateDefaultMediaType').mockReturnValue(fakeOperationConfig);

const actualOperationConfig = helpers.negotiateOptionsBySpecificResponse(httpOperation, desiredOptions, httpRequest, httpResponseSchema);
const actualOperationConfig = helpers.negotiateOptionsBySpecificResponse(httpOperation, desiredOptions, httpResponseSchema);

expect(helpers.negotiateByPartialOptionsAndHttpContent).not.toHaveBeenCalled();
expect(helpers.negotiateDefaultMediaType).toHaveBeenCalledTimes(1);
Expand All @@ -339,7 +337,7 @@ describe('NegotiatorHelpers', () => {
jest.spyOn(helpers, 'negotiateByPartialOptionsAndHttpContent');
jest.spyOn(helpers, 'negotiateDefaultMediaType').mockReturnValue(fakeOperationConfig);

const actualOperationConfig = helpers.negotiateOptionsBySpecificResponse(httpOperation, desiredOptions, httpRequest, httpResponseSchema);
const actualOperationConfig = helpers.negotiateOptionsBySpecificResponse(httpOperation, desiredOptions, httpResponseSchema);

expect(helpers.negotiateByPartialOptionsAndHttpContent).not.toHaveBeenCalled();
expect(helpers.negotiateDefaultMediaType).toHaveBeenCalledTimes(1);
Expand Down

0 comments on commit 5163835

Please sign in to comment.