Skip to content

Commit

Permalink
test: failing test for binary request bodies (mswjs#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Oct 10, 2022
1 parent 3e7c4c1 commit e9f2988
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
14 changes: 14 additions & 0 deletions test/rest-api/binary.mocks.ts
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()
33 changes: 33 additions & 0 deletions test/rest-api/binary.test.ts
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 })
})

0 comments on commit e9f2988

Please sign in to comment.