UNPKG

e2ed

Version:

E2E testing framework over Playwright

34 lines (33 loc) 1.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSetResponse = void 0; const node_async_hooks_1 = require("node:async_hooks"); const asserts_1 = require("../asserts"); const http_1 = require("../http"); const log_1 = require("../log"); const parse_1 = require("../parse"); /** * Get `setResponse` function for WebSocket mocks by `WebSocketMockState`. * @internal */ const getSetResponse = ({ optionsWithRouteByUrl, }) => node_async_hooks_1.AsyncLocalStorage.bind((playwrightRoute) => { const url = playwrightRoute.url(); const optionsWithRoute = optionsWithRouteByUrl[url]; (0, asserts_1.assertValueIsDefined)(optionsWithRoute, 'optionsWithRoute is defined', { url }); const { skipLogs, route, webSocketMockFunction } = optionsWithRoute; const isRequestBodyInJsonFormat = route.getIsRequestBodyInJsonFormat(); const isResponseBodyInJsonFormat = route.getIsResponseBodyInJsonFormat(); playwrightRoute.onMessage(node_async_hooks_1.AsyncLocalStorage.bind(async (message) => { const { value: request, hasParseError } = (0, parse_1.parseValueAsJsonIfNeeded)(String(message), isRequestBodyInJsonFormat); if (hasParseError && skipLogs !== true) { (0, log_1.log)('WebSocket message is not in JSON format', { logEventStatus: "failed" /* LogEventStatus.Failed */, message, url }, 8 /* LogEventType.InternalUtil */); } const response = await webSocketMockFunction(route.routeParams, request); const responseAsString = (0, http_1.getBodyAsString)(response, isResponseBodyInJsonFormat); playwrightRoute.send(responseAsString); if (skipLogs !== true) { (0, log_1.log)(`A mock was applied to the WebSocket route "${route.constructor.name}"`, { request, response, route, webSocketMockFunction }, 8 /* LogEventType.InternalUtil */); } })); }); exports.getSetResponse = getSetResponse;