UNPKG

flagpole

Version:

Simple and fast DOM integration, headless or headful browser, and REST API testing framework.

142 lines 4.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const url_1 = require("url"); const assertioncontext_1 = require("./assertioncontext"); const value_1 = require("./value"); var ResponseType; (function (ResponseType) { ResponseType[ResponseType["html"] = 0] = "html"; ResponseType[ResponseType["json"] = 1] = "json"; ResponseType[ResponseType["image"] = 2] = "image"; ResponseType[ResponseType["stylesheet"] = 3] = "stylesheet"; ResponseType[ResponseType["script"] = 4] = "script"; ResponseType[ResponseType["video"] = 5] = "video"; ResponseType[ResponseType["audio"] = 6] = "audio"; ResponseType[ResponseType["resource"] = 7] = "resource"; ResponseType[ResponseType["browser"] = 8] = "browser"; ResponseType[ResponseType["extjs"] = 9] = "extjs"; })(ResponseType = exports.ResponseType || (exports.ResponseType = {})); class NormalizedResponse { constructor() { this.body = ''; this.statusCode = 0; this.statusMessage = ''; this.headers = {}; this.cookies = []; } static fromRequest(response, body, cookies) { const r = new NormalizedResponse(); r.statusCode = response.statusCode || 0; r.statusMessage = response.statusMessage || ''; r.headers = response.headers; r.body = body; r.cookies = cookies; return r; } static fromPuppeteer(response, body, cookies) { const r = new NormalizedResponse(); r.statusCode = response.status(); r.statusMessage = response.statusText(); r.headers = response.headers(); r.body = body; r.cookies = cookies; return r; } static fromProbeImage(response, cookies) { const r = new NormalizedResponse(); r.headers = { 'content-type': response.mime }; r.body = JSON.stringify(response); return r; } static fromLocalFile(relativePath) { const r = new NormalizedResponse(); let fs = require('fs'); let path = __dirname + '/' + relativePath; return new Promise((resolve, reject) => { fs.readFile(path, function (err, data) { if (err) { return reject(err); } r.body = data.toString(); resolve(r); }); }); } } exports.NormalizedResponse = NormalizedResponse; class GenericResponse { get isBrowser() { return false; } get statusCode() { return this._wrapAsValue(this._response.statusCode, 'HTTP Status Code'); } get statusMessage() { return this._wrapAsValue(this._response.statusMessage, 'HTTP Status Message'); } get body() { return this._wrapAsValue(this._response.body, 'Raw Response Body'); } get length() { return this._wrapAsValue(this._response.body.length, 'Length of Response Body'); } get headers() { return this._wrapAsValue(this._response.headers, 'HTTP Headers'); } get cookies() { return this._wrapAsValue(this._response.cookies, 'HTTP Cookies'); } get jsonBody() { try { const json = JSON.parse(this._response.body); return this._wrapAsValue(json, 'JSON Response'); } catch (ex) { return this._wrapAsValue(null, 'JSON Response'); } } get url() { return this._wrapAsValue(this.scenario.url, 'Request URL'); } get finalUrl() { return this._wrapAsValue(this.scenario.finalUrl, 'Response URL (after redirects)'); } get loadTime() { return this._wrapAsValue(this.scenario.requestDuration, 'Request to Response Load Time'); } get context() { return new assertioncontext_1.AssertionContext(this.scenario, this); } constructor(scenario, response) { this.scenario = scenario; this._response = response; } absolutizeUri(uri) { let baseUrl = new url_1.URL(this.scenario.suite.buildUrl(this.scenario.url || '')); return (new url_1.URL(uri, baseUrl.href)).href; } getRoot() { return this._response.body; } header(key) { key = typeof this._response.headers[key] !== 'undefined' ? key : key.toLowerCase(); const headerValue = this._response.headers[key]; return this._wrapAsValue(typeof headerValue == 'undefined' ? null : headerValue, 'HTTP Headers[' + key + ']'); } cookie(key) { let cookie = null; this._response.cookies.forEach((c) => { if (c.key == key) { cookie = c; } }); return this._wrapAsValue(cookie, 'HTTP Cookies[' + key + ']'); } _wrapAsValue(data, name) { return new value_1.Value(data, this.context, name); } } exports.GenericResponse = GenericResponse; //# sourceMappingURL=response.js.map