@ply-ct/ply
Version:
REST API Automated Testing
107 lines • 4.39 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlyResponse = void 0;
const ply_values_1 = require("@ply-ct/ply-values");
const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
const util_1 = require("./util");
class PlyResponse {
constructor(runId, status, headers, body, time) {
this.runId = runId;
this.status = status;
this.headers = headers;
this.body = body;
this.time = time;
}
/**
* Orders body object keys unless suppressed by options.
* Sorts arrays if...
* Does not substitute values.
* Response header keys are always lowercase.
* Binary response body is base64 encoded.
* @param runId
* @param options
* @param wantedHeaders optional name of headers subset to keep
* @param stringBody body object is stringified (windows eols are replaced if prettyIndent)
*/
getResponse(runId, options, massagers, stringBody = false) {
const headers = {};
const headerNames = (massagers === null || massagers === void 0 ? void 0 : massagers.headers) || Object.keys(this.headers);
headerNames.forEach((h) => {
headers[h.toLowerCase()] = this.headers[h];
});
let body = this.body;
let isJsonString = false;
if (typeof body === 'string' && (0, util_1.isJson)(body)) {
try {
body = JSON.parse(body);
isJsonString = true;
}
catch (err) {
// cannot parse -- body remains string
if (options.verbose)
console.debug(err);
}
}
if ((0, util_1.isBinary)(this.headers, options)) {
if (body) {
body = (0, util_1.uintArrayToBase64)(new Uint8Array(body));
}
}
else if (typeof body === 'object' && (massagers === null || massagers === void 0 ? void 0 : massagers.arraySorts)) {
for (const expr of Object.keys(massagers.arraySorts)) {
if (expr === '${response.body}' || expr.startsWith('${response.body.')) {
const arr = (0, ply_values_1.safeEval)(expr, { response: { body } });
if (Array.isArray(arr)) {
const prop = massagers.arraySorts[expr];
arr.sort((a1, a2) => {
const v1 = a1[prop];
const v2 = a2[prop];
if (v1 === undefined && v2 !== undefined)
return -1;
if (v2 === undefined && v1 !== undefined)
return 1;
if (typeof v1 === 'number' && typeof v2 === 'number')
return v1 - v2;
return ('' + v1).localeCompare('' + v2);
});
}
}
}
}
if (stringBody && typeof body === 'object') {
if (options.responseBodySortedKeys) {
body = (0, json_stable_stringify_1.default)(body, { space: ''.padStart(options.prettyIndent || 0, ' ') });
}
else {
body = JSON.stringify(body, null, options.prettyIndent);
}
}
else if (!isJsonString &&
typeof body === 'string' &&
body.trim().startsWith('<') &&
options.prettyIndent) {
// format XML
try {
const xmlOpts = {
indentation: ''.padStart(options.prettyIndent, ' '),
collapseContent: true
};
body = require('xml-formatter')(body, xmlOpts);
}
catch (err) {
// cannot format
if (options.verbose)
console.debug(err);
}
}
if (body && stringBody && options.prettyIndent) {
body = (0, util_1.fixEol)(body);
}
return new PlyResponse(runId, this.status, headers, body, this.time);
}
}
exports.PlyResponse = PlyResponse;
//# sourceMappingURL=response.js.map