UNPKG

@pact-foundation/pact

Version:
87 lines 3.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RequestBuilder = void 0; const ramda_1 = require("ramda"); const matchingRules_1 = require("../../common/matchingRules"); const matchers_1 = require("../../v3/matchers"); const _1 = require("."); class RequestBuilder { interaction; // tslint:disable:no-empty-function constructor(interaction) { this.interaction = interaction; } query(query) { (0, ramda_1.forEachObjIndexed)((v, k) => { if (Array.isArray(v)) { v.forEach((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; } headers(headers) { (0, ramda_1.forEachObjIndexed)((v, k) => { if (Array.isArray(v)) { v.forEach((header, index) => { this.interaction.withRequestHeader(`${k}`, index, (0, matchers_1.matcherValueOrString)(header)); }); } else { this.interaction.withRequestHeader(`${k}`, 0, (0, matchers_1.matcherValueOrString)(v)); } }, headers); return this; } jsonBody(body) { this.interaction.withRequestBody((0, matchers_1.matcherValueOrString)(body), 'application/json'); return this; } xmlBody(body) { this.interaction.withRequestBody((0, matchers_1.matcherValueOrString)(body), 'application/xml'); return this; } binaryFile(contentType, file) { const body = (0, _1.readBinaryData)(file); this.interaction.withRequestBinaryBody(body, contentType); return this; } /** * Sets the request body as multipart/form-data content. * This is useful for testing APIs that accept file uploads or multipart form submissions. * * @param contentType - The content type of the multipart body (e.g., 'multipart/form-data') * @param file - Path to the file containing the multipart body content * @param mimePartName - The name of the mime part in the multipart body * @param boundary - Optional boundary string for the multipart content. If not provided, will be passed as undefined. * @returns The V4RequestBuilder instance for method chaining */ multipartBody(contentType, file, mimePartName, boundary) { this.interaction.withRequestMultipartBody(contentType, file, mimePartName, boundary); return this; } /** * Applies matching rules to the consumer request. * Matching rules allow you to define flexible matching criteria for request attributes * beyond exact equality (e.g., regex patterns, type matching, number ranges). * * @param rules - The matching rules as a strongly typed Rules object. Rules should follow the Pact matching rules format. * @returns The V4RequestBuilder instance for method chaining */ matchingRules(rules) { (0, matchingRules_1.validateRules)(rules); const ffiRules = (0, matchingRules_1.convertRulesToFFI)(rules); this.interaction.withRequestMatchingRules(JSON.stringify(ffiRules)); return this; } body(contentType, body) { this.interaction.withRequestBinaryBody(body, contentType); return this; } } exports.RequestBuilder = RequestBuilder; //# sourceMappingURL=requestBuilder.js.map