@pact-foundation/pact
Version:
Pact for all things Javascript
66 lines • 2.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResponseBuilder = void 0;
const ramda_1 = require("ramda");
const matchingRules_1 = require("../../common/matchingRules");
const matchers_1 = require("../../v3/matchers");
const _1 = require(".");
class ResponseBuilder {
interaction;
constructor(interaction) {
this.interaction = interaction;
}
headers(headers) {
(0, ramda_1.forEachObjIndexed)((v, k) => {
this.interaction.withResponseHeader(`${k}`, 0, (0, matchers_1.matcherValueOrString)(v));
}, headers);
return this;
}
jsonBody(body) {
this.interaction.withResponseBody((0, matchers_1.matcherValueOrString)(body), 'application/json');
return this;
}
xmlBody(body) {
this.interaction.withResponseBody((0, matchers_1.matcherValueOrString)(body), 'application/xml');
return this;
}
binaryFile(contentType, file) {
const body = (0, _1.readBinaryData)(file);
this.interaction.withResponseBinaryBody(body, contentType);
return this;
}
/**
* Sets the response body as multipart/form-data content.
* This is useful for testing APIs that respond with multipart/form-data.
*
* @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 V4ResponseBuilder instance for method chaining
*/
multipartBody(contentType, file, mimePartName, boundary) {
this.interaction.withResponseMultipartBody(contentType, file, mimePartName, boundary);
return this;
}
/**
* Applies matching rules to the provider response.
* Matching rules allow you to define flexible matching criteria for response 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 V4ResponseBuilder instance for method chaining
*/
matchingRules(rules) {
(0, matchingRules_1.validateRules)(rules);
const ffiRules = (0, matchingRules_1.convertRulesToFFI)(rules);
this.interaction.withResponseMatchingRules(JSON.stringify(ffiRules));
return this;
}
body(contentType, body) {
this.interaction.withResponseBinaryBody(body, contentType);
return this;
}
}
exports.ResponseBuilder = ResponseBuilder;
//# sourceMappingURL=responseBuilder.js.map