UNPKG

wiremock-captain

Version:

A better way to use the WireMock simulator to test your HTTP APIs

58 lines (57 loc) 2.37 kB
"use strict"; // Copyright (c) WarnerMedia Direct, LLC. All rights reserved. Licensed under the MIT license. // See the LICENSE file for license information. Object.defineProperty(exports, "__esModule", { value: true }); exports.createWireMockRequest = createWireMockRequest; const externalTypes_1 = require("./types/externalTypes"); function createWireMockRequest(request, features) { const { body, cookies, headers, method, queryParameters, endpoint, formParameters } = request; const endpointFeature = features.requestEndpointFeature ?? externalTypes_1.EndpointFeature.Default; const mock = { method, }; mock[endpointFeature] = endpoint; if (body) { const bodyFeature = features.requestBodyFeature ?? externalTypes_1.MatchingAttributes.EqualToJson; const mockBody = {}; mockBody[bodyFeature] = body; if (bodyFeature === externalTypes_1.MatchingAttributes.EqualToJson) { mockBody['ignoreArrayOrder'] = features.requestIgnoreArrayOrder ?? false; mockBody['ignoreExtraElements'] = features.requestIgnoreExtraElements ?? false; } mock.bodyPatterns = [mockBody]; } if (cookies) { mock.cookies = getMockedObject(cookies, features.requestCookieFeatures); } if (headers) { mock.headers = getMockedObject(headers, features.requestHeaderFeatures); } if (queryParameters) { mock.queryParameters = getMockedObject(queryParameters, features.requestQueryParamFeatures); } if (formParameters) { mock.formParameters = getMockedObject(formParameters, features.requestFormParameterFeatures); } return mock; } /** * Maps cookies, headers, and queryParameters to wiremock schema * @param {Record<string, KeyValue>} dict * @param {Record<string, MatchingAttributes>} dictMatchingAttributes * @returns {{[p: string]: any}} */ function getMockedObject(dict, dictMatchingAttributes) { const mockObject = {}; for (const key of Object.keys(dict)) { mockObject[key] = mapPropertyToAttribute( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion dict[key], dictMatchingAttributes?.[key] ?? externalTypes_1.MatchingAttributes.EqualTo); } return mockObject; } function mapPropertyToAttribute(value, attribute) { return { [attribute]: value, }; }