-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: add spec compliant default Accept
header
#618
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const ACCEPT_HEADER = `Accept` | ||
export const CONTENT_TYPE_HEADER = `Content-Type` | ||
export const CONTENT_TYPE_JSON = `application/json` | ||
export const CONTENT_TYPE_GQL = `application/graphql-response+json` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { GraphQLClient, rawRequest, request } from '../src/index.js' | ||
import type { RequestConfig } from '../src/types.js' | ||
import { setupMockServer } from './__helpers.js' | ||
import { gql } from 'graphql-tag' | ||
import type { Mock } from 'vitest' | ||
|
@@ -62,65 +61,6 @@ test(`minimal raw query with response headers`, async () => { | |
expect(headers.get(`X-Custom-Header`)).toEqual(reqHeaders![`X-Custom-Header`]) | ||
}) | ||
|
||
test(`minimal raw query with response headers and new graphql content type`, async () => { | ||
const { headers: _, body } = ctx.res({ | ||
headers: { | ||
'Content-Type': `application/graphql+json`, | ||
}, | ||
body: { | ||
data: { | ||
me: { | ||
id: `some-id`, | ||
}, | ||
}, | ||
extensions: { | ||
version: `1`, | ||
}, | ||
}, | ||
}).spec | ||
|
||
const { headers: __, ...result } = await rawRequest(ctx.url, `{ me { id } }`) | ||
|
||
expect(result).toEqual({ ...body, status: 200 }) | ||
}) | ||
|
||
test(`minimal raw query with response headers and application/graphql-response+json response type`, async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We no longer need this test as this header is now sent by default. Also this wrongly set the |
||
const { headers: _, body } = ctx.res({ | ||
headers: { | ||
'Content-Type': `application/graphql-response+json`, | ||
}, | ||
body: { | ||
data: { | ||
me: { | ||
id: `some-id`, | ||
}, | ||
}, | ||
extensions: { | ||
version: `1`, | ||
}, | ||
}, | ||
}).spec | ||
|
||
const { headers: __, ...result } = await rawRequest(ctx.url, `{ me { id } }`) | ||
|
||
expect(result).toEqual({ ...body, status: 200 }) | ||
}) | ||
|
||
test(`content-type with charset`, async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test was not doing anything, the relevant code was already commented out. |
||
const { data } = ctx.res({ | ||
// headers: { 'Content-Type': 'application/json; charset=utf-8' }, | ||
body: { | ||
data: { | ||
me: { | ||
id: `some-id`, | ||
}, | ||
}, | ||
}, | ||
}).spec.body! | ||
|
||
expect(await request(ctx.url, `{ me { id } }`)).toEqual(data) | ||
}) | ||
|
||
test(`basic error`, async () => { | ||
ctx.res({ | ||
body: { | ||
|
@@ -336,31 +276,6 @@ test.skip(`extra fetch options`, async () => { | |
`) | ||
}) | ||
|
||
test(`case-insensitive content-type header for custom fetch`, async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was testing internal behavior of |
||
const testData = { data: { test: `test` } } | ||
const testResponseHeaders = new Map() | ||
testResponseHeaders.set(`ConTENT-type`, `apPliCatiON/JSON`) | ||
|
||
const options: RequestConfig = { | ||
// @ts-expect-error testing | ||
fetch: (url) => | ||
Promise.resolve({ | ||
headers: testResponseHeaders, | ||
data: testData, | ||
json: () => testData, | ||
text: () => JSON.stringify(testData), | ||
ok: true, | ||
status: 200, | ||
url, | ||
}), | ||
} | ||
|
||
const client = new GraphQLClient(ctx.url, options) | ||
const result = await client.request(`{ test }`) | ||
|
||
expect(result).toEqual(testData.data) | ||
}) | ||
|
||
describe(`operationName parsing`, () => { | ||
it(`should work for gql documents`, async () => { | ||
const mock = ctx.res({ body: { data: { foo: 1 } } }) | ||
|
@@ -405,3 +320,41 @@ test(`should not throw error when errors property is an empty array (occurred wh | |
|
||
expect(res).toEqual(expect.objectContaining({ test: `test` })) | ||
}) | ||
|
||
it(`adds the default headers to the request`, async () => { | ||
const mock = ctx.res({ body: { data: {} } }) | ||
await request( | ||
ctx.url, | ||
gql` | ||
query myGqlOperation { | ||
users | ||
} | ||
`, | ||
) | ||
|
||
const headers = mock.requests[0]?.headers | ||
expect(headers?.[`accept`]).toEqual(`application/graphql-response+json, application/json`) | ||
expect(headers?.[`content-type`]).toEqual(`application/json`) | ||
}) | ||
|
||
it(`allows overriding the default headers for the request`, async () => { | ||
const mock = ctx.res({ body: { data: {} } }) | ||
const query = gql` | ||
query myGqlOperation { | ||
users | ||
} | ||
` | ||
|
||
await request({ | ||
url: ctx.url, | ||
document: query, | ||
requestHeaders: { | ||
accept: `text/plain`, | ||
'content-type': `text/plain`, | ||
}, | ||
}) | ||
|
||
const headers = mock.requests[0]?.headers | ||
expect(headers?.[`accept`]).toEqual(`text/plain`) | ||
expect(headers?.[`content-type`]).toEqual(`text/plain`) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,24 +107,6 @@ describe(`using class`, () => { | |
expect(mock.requests[0]?.headers[`x-foo`]).toEqual(`new`) | ||
}) | ||
}) | ||
|
||
describe(`allows content-type header to be overwritten`, () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test was moved into the main suite. |
||
test(`with request method`, async () => { | ||
const headers = new Headers({ 'content-type': `text/plain` }) | ||
const client = new GraphQLClient(ctx.url, { headers }) | ||
const mock = ctx.res() | ||
await client.request(`{ me { id } }`) | ||
expect(mock.requests[0]?.headers[`content-type`]).toEqual(`text/plain`) | ||
}) | ||
|
||
test(`with rawRequest method`, async () => { | ||
const headers = new Headers({ 'content-type': `text/plain` }) | ||
const client = new GraphQLClient(ctx.url, { headers }) | ||
const mock = ctx.res() | ||
await client.rawRequest(`{ me { id } }`) | ||
expect(mock.requests[0]?.headers[`content-type`]).toEqual(`text/plain`) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is no longer relevant as this mime-type is no longer spec compliant.