UNPKG

@pact-foundation/pact

Version:
384 lines 18.2 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InteractionWithPluginResponse = exports.ResponseWithPluginBuilder = exports.RequestWithPluginBuilder = exports.InteractionWithPluginRequest = exports.InteractionWithPlugin = exports.InteractionWithResponse = exports.ResponseBuilder = exports.RequestBuilder = exports.InteractionwithRequest = exports.InteractionWithCompleteRequest = exports.UnconfiguredInteraction = void 0; var ramda_1 = require("ramda"); var matchers_1 = require("../../v3/matchers"); var fs = require("fs"); var display_1 = require("../../v3/display"); var logger_1 = __importDefault(require("../../common/logger")); var UnconfiguredInteraction = /** @class */ (function () { // tslint:disable:no-empty-function function UnconfiguredInteraction(pact, interaction, opts, cleanupFn) { this.pact = pact; this.interaction = interaction; this.opts = opts; this.cleanupFn = cleanupFn; } UnconfiguredInteraction.prototype.uponReceiving = function (description) { this.interaction.uponReceiving(description); return this; }; UnconfiguredInteraction.prototype.given = function (state, parameters) { if (parameters) { this.interaction.givenWithParams(state, JSON.stringify(parameters)); } else { this.interaction.given(state); } return this; }; UnconfiguredInteraction.prototype.withCompleteRequest = function (request) { return new InteractionWithCompleteRequest(this.pact, this.interaction, this.opts, this.cleanupFn); }; UnconfiguredInteraction.prototype.withRequest = function (method, path, builder) { this.interaction.withRequest(method, (0, matchers_1.matcherValueOrString)(path)); if (builder) { builder(new RequestBuilder(this.interaction)); } return new InteractionwithRequest(this.pact, this.interaction, this.opts, this.cleanupFn); }; UnconfiguredInteraction.prototype.usingPlugin = function (config) { this.pact.addPlugin(config.plugin, config.version); return new InteractionWithPlugin(this.pact, this.interaction, this.opts, this.cleanupFn); }; return UnconfiguredInteraction; }()); exports.UnconfiguredInteraction = UnconfiguredInteraction; var InteractionWithCompleteRequest = /** @class */ (function () { function InteractionWithCompleteRequest(pact, interaction, opts, cleanupFn) { this.pact = pact; this.interaction = interaction; this.opts = opts; this.cleanupFn = cleanupFn; throw Error('V4InteractionWithCompleteRequest is unimplemented'); } InteractionWithCompleteRequest.prototype.withCompleteResponse = function (response) { return new InteractionWithResponse(this.pact, this.opts, this.cleanupFn); }; return InteractionWithCompleteRequest; }()); exports.InteractionWithCompleteRequest = InteractionWithCompleteRequest; var InteractionwithRequest = /** @class */ (function () { // tslint:disable:no-empty-function function InteractionwithRequest(pact, interaction, opts, cleanupFn) { this.pact = pact; this.interaction = interaction; this.opts = opts; this.cleanupFn = cleanupFn; } InteractionwithRequest.prototype.willRespondWith = function (status, builder) { this.interaction.withStatus(status); if (typeof builder === 'function') { builder(new ResponseBuilder(this.interaction)); } return new InteractionWithResponse(this.pact, this.opts, this.cleanupFn); }; return InteractionwithRequest; }()); exports.InteractionwithRequest = InteractionwithRequest; var RequestBuilder = /** @class */ (function () { // tslint:disable:no-empty-function function RequestBuilder(interaction) { this.interaction = interaction; } RequestBuilder.prototype.query = function (query) { var _this = this; (0, ramda_1.forEachObjIndexed)(function (v, k) { if (Array.isArray(v)) { v.forEach(function (vv, i) { _this.interaction.withQuery(k, i, (0, matchers_1.matcherValueOrString)(vv)); }); } else { _this.interaction.withQuery(k, 0, (0, matchers_1.matcherValueOrString)(v)); } }, query); return this; }; RequestBuilder.prototype.headers = function (headers) { var _this = this; (0, ramda_1.forEachObjIndexed)(function (v, k) { if (Array.isArray(v)) { v.forEach(function (header, index) { _this.interaction.withRequestHeader("".concat(k), index, (0, matchers_1.matcherValueOrString)(header)); }); } else { _this.interaction.withRequestHeader("".concat(k), 0, (0, matchers_1.matcherValueOrString)(v)); } }, headers); return this; }; RequestBuilder.prototype.jsonBody = function (body) { this.interaction.withRequestBody((0, matchers_1.matcherValueOrString)(body), 'application/json'); return this; }; RequestBuilder.prototype.binaryFile = function (contentType, file) { var body = readBinaryData(file); this.interaction.withRequestBinaryBody(body, contentType); return this; }; RequestBuilder.prototype.multipartBody = function (contentType, file, mimePartName) { this.interaction.withRequestMultipartBody(contentType, file, mimePartName); return this; }; RequestBuilder.prototype.body = function (contentType, body) { this.interaction.withRequestBinaryBody(body, contentType); return this; }; return RequestBuilder; }()); exports.RequestBuilder = RequestBuilder; var ResponseBuilder = /** @class */ (function () { function ResponseBuilder(interaction) { this.interaction = interaction; } ResponseBuilder.prototype.headers = function (headers) { var _this = this; (0, ramda_1.forEachObjIndexed)(function (v, k) { _this.interaction.withResponseHeader("".concat(k), 0, (0, matchers_1.matcherValueOrString)(v)); }, headers); return this; }; ResponseBuilder.prototype.jsonBody = function (body) { this.interaction.withResponseBody((0, matchers_1.matcherValueOrString)(body), 'application/json'); return this; }; ResponseBuilder.prototype.binaryFile = function (contentType, file) { var body = readBinaryData(file); this.interaction.withResponseBinaryBody(body, contentType); return this; }; ResponseBuilder.prototype.multipartBody = function (contentType, file, mimePartName) { this.interaction.withResponseMultipartBody(contentType, file, mimePartName); return this; }; ResponseBuilder.prototype.body = function (contentType, body) { this.interaction.withResponseBinaryBody(body, contentType); return this; }; return ResponseBuilder; }()); exports.ResponseBuilder = ResponseBuilder; var InteractionWithResponse = /** @class */ (function () { // tslint:disable:no-empty-function function InteractionWithResponse(pact, opts, cleanupFn) { this.pact = pact; this.opts = opts; this.cleanupFn = cleanupFn; } InteractionWithResponse.prototype.executeTest = function (testFn) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, executeTest(this.pact, this.opts, testFn, this.cleanupFn)]; }); }); }; return InteractionWithResponse; }()); exports.InteractionWithResponse = InteractionWithResponse; var InteractionWithPlugin = /** @class */ (function () { // tslint:disable:no-empty-function function InteractionWithPlugin(pact, interaction, opts, cleanupFn) { this.pact = pact; this.interaction = interaction; this.opts = opts; this.cleanupFn = cleanupFn; } // Multiple plugins are allowed InteractionWithPlugin.prototype.usingPlugin = function (config) { this.pact.addPlugin(config.plugin, config.version); return this; }; InteractionWithPlugin.prototype.withRequest = function (method, path, builder) { this.interaction.withRequest(method, (0, matchers_1.matcherValueOrString)(path)); if (typeof builder === 'function') { builder(new RequestWithPluginBuilder(this.interaction)); } return new InteractionWithPluginRequest(this.pact, this.interaction, this.opts, this.cleanupFn); }; return InteractionWithPlugin; }()); exports.InteractionWithPlugin = InteractionWithPlugin; var InteractionWithPluginRequest = /** @class */ (function () { // tslint:disable:no-empty-function function InteractionWithPluginRequest(pact, interaction, opts, cleanupFn) { this.pact = pact; this.interaction = interaction; this.opts = opts; this.cleanupFn = cleanupFn; } InteractionWithPluginRequest.prototype.willRespondWith = function (status, builder) { this.interaction.withStatus(status); if (typeof builder === 'function') { builder(new ResponseWithPluginBuilder(this.interaction)); } return new InteractionWithPluginResponse(this.pact, this.opts, this.cleanupFn); }; return InteractionWithPluginRequest; }()); exports.InteractionWithPluginRequest = InteractionWithPluginRequest; var RequestWithPluginBuilder = /** @class */ (function (_super) { __extends(RequestWithPluginBuilder, _super); function RequestWithPluginBuilder() { return _super !== null && _super.apply(this, arguments) || this; } RequestWithPluginBuilder.prototype.pluginContents = function (contentType, contents) { this.interaction.withPluginRequestInteractionContents(contentType, contents); return this; }; return RequestWithPluginBuilder; }(RequestBuilder)); exports.RequestWithPluginBuilder = RequestWithPluginBuilder; var ResponseWithPluginBuilder = /** @class */ (function (_super) { __extends(ResponseWithPluginBuilder, _super); function ResponseWithPluginBuilder() { return _super !== null && _super.apply(this, arguments) || this; } ResponseWithPluginBuilder.prototype.pluginContents = function (contentType, contents) { this.interaction.withPluginResponseInteractionContents(contentType, contents); return this; }; return ResponseWithPluginBuilder; }(ResponseBuilder)); exports.ResponseWithPluginBuilder = ResponseWithPluginBuilder; var InteractionWithPluginResponse = /** @class */ (function () { // tslint:disable:no-empty-function function InteractionWithPluginResponse(pact, opts, cleanupFn) { this.pact = pact; this.opts = opts; this.cleanupFn = cleanupFn; } InteractionWithPluginResponse.prototype.executeTest = function (testFn) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, executeTest(this.pact, this.opts, testFn, this.cleanupFn)]; }); }); }; return InteractionWithPluginResponse; }()); exports.InteractionWithPluginResponse = InteractionWithPluginResponse; var readBinaryData = function (file) { try { var body = fs.readFileSync(file); return body; } catch (e) { throw new Error("unable to read file for binary payload : ".concat(e.message)); } }; var cleanup = function (success, pact, opts, server, cleanupFn) { if (success) { pact.writePactFile(opts.dir || './pacts'); } pact.cleanupMockServer(server.port); pact.cleanupPlugins(); cleanupFn(); }; var executeTest = function (pact, opts, testFn, cleanupFn) { return __awaiter(void 0, void 0, void 0, function () { var scheme, host, port, server, val, error, e_1, matchingResults, errors, success, errorMessage; return __generator(this, function (_a) { switch (_a.label) { case 0: scheme = opts.tls ? 'https' : 'http'; host = opts.host || '127.0.0.1'; port = pact.createMockServer(host, opts.port || 0, false); server = { port: port, url: "".concat(scheme, "://").concat(host, ":").concat(port), id: 'unknown' }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, testFn(server)]; case 2: val = _a.sent(); return [3 /*break*/, 4]; case 3: e_1 = _a.sent(); error = e_1; return [3 /*break*/, 4]; case 4: matchingResults = pact.mockServerMismatches(port); errors = (0, display_1.filterMissingFeatureFlag)(matchingResults); success = pact.mockServerMatchedSuccessfully(port); // Scenario: Pact validation failed if (!success && errors.length > 0) { errorMessage = 'Test failed for the following reasons:'; errorMessage += "\n\n ".concat((0, display_1.generateMockServerError)(matchingResults, '\t')); cleanup(false, pact, opts, server, cleanupFn); // If the tests throws an error, we need to rethrow the error, but print out // any additional mock server errors to help the user understand what happened // (The proximate cause here is often the HTTP 500 from the mock server, // where the HTTP client then throws) if (error) { logger_1.default.error(errorMessage); throw error; } // Test didn't throw, so we need to ensure the test fails return [2 /*return*/, Promise.reject(new Error(errorMessage))]; } // Scenario: test threw an error, but Pact validation was OK (error in client or test) if (error) { cleanup(false, pact, opts, server, cleanupFn); throw error; } // Scenario: Pact validation passed, test didn't throw - return the callback value cleanup(true, pact, opts, server, cleanupFn); return [2 /*return*/, val]; } }); }); }; //# sourceMappingURL=index.js.map