Skip to content

Commit

Permalink
chore: fix happy-dom returning case-sensitive headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jul 11, 2024
1 parent b78d09a commit 272cdb7
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 6 deletions.
3 changes: 0 additions & 3 deletions src/open-api/utils/get-servers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* @vitest-environment jsdom
*/
import { createOpenApiSpec } from '../../../test/support/create-open-api-spec.js'
import { getServers } from './get-servers.js'

Expand Down
1 change: 1 addition & 0 deletions test/oas/oas-json-schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { fromOpenApi } from '../../src/open-api/from-open-api.js'
import { withHandlers } from '../support/with-handlers.js'
import { createOpenApiSpec } from '../support/create-open-api-spec.js'
Expand Down
1 change: 1 addition & 0 deletions test/oas/oas-response-headers.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { fromOpenApi } from '../../src/open-api/from-open-api.js'
import { createOpenApiSpec } from '../../test/support/create-open-api-spec.js'
import { InspectedHandler, inspectHandlers } from '../support/inspect.js'
Expand Down
1 change: 1 addition & 0 deletions test/oas/oas-response.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { fromOpenApi } from '../../src/open-api/from-open-api.js'
import { InspectedHandler, inspectHandlers } from '../support/inspect.js'

Expand Down
1 change: 1 addition & 0 deletions test/oas/oas-servers.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { fromOpenApi } from '../../src/open-api/from-open-api.js'
import { createOpenApiSpec } from '../support/create-open-api-spec.js'
import { InspectedHandler, inspectHandlers } from '../support/inspect.js'
Expand Down
1 change: 1 addition & 0 deletions test/oas/petstore.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment happy-dom
import { RequestHandler } from 'msw'
import { fromOpenApi } from '../../src/open-api/from-open-api.js'
import { withHandlers } from '../../test/support/with-handlers.js'
Expand Down
12 changes: 11 additions & 1 deletion test/support/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ export async function inspectHandlers(handlers: Array<RequestHandler>) {
return await Promise.all(handlers.map(inspectHandler))
}

export function normalizeHeaders(headers: Headers) {
const nextHeaders = Array.from(headers.entries()).map<[string, string]>(
([name, value]) => {
return [name.toLowerCase(), value]
},
)
nextHeaders.sort()
return nextHeaders
}

async function serializeResponse(
response?: Response,
): Promise<SerializedResponse | undefined> {
Expand All @@ -95,7 +105,7 @@ async function serializeResponse(
return {
status: response.status,
statusText: response.statusText,
headers: Array.from(response.headers),
headers: normalizeHeaders(response.headers),
body: await response.text(),
}
}
1 change: 1 addition & 0 deletions test/traffic/request-parameters.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'node:crypto'
import { fromTraffic } from '../../src/traffic/from-traffic.js'
import { readArchive, normalizeLocalhost, _toHeaders } from './utils/index.js'

Expand Down
1 change: 1 addition & 0 deletions test/traffic/response-stream.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @vitest-environment node
import { fromTraffic } from '../../src/traffic/from-traffic.js'
import { InspectedHandler, inspectHandlers } from '../support/inspect.js'
import { normalizeLocalhost, readArchive, _toHeaders } from './utils/index.js'
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default defineConfig({
test: {
globals: true,
setupFiles: ['./vitest.setup.ts'],
environment: 'happy-dom',
environment: 'node',
environmentOptions: {
happyDOM: {
url: 'http://localhost',
Expand Down
6 changes: 5 additions & 1 deletion vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { invariant } from 'outvariant'
import { faker } from '@yellow-ticket/seed-json-schema'
import { normalizeHeaders } from './test/support/inspect'

beforeEach(() => {
faker.seed(1)
Expand Down Expand Up @@ -59,7 +60,10 @@ expect.extend({

// Headers.
if (
!this.equals(Array.from(actual.headers), Array.from(expected.headers))
!this.equals(
normalizeHeaders(actual.headers),
normalizeHeaders(expected.headers),
)
) {
return {
pass: false,
Expand Down

0 comments on commit 272cdb7

Please sign in to comment.