Skip to content

Commit

Permalink
fix(FetchResponse): add parseRawHeaders static method (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Jan 4, 2025
1 parent fa66fd1 commit 1691679
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/interceptors/ClientRequest/MockHttpSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -565,7 +564,7 @@ export class MockHttpSocket extends MockSocket {
status,
statusText
) => {
const headers = parseRawHeaders(rawHeaders)
const headers = FetchResponse.parseRawHeaders(rawHeaders)

const response = new FetchResponse(
/**
Expand Down
10 changes: 0 additions & 10 deletions src/interceptors/Socket/utils/parseRawHeaders.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/utils/fetchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>): 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)
Expand Down

0 comments on commit 1691679

Please sign in to comment.