Skip to content

Commit 052f1b5

Browse files
authored
integration-testsuite: relax error expectations (#6760)
Fastify's JSON parser returns errors directly in these cases rather than letting our middleware react to the lack of parsing.
1 parent 07ec974 commit 052f1b5

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

.changeset/lazy-emus-hunt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/server-integration-testsuite": patch
3+
---
4+
5+
Relax error-handling expectations to work better with Fastify

packages/integration-testsuite/src/apolloServerTests.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,13 +2724,22 @@ export function defineIntegrationTestSuiteApolloServerTests(
27242724
expect(invalidRequestErrors).toHaveLength(0);
27252725
}
27262726

2727-
function blocked(res: Response) {
2728-
expect(res.status).toBe(400);
2729-
expect(res.text).toMatch(/This operation has been blocked/);
2730-
expect(invalidRequestErrors).toHaveLength(1);
2731-
expect(invalidRequestErrors.pop()?.message).toMatch(
2732-
/This operation has been blocked/,
2733-
);
2727+
// When Apollo Server itself blocks a request, it returns status code 400
2728+
// with a particular message. With some web frameworks, a request without
2729+
// a parsable Content-Type will make it to our middleware and get blocked
2730+
// by us; with other frameworks (eg Fastify) the framework itself will
2731+
// block it earlier in some cases. This function is thus relaxed for the
2732+
// one particular case where Fastify returns a 415 earlier; we can relax
2733+
// it further if other integrations need it.
2734+
function blocked(res: Response, statusCodes = [400]) {
2735+
expect(statusCodes).toContain(res.status);
2736+
if (res.status === 400) {
2737+
expect(res.text).toMatch(/This operation has been blocked/);
2738+
expect(invalidRequestErrors).toHaveLength(1);
2739+
expect(invalidRequestErrors.pop()?.message).toMatch(
2740+
/This operation has been blocked/,
2741+
);
2742+
}
27342743
}
27352744

27362745
it('default', async () => {
@@ -2745,7 +2754,10 @@ export function defineIntegrationTestSuiteApolloServerTests(
27452754
);
27462755

27472756
// POST without content-type is blocked.
2748-
blocked(await request(url).post('/').send(JSON.stringify(operation)));
2757+
blocked(
2758+
await request(url).post('/').send(JSON.stringify(operation)),
2759+
[400, 415],
2760+
);
27492761

27502762
// POST with text/plain is blocked.
27512763
blocked(

packages/integration-testsuite/src/httpServerTests.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,24 +272,21 @@ export function defineIntegrationTestSuiteHttpServerTests(
272272

273273
it('throws an error if POST body is empty', async () => {
274274
const app = await createApp();
275-
const req = request(app)
275+
const res = await request(app)
276276
.post('/')
277277
.type('text/plain')
278278
.set('apollo-require-preflight', 't')
279279
.send(' ');
280-
return req.then((res) => {
281-
expect(res.status).toEqual(400);
282-
expect((res.error as HTTPError).text).toMatch('POST body missing');
283-
});
280+
expect(res.status).toEqual(400);
284281
});
285282

286283
it('throws an error if POST body is missing even with content-type', async () => {
287284
const app = await createApp();
288-
const req = request(app).post('/').type('application/json').send();
289-
return req.then((res) => {
290-
expect(res.status).toEqual(400);
291-
expect((res.error as HTTPError).text).toMatch('POST body missing');
292-
});
285+
const res = await request(app)
286+
.post('/')
287+
.type('application/json')
288+
.send();
289+
expect(res.status).toEqual(400);
293290
});
294291

295292
it('throws an error if invalid content-type', async () => {

0 commit comments

Comments
 (0)