46 lines
8.0 KiB
JavaScript
46 lines
8.0 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.sendRequestThroughAPI = exports.send = void 0;
|
|
const create_request_options_1 = require("./create-request-options");
|
|
const process_response_data_1 = require("./process-response-data");
|
|
const http_headers_1 = __importDefault(require("../../utils/http-headers"));
|
|
const runtime_1 = require("../../errors/runtime");
|
|
const lodash_1 = require("lodash");
|
|
const types_1 = require("../../errors/types");
|
|
const testcafe_hammerhead_1 = require("testcafe-hammerhead");
|
|
async function send(requestOptions, rawResponse) {
|
|
const request = new testcafe_hammerhead_1.DestinationRequest(requestOptions);
|
|
const dataWaiter = new Promise(resolve => {
|
|
request.on('response', (res) => resolve(res));
|
|
request.on('error', (err) => resolve(err.message));
|
|
request.on('fatalError', (message) => resolve(message));
|
|
});
|
|
const data = await dataWaiter;
|
|
if (typeof data === 'string')
|
|
return data;
|
|
const body = await (0, process_response_data_1.processResponseData)(data, rawResponse);
|
|
return {
|
|
status: data.statusCode,
|
|
statusText: data.statusMessage,
|
|
headers: data.headers,
|
|
body: body,
|
|
};
|
|
}
|
|
exports.send = send;
|
|
async function sendRequestThroughAPI(testRun, options, callsite) {
|
|
const currentPageUrl = new URL(await testRun.getCurrentUrl());
|
|
const requestOptions = await (0, create_request_options_1.createRequestOptions)(currentPageUrl, testRun, options, callsite);
|
|
const data = await send(requestOptions, options.rawResponse);
|
|
if (typeof data === 'string')
|
|
throw new runtime_1.RequestRuntimeError(callsite, types_1.RUNTIME_ERRORS.requestRuntimeError, data.replace(/<.*?>/g, ''));
|
|
const setCookie = data.headers[http_headers_1.default.setCookie];
|
|
const sameOrigin = !currentPageUrl.host || (0, testcafe_hammerhead_1.sameOriginCheck)(currentPageUrl.href, requestOptions.url);
|
|
if (setCookie && (sameOrigin || options.withCredentials))
|
|
testRun.session.cookies.copySyncCookies((0, lodash_1.castArray)(setCookie).join(';'), currentPageUrl.href);
|
|
return data;
|
|
}
|
|
exports.sendRequestThroughAPI = sendRequestThroughAPI;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2VuZC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy90ZXN0LXJ1bi9yZXF1ZXN0L3NlbmQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBR0EscUVBQWdFO0FBQ2hFLG1FQUE4RDtBQUM5RCw0RUFBb0Q7QUFDcEQsa0RBQTJEO0FBRTNELG1DQUFtQztBQUNuQyw4Q0FBb0Q7QUFFcEQsNkRBSTZCO0FBSXRCLEtBQUssVUFBVSxJQUFJLENBQUUsY0FBOEIsRUFBRSxXQUFxQjtJQUM3RSxNQUFNLE9BQU8sR0FBRyxJQUFJLHdDQUFrQixDQUFDLGNBQWMsQ0FBQyxDQUFDO0lBRXZELE1BQU0sVUFBVSxHQUFHLElBQUksT0FBTyxDQUFpQyxPQUFPLENBQUMsRUFBRTtRQUNyRSxPQUFPLENBQUMsRUFBRSxDQUFDLFVBQVUsRUFBRSxDQUFDLEdBQTBCLEVBQUUsRUFBRSxDQUFDLE9BQU8sQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDO1FBQ3JFLE9BQU8sQ0FBQyxFQUFFLENBQUMsT0FBTyxFQUFFLENBQUMsR0FBVSxFQUFFLEVBQUUsQ0FBQyxPQUFPLENBQUMsR0FBRyxDQUFDLE9BQU8sQ0FBQyxDQUFDLENBQUM7UUFDMUQsT0FBTyxDQUFDLEVBQUUsQ0FBQyxZQUFZLEVBQUUsQ0FBQyxPQUFlLEVBQUUsRUFBRSxDQUFDLE9BQU8sQ0FBQyxPQUFPLENBQUMsQ0FBQyxDQUFDO0lBQ3BFLENBQUMsQ0FBQyxDQUFDO0lBRUgsTUFBTSxJQUFJLE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