|
1 | 1 | import { GraphQLClient, rawRequest, request } from '../src/index.js' |
2 | | -import type { RequestConfig } from '../src/types.js' |
3 | 2 | import { setupMockServer } from './__helpers.js' |
4 | 3 | import { gql } from 'graphql-tag' |
5 | 4 | import type { Mock } from 'vitest' |
@@ -62,65 +61,6 @@ test(`minimal raw query with response headers`, async () => { |
62 | 61 | expect(headers.get(`X-Custom-Header`)).toEqual(reqHeaders![`X-Custom-Header`]) |
63 | 62 | }) |
64 | 63 |
|
65 | | -test(`minimal raw query with response headers and new graphql content type`, async () => { |
66 | | - const { headers: _, body } = ctx.res({ |
67 | | - headers: { |
68 | | - 'Content-Type': `application/graphql+json`, |
69 | | - }, |
70 | | - body: { |
71 | | - data: { |
72 | | - me: { |
73 | | - id: `some-id`, |
74 | | - }, |
75 | | - }, |
76 | | - extensions: { |
77 | | - version: `1`, |
78 | | - }, |
79 | | - }, |
80 | | - }).spec |
81 | | - |
82 | | - const { headers: __, ...result } = await rawRequest(ctx.url, `{ me { id } }`) |
83 | | - |
84 | | - expect(result).toEqual({ ...body, status: 200 }) |
85 | | -}) |
86 | | - |
87 | | -test(`minimal raw query with response headers and application/graphql-response+json response type`, async () => { |
88 | | - const { headers: _, body } = ctx.res({ |
89 | | - headers: { |
90 | | - 'Content-Type': `application/graphql-response+json`, |
91 | | - }, |
92 | | - body: { |
93 | | - data: { |
94 | | - me: { |
95 | | - id: `some-id`, |
96 | | - }, |
97 | | - }, |
98 | | - extensions: { |
99 | | - version: `1`, |
100 | | - }, |
101 | | - }, |
102 | | - }).spec |
103 | | - |
104 | | - const { headers: __, ...result } = await rawRequest(ctx.url, `{ me { id } }`) |
105 | | - |
106 | | - expect(result).toEqual({ ...body, status: 200 }) |
107 | | -}) |
108 | | - |
109 | | -test(`content-type with charset`, async () => { |
110 | | - const { data } = ctx.res({ |
111 | | - // headers: { 'Content-Type': 'application/json; charset=utf-8' }, |
112 | | - body: { |
113 | | - data: { |
114 | | - me: { |
115 | | - id: `some-id`, |
116 | | - }, |
117 | | - }, |
118 | | - }, |
119 | | - }).spec.body! |
120 | | - |
121 | | - expect(await request(ctx.url, `{ me { id } }`)).toEqual(data) |
122 | | -}) |
123 | | - |
124 | 64 | test(`basic error`, async () => { |
125 | 65 | ctx.res({ |
126 | 66 | body: { |
@@ -336,31 +276,6 @@ test.skip(`extra fetch options`, async () => { |
336 | 276 | `) |
337 | 277 | }) |
338 | 278 |
|
339 | | -test(`case-insensitive content-type header for custom fetch`, async () => { |
340 | | - const testData = { data: { test: `test` } } |
341 | | - const testResponseHeaders = new Map() |
342 | | - testResponseHeaders.set(`ConTENT-type`, `apPliCatiON/JSON`) |
343 | | - |
344 | | - const options: RequestConfig = { |
345 | | - // @ts-expect-error testing |
346 | | - fetch: (url) => |
347 | | - Promise.resolve({ |
348 | | - headers: testResponseHeaders, |
349 | | - data: testData, |
350 | | - json: () => testData, |
351 | | - text: () => JSON.stringify(testData), |
352 | | - ok: true, |
353 | | - status: 200, |
354 | | - url, |
355 | | - }), |
356 | | - } |
357 | | - |
358 | | - const client = new GraphQLClient(ctx.url, options) |
359 | | - const result = await client.request(`{ test }`) |
360 | | - |
361 | | - expect(result).toEqual(testData.data) |
362 | | -}) |
363 | | - |
364 | 279 | describe(`operationName parsing`, () => { |
365 | 280 | it(`should work for gql documents`, async () => { |
366 | 281 | 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 |
405 | 320 |
|
406 | 321 | expect(res).toEqual(expect.objectContaining({ test: `test` })) |
407 | 322 | }) |
| 323 | + |
| 324 | +it(`adds the default headers to the request`, async () => { |
| 325 | + const mock = ctx.res({ body: { data: {} } }) |
| 326 | + await request( |
| 327 | + ctx.url, |
| 328 | + gql` |
| 329 | + query myGqlOperation { |
| 330 | + users |
| 331 | + } |
| 332 | + `, |
| 333 | + ) |
| 334 | + |
| 335 | + const headers = mock.requests[0]?.headers |
| 336 | + expect(headers?.[`accept`]).toEqual(`application/graphql-response+json, application/json`) |
| 337 | + expect(headers?.[`content-type`]).toEqual(`application/json`) |
| 338 | +}) |
| 339 | + |
| 340 | +it(`allows overriding the default headers for the request`, async () => { |
| 341 | + const mock = ctx.res({ body: { data: {} } }) |
| 342 | + const query = gql` |
| 343 | + query myGqlOperation { |
| 344 | + users |
| 345 | + } |
| 346 | + ` |
| 347 | + |
| 348 | + await request({ |
| 349 | + url: ctx.url, |
| 350 | + document: query, |
| 351 | + requestHeaders: { |
| 352 | + accept: `text/plain`, |
| 353 | + 'content-type': `text/plain`, |
| 354 | + }, |
| 355 | + }) |
| 356 | + |
| 357 | + const headers = mock.requests[0]?.headers |
| 358 | + expect(headers?.[`accept`]).toEqual(`text/plain`) |
| 359 | + expect(headers?.[`content-type`]).toEqual(`text/plain`) |
| 360 | +}) |
0 commit comments