-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: failing test for binary request bodies (mswjs#929)
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { setupWorker, rest } from 'msw' | ||
|
||
const worker = setupWorker( | ||
rest.post('https://test.mswjs.io/api/binary', async (req, res, ctx) => { | ||
const body = await req.arrayBuffer() | ||
const hexNumbers = Array.from(new Uint8Array(body)) | ||
.map((byte) => byte.toString(16)) | ||
.join(' ') | ||
|
||
return res(ctx.json({ result: hexNumbers })) | ||
}), | ||
) | ||
|
||
worker.start() |
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,33 @@ | ||
import * as path from 'path' | ||
import { pageWith } from 'page-with' | ||
|
||
function createRuntime() { | ||
return pageWith({ | ||
example: path.resolve(__dirname, 'binary.mocks.ts'), | ||
}) | ||
} | ||
|
||
test('transfers binary data without corruption', async () => { | ||
const runtime = await createRuntime() | ||
|
||
const payload = [0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46] | ||
const expectedResult = payload.map((byte) => byte.toString(16)).join(' ') | ||
|
||
runtime.page.evaluate((payload) => { | ||
return fetch('https://test.mswjs.io/api/binary', { | ||
method: 'POST', | ||
body: new Uint8Array(payload).buffer, | ||
}) | ||
}, payload) | ||
|
||
const res = await runtime.page.waitForResponse( | ||
'https://test.mswjs.io/api/binary', | ||
) | ||
const status = res.status() | ||
const headers = await res.allHeaders() | ||
const body = await res.json() | ||
|
||
expect(status).toBe(200) | ||
expect(headers).toHaveProperty('x-powered-by', 'msw') | ||
expect(body).toEqual({ result: expectedResult }) | ||
}) |