|
| 1 | +import type { |
| 2 | + CreateServerForIntegrationTests, |
| 3 | + CreateServerForIntegrationTestsResult, |
| 4 | +} from './index.js'; |
| 5 | +import { afterAll, beforeAll, describe, test } from '@jest/globals'; |
| 6 | +import { serverAudits } from 'graphql-http'; |
| 7 | +import fetch from 'node-fetch'; |
| 8 | + |
| 9 | +export function defineIntegrationTestSuiteHttpSpecTests( |
| 10 | + createServer: CreateServerForIntegrationTests, |
| 11 | +) { |
| 12 | + describe('httpSpecTests.ts', () => { |
| 13 | + let createServerResult: CreateServerForIntegrationTestsResult; |
| 14 | + |
| 15 | + beforeAll(async () => { |
| 16 | + createServerResult = await createServer({ |
| 17 | + // Any schema will do (the tests just run `{__typename}`). |
| 18 | + typeDefs: 'type Query { x: ID }', |
| 19 | + // The test doesn't know we should send apollo-require-preflight along |
| 20 | + // with GETs. We could override `fetchFn` to add it but this seems simple enough. |
| 21 | + csrfPrevention: false, |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + afterAll(async () => { |
| 26 | + await createServerResult.server.stop(); |
| 27 | + await createServerResult.extraCleanup?.(); |
| 28 | + }); |
| 29 | + |
| 30 | + for (const audit of serverAudits({ |
| 31 | + url: () => createServerResult.url, |
| 32 | + fetchFn: fetch, |
| 33 | + })) { |
| 34 | + test(audit.name, async () => { |
| 35 | + const result = await audit.fn(); |
| 36 | + |
| 37 | + if (result.status === 'ok') { |
| 38 | + return; |
| 39 | + } |
| 40 | + if (result.status === 'error') { |
| 41 | + throw new Error(result.reason); |
| 42 | + } |
| 43 | + |
| 44 | + if (result.status !== 'warn') { |
| 45 | + throw new Error(`unknown status ${result.status}`); |
| 46 | + } |
| 47 | + |
| 48 | + // We failed an optional audit. That's OK, but let's make sure it's |
| 49 | + // one of the ones we expect to fail! |
| 50 | + |
| 51 | + // The spec has a bunch of optional suggestions which say that you |
| 52 | + // should use 200 rather than 400 for various errors unless opting in to |
| 53 | + // the new application/graphql-response+json response type. That's based |
| 54 | + // on the theory that "400 + application/json" might come from some |
| 55 | + // random proxy layer rather than an actual GraphQL processor and so it |
| 56 | + // shouldn't be relied on. (It *does* expect you to use 400 for these |
| 57 | + // errors when returning `application/graphql-response+json`, and we |
| 58 | + // pass those tests.) But Apollo Server has used non-200 status codes |
| 59 | + // for a long time, and in fact a major reason these are merely SHOULDs |
| 60 | + // in the spec is so that AS can pass without backwards-incompatible |
| 61 | + // changes here. So we ignore these particular SHOULD failures. |
| 62 | + if ( |
| 63 | + audit.name.startsWith('SHOULD use 200 status code') && |
| 64 | + audit.name.endsWith('when accepting application/json') && |
| 65 | + result.reason === 'Status code 400 is not 200' |
| 66 | + ) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // This is a bit weird: this issue is not actually that we include the 'data' |
| 71 | + // entry, but that JSON parse errors aren't delivered as JSON responses at all. |
| 72 | + // See https://github.com/graphql/graphql-http/issues/25 |
| 73 | + if ( |
| 74 | + audit.name === |
| 75 | + 'SHOULD not contain the data entry on JSON parsing failure when accepting application/graphql-response+json' |
| 76 | + ) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + throw new Error(result.reason); |
| 81 | + }); |
| 82 | + } |
| 83 | + }); |
| 84 | +} |
0 commit comments