flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
102 lines • 3.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpResponse = void 0;
const fs_extra_1 = require("fs-extra");
class HttpResponse {
constructor(opts) {
this.body = "";
this.json = null;
this.statusCode = 0;
this.statusMessage = "";
this.headers = {};
this.cookies = {};
this.trailers = {};
this.url = "";
this.method = "";
if (opts) {
this.body =
typeof opts.body == "string" ? opts.body : JSON.stringify(opts.body);
this.statusCode = opts.status ? opts.status[0] : 200;
this.statusMessage = opts.status ? opts.status[1] : "OK";
this.headers = opts.headers || {};
this.cookies = opts.cookies || {};
this.trailers = opts.trailers || {};
this.url = opts.url || "";
this.method = opts.method || "";
}
}
static createEmpty() {
const r = new HttpResponse();
return r;
}
static fromNeedle(response) {
const r = new HttpResponse({
status: [response.statusCode || 0, response.statusMessage || ""],
headers: response.headers,
body: typeof response.body === "string"
? response.body
: response.body.toString("utf8"),
cookies: response.cookies ? response.cookies : {},
trailers: response.trailers,
method: response.method,
url: response.url,
});
return r;
}
static fromPuppeteer(response, body, cookies) {
const r = new HttpResponse();
r.statusCode = response.status();
r.statusMessage = response.statusText();
r.headers = response.headers();
r.body = body;
r.cookies = cookies || {};
r.url = response.url();
return r;
}
static fromJsonData(request, data) {
const r = new HttpResponse();
r.headers = {};
r.statusCode = 200;
r.body = "";
r.json = data;
r.url = request.uri || "";
return r;
}
static fromProbeImage(response, cookies) {
const r = new HttpResponse();
r.headers = response.headers;
r.statusCode = response.statusCode;
r.json = Object.assign(Object.assign({}, response.imageData), {
length: response.length,
url: response.url,
mime: response.imageData.mimeType,
});
r.body = "";
r.cookies = cookies || {};
r.url = response.url;
return r;
}
static fromLocalFile(relativePath) {
let path = __dirname + "/" + relativePath;
return new Promise((resolve, reject) => {
fs_extra_1.readFile(path, (err, data) => {
if (err) {
return reject(err);
}
resolve(new HttpResponse({
body: data.toString(),
}));
});
});
}
static fromString(content) {
return new HttpResponse({
body: content,
});
}
static fromOpts(opts) {
return new HttpResponse(opts);
}
}
exports.HttpResponse = HttpResponse;
//# sourceMappingURL=httpresponse.js.map