114 lines
19 KiB
JavaScript
114 lines
19 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const testcafe_hammerhead_1 = require("testcafe-hammerhead");
|
||
|
const hook_1 = __importDefault(require("./hook"));
|
||
|
const parse_user_agent_1 = require("../../utils/parse-user-agent");
|
||
|
const test_run_tracker_1 = __importDefault(require("../test-run-tracker"));
|
||
|
const re_executable_promise_1 = __importDefault(require("../../utils/re-executable-promise"));
|
||
|
const runtime_1 = require("../../errors/runtime");
|
||
|
const types_1 = require("../../errors/types");
|
||
|
const DEFAULT_OPTIONS = {
|
||
|
logRequestHeaders: false,
|
||
|
logRequestBody: false,
|
||
|
stringifyRequestBody: false,
|
||
|
logResponseHeaders: false,
|
||
|
logResponseBody: false,
|
||
|
stringifyResponseBody: false,
|
||
|
};
|
||
|
const REQUEST_LOGGER_CLASS_NAME = 'RequestLogger';
|
||
|
class RequestLoggerImplementation extends hook_1.default {
|
||
|
constructor(requestFilterRuleInit, options) {
|
||
|
const effectiveOptions = Object.assign({}, DEFAULT_OPTIONS, options);
|
||
|
RequestLoggerImplementation._assertLogOptions(effectiveOptions);
|
||
|
const configureResponseEventOptions = new testcafe_hammerhead_1.ConfigureResponseEventOptions(effectiveOptions.logResponseHeaders, effectiveOptions.logResponseBody);
|
||
|
super(requestFilterRuleInit, configureResponseEventOptions);
|
||
|
this._className = REQUEST_LOGGER_CLASS_NAME;
|
||
|
this._options = effectiveOptions;
|
||
|
this._internalRequests = {};
|
||
|
}
|
||
|
static _assertLogOptions(logOptions) {
|
||
|
if (!logOptions.logRequestBody && logOptions.stringifyRequestBody)
|
||
|
throw new runtime_1.APIError('RequestLogger', types_1.RUNTIME_ERRORS.requestHookConfigureAPIError, 'RequestLogger', 'Cannot stringify the request body because it is not logged. Specify { logRequestBody: true } in log options.');
|
||
|
if (!logOptions.logResponseBody && logOptions.stringifyResponseBody)
|
||
|
throw new runtime_1.APIError('RequestLogger', types_1.RUNTIME_ERRORS.requestHookConfigureAPIError, 'RequestLogger', 'Cannot stringify the response body because it is not logged. Specify { logResponseBody: true } in log options.');
|
||
|
}
|
||
|
async onRequest(event) {
|
||
|
const loggedReq = {
|
||
|
id: event._requestInfo.requestId,
|
||
|
testRunId: event._requestInfo.sessionId,
|
||
|
userAgent: (0, parse_user_agent_1.parseUserAgent)(event._requestInfo.userAgent).prettyUserAgent,
|
||
|
request: {
|
||
|
timestamp: Date.now(),
|
||
|
url: event._requestInfo.url,
|
||
|
method: event._requestInfo.method,
|
||
|
},
|
||
|
};
|
||
|
if (this._options.logRequestHeaders)
|
||
|
loggedReq.request.headers = Object.assign({}, event._requestInfo.headers);
|
||
|
if (this._options.logRequestBody)
|
||
|
loggedReq.request.body = this._options.stringifyRequestBody ? event._requestInfo.body.toString() : event._requestInfo.body;
|
||
|
this._internalRequests[loggedReq.id] = loggedReq;
|
||
|
}
|
||
|
async onResponse(event) {
|
||
|
const loggedReq = this._internalRequests[event.requestId];
|
||
|
// NOTE: If the 'clear' method is called during a long running request,
|
||
|
// we should not save a response part - request part has been already removed.
|
||
|
if (!loggedReq)
|
||
|
return;
|
||
|
loggedReq.response = {
|
||
|
statusCode: event.statusCode,
|
||
|
timestamp: Date.now(),
|
||
|
};
|
||
|
if (this._options.logResponseHeaders)
|
||
|
loggedReq.response.headers = Object.assign({}, event.headers);
|
||
|
if (this._options.logResponseBody) {
|
||
|
loggedReq.response.body = this._options.stringifyResponseBody && event.body
|
||
|
? event.body.toString()
|
||
|
: event.body;
|
||
|
}
|
||
|
}
|
||
|
_prepareInternalRequestInfo() {
|
||
|
const testRun = test_run_tracker_1.default.resolveContextTestRun();
|
||
|
let preparedRequests = Object.values(this._internalRequests);
|
||
|
if (testRun)
|
||
|
preparedRequests = preparedRequests.filter(r => r.testRunId === testRun.id);
|
||
|
return preparedRequests;
|
||
|
}
|
||
|
_getCompletedRequests() {
|
||
|
return this._prepareInternalRequestInfo().filter(r => r.response);
|
||
|
}
|
||
|
// API
|
||
|
contains(predicate) {
|
||
|
return re_executable_promise_1.default.fromFn(async () => {
|
||
|
return !!this._getCompletedRequests().find(predicate);
|
||
|
});
|
||
|
}
|
||
|
count(predicate) {
|
||
|
return re_executable_promise_1.default.fromFn(async () => {
|
||
|
return this._getCompletedRequests().filter(predicate).length;
|
||
|
});
|
||
|
}
|
||
|
clear() {
|
||
|
const testRun = test_run_tracker_1.default.resolveContextTestRun();
|
||
|
if (testRun) {
|
||
|
Object.keys(this._internalRequests).forEach(id => {
|
||
|
if (this._internalRequests[id].testRunId === testRun.id)
|
||
|
delete this._internalRequests[id];
|
||
|
});
|
||
|
}
|
||
|
else
|
||
|
this._internalRequests = {};
|
||
|
}
|
||
|
get requests() {
|
||
|
return this._prepareInternalRequestInfo();
|
||
|
}
|
||
|
}
|
||
|
function createRequestLogger(requestFilterRuleInit, logOptions) {
|
||
|
return new RequestLoggerImplementation(requestFilterRuleInit, logOptions);
|
||
|
}
|
||
|
exports.default = createRequestLogger;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVxdWVzdC1sb2dnZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvYXBpL3JlcXVlc3QtaG9va3MvcmVxdWVzdC1sb2dnZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQSw2REFLNkI7QUFFN0Isa0RBQWlDO0FBQ2pDLG1FQUE4RDtBQUM5RCwyRUFBaUQ7QUFDakQsOEZBQW9FO0FBQ3BFLGtEQUFnRDtBQUNoRCw4Q0FBb0Q7QUFVcEQsTUFBTSxlQUFlLEdBQTBCO0lBQzNDLGlCQUFpQixFQUFNLEtBQUs7SUFDNUIsY0FBYyxFQUFTLEtBQUs7SUFDNUIsb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