flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
117 lines • 4.38 kB
JavaScript
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpResponse = exports.parseResponseFromString = exports.createEmptyResponse = exports.parseResponsefromJsonData = exports.parseResponseFromNeedle = exports.parseResponseFromLocalFile = void 0;
const fs_extra_1 = require("fs-extra");
const constants_1 = require("../interfaces/constants");
const parseResponseFromLocalFile = (relativePath) => __awaiter(void 0, void 0, void 0, function* () {
const path = `${__dirname}/${relativePath}`;
const data = yield fs_extra_1.readFile(path);
return new HttpResponse({
body: data.toString(),
});
});
exports.parseResponseFromLocalFile = parseResponseFromLocalFile;
const parseResponseFromNeedle = (response) => {
var _a;
return new HttpResponse({
status: [response.statusCode || 0, response.statusMessage || ""],
headers: response.headers,
body: typeof response.body === "string" ||
((_a = response.headers["content-type"]) === null || _a === void 0 ? void 0 : _a.includes("image"))
? response.body
: response.body.toString("utf8"),
cookies: response.cookies ? response.cookies : {},
trailers: response.trailers,
method: response.method || "get",
url: response.url || "",
rawBody: response.raw,
});
};
exports.parseResponseFromNeedle = parseResponseFromNeedle;
const parseResponsefromJsonData = (jsonBody) => new HttpResponse({
headers: {
"content-type": constants_1.CONTENT_TYPE_JSON,
},
jsonBody,
});
exports.parseResponsefromJsonData = parseResponsefromJsonData;
const createEmptyResponse = () => new HttpResponse({
body: "",
});
exports.createEmptyResponse = createEmptyResponse;
const parseResponseFromString = (body) => new HttpResponse({
body,
});
exports.parseResponseFromString = parseResponseFromString;
class HttpResponse {
constructor(opts) {
this.opts = opts;
}
get method() {
return this.opts.method || "get";
}
get url() {
return this.opts.url || "/";
}
get headers() {
return this.opts.headers || {};
}
get trailers() {
return this.opts.trailers || {};
}
get cookies() {
return this.opts.cookies || {};
}
get statusCode() {
return this.opts.status ? this.opts.status[0] : 200;
}
get statusMessage() {
return this.opts.status ? this.opts.status[1] : "OK";
}
get status() {
return this.opts.status || [200, "OK"];
}
get body() {
return this.opts.body !== undefined
? this.opts.body
: this.opts.rawBody !== undefined
? String(this.opts.rawBody)
: this.opts.jsonBody !== undefined
? JSON.stringify(this.opts.jsonBody)
: "";
}
get rawBody() {
return this.opts.rawBody !== undefined
? this.opts.rawBody
: this.opts.body !== undefined
? this.opts.body
: this.opts.jsonBody !== undefined
? JSON.stringify(this.opts.jsonBody)
: "";
}
get jsonBody() {
try {
if (this.opts.jsonBody !== undefined)
return this.opts.jsonBody;
return this.opts.body != undefined
? JSON.parse(this.opts.body)
: this.opts.rawBody != undefined
? JSON.parse(this.opts.rawBody)
: null;
}
catch (ex) {
return null;
}
}
}
exports.HttpResponse = HttpResponse;
//# sourceMappingURL=http-response.js.map
;