From 2d8ea8b250ea54cd2d41668c6c37ecb31020f9b1 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Sat, 4 Jan 2025 19:48:06 +0100 Subject: [PATCH] fix(FetchResponse): add `parseRawHeaders` static method --- src/interceptors/ClientRequest/MockHttpSocket.ts | 5 ++--- src/interceptors/Socket/utils/parseRawHeaders.ts | 10 ---------- src/utils/fetchUtils.ts | 11 +++++++++++ 3 files changed, 13 insertions(+), 13 deletions(-) delete mode 100644 src/interceptors/Socket/utils/parseRawHeaders.ts diff --git a/src/interceptors/ClientRequest/MockHttpSocket.ts b/src/interceptors/ClientRequest/MockHttpSocket.ts index d759ebc1..45cf6b5d 100644 --- a/src/interceptors/ClientRequest/MockHttpSocket.ts +++ b/src/interceptors/ClientRequest/MockHttpSocket.ts @@ -12,7 +12,6 @@ import { MockSocket } from '../Socket/MockSocket' import type { NormalizedSocketWriteArgs } from '../Socket/utils/normalizeSocketWriteArgs' import { isPropertyAccessible } from '../../utils/isPropertyAccessible' import { baseUrlFromConnectionOptions } from '../Socket/utils/baseUrlFromConnectionOptions' -import { parseRawHeaders } from '../Socket/utils/parseRawHeaders' import { createServerErrorResponse } from '../../utils/responseUtils' import { createRequestId } from '../../createRequestId' import { getRawFetchHeaders } from './utils/recordRawHeaders' @@ -472,7 +471,7 @@ export class MockHttpSocket extends MockSocket { const url = new URL(path, this.baseUrl) const method = this.connectionOptions.method?.toUpperCase() || 'GET' - const headers = parseRawHeaders(rawHeaders) + const headers = FetchResponse.parseRawHeaders(rawHeaders) const canHaveBody = method !== 'GET' && method !== 'HEAD' // Translate the basic authorization in the URL to the request header. @@ -565,7 +564,7 @@ export class MockHttpSocket extends MockSocket { status, statusText ) => { - const headers = parseRawHeaders(rawHeaders) + const headers = FetchResponse.parseRawHeaders(rawHeaders) const response = new FetchResponse( /** diff --git a/src/interceptors/Socket/utils/parseRawHeaders.ts b/src/interceptors/Socket/utils/parseRawHeaders.ts deleted file mode 100644 index c66ee2f8..00000000 --- a/src/interceptors/Socket/utils/parseRawHeaders.ts +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Create a Fetch API `Headers` instance from the given raw headers list. - */ -export function parseRawHeaders(rawHeaders: Array): Headers { - const headers = new Headers() - for (let line = 0; line < rawHeaders.length; line += 2) { - headers.append(rawHeaders[line], rawHeaders[line + 1]) - } - return headers -} diff --git a/src/utils/fetchUtils.ts b/src/utils/fetchUtils.ts index 4931203f..37f75929 100644 --- a/src/utils/fetchUtils.ts +++ b/src/utils/fetchUtils.ts @@ -44,6 +44,17 @@ export class FetchResponse extends Response { }) } + /** + * Parses the given raw HTTP headers into a Fetch API `Headers` instance. + */ + static parseRawHeaders(rawHeaders: Array): Headers { + const headers = new Headers() + for (let line = 0; line < rawHeaders.length; line += 2) { + headers.append(rawHeaders[line], rawHeaders[line + 1]) + } + return headers + } + constructor(body?: BodyInit | null, init: FetchResponseInit = {}) { const status = init.status ?? 200 const safeStatus = FetchResponse.isConfigurableStatusCode(status)