Skip to content

Commit

Permalink
Reapply changes to node-http (#2705)
Browse files Browse the repository at this point in the history
  • Loading branch information
domdomegg authored May 5, 2023
1 parent 5dd8770 commit 2234a6a
Showing 1 changed file with 50 additions and 7 deletions.
57 changes: 50 additions & 7 deletions packages/graphql-yoga/__integration-tests__/node-http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import {
IncomingMessage,
Server,
ServerResponse,
STATUS_CODES,
} from 'node:http'
import { AddressInfo } from 'node:net'
import { fetch } from '@whatwg-node/fetch'

import { createSchema, createYoga } from '../src/index.js'
import { createGraphQLError, createSchema, createYoga } from '../src/index.js'

it('should expose Node req and res objects in the context', async () => {
describe('node-http', () => {
let server: Server
try {
let port: number

beforeAll(async () => {
const yoga = createYoga<{
req: IncomingMessage
res: ServerResponse
Expand All @@ -20,27 +23,67 @@ it('should expose Node req and res objects in the context', async () => {
typeDefs: /* GraphQL */ `
type Query {
isNode: Boolean!
throw(status: Int): Int!
}
`,
resolvers: {
Query: {
isNode: (_, __, { req, res }) => Boolean(req) && Boolean(res),
throw(_, { status }) {
throw createGraphQLError('Test', {
extensions: {
http: {
status,
},
},
})
},
},
},
}),
logging: false,
})
server = createServer(yoga)
await new Promise<void>((resolve) => server.listen(0, resolve))
const port = (server.address() as AddressInfo).port
port = (server.address() as AddressInfo).port
})

afterAll(async () => {
if (server) {
await new Promise<void>((resolve) => server.close(() => resolve()))
}
})

it('should expose Node req and res objects in the context', async () => {
const response = await fetch(
`http://localhost:${port}/graphql?query=query{isNode}`,
)
expect(response.status).toBe(200)
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data.isNode).toBe(true)
} finally {
await new Promise<void>((resolve) => server.close(() => resolve()))
}
})

it('should set status text by status code', async () => {
for (const statusCodeStr in STATUS_CODES) {
const status = Number(statusCodeStr)
if (status < 200) continue
const response = await fetch(`http://localhost:${port}/graphql`, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
query: /* GraphQL */ `
query StatusTest($status: Int!) {
throw(status: $status)
}
`,
variables: { status },
}),
})
expect(response.status).toBe(status)
expect(response.statusText).toBe(STATUS_CODES[status])
}
})
})

0 comments on commit 2234a6a

Please sign in to comment.