-
-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(graphql): clone request before parsing (#1714)
- Loading branch information
1 parent
5f3dd2a
commit 3b8f57e
Showing
3 changed files
with
86 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import { HttpResponse, http, graphql } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
import * as express from 'express' | ||
import { HttpServer } from '@open-draft/test-server/http' | ||
|
||
const httpServer = new HttpServer((app) => { | ||
app.post('/resource', express.json(), (req, res) => { | ||
res.json({ response: `received: ${req.body.message}` }) | ||
}) | ||
}) | ||
|
||
const server = setupServer() | ||
|
||
beforeAll(async () => { | ||
server.listen() | ||
await httpServer.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
afterAll(async () => { | ||
server.close() | ||
await httpServer.close() | ||
}) | ||
|
||
it('does not read the body while parsing an unhandled request', async () => { | ||
// Expecting an unhandled request warning in this test. | ||
jest.spyOn(console, 'warn').mockImplementation(() => {}) | ||
|
||
const requestUrl = httpServer.http.url('/resource') | ||
const response = await fetch(requestUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
message: 'Hello server', | ||
}), | ||
}) | ||
expect(await response.json()).toEqual({ response: `received: Hello server` }) | ||
}) | ||
|
||
it('does not read the body while parsing an unhandled request', async () => { | ||
const requestUrl = httpServer.http.url('/resource') | ||
server.use( | ||
http.post(requestUrl, () => { | ||
return HttpResponse.json({ mocked: true }) | ||
}), | ||
) | ||
const response = await fetch(requestUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
message: 'Hello server', | ||
}), | ||
}) | ||
expect(await response.json()).toEqual({ mocked: true }) | ||
}) |