It's not currently possible to provide MockInterceptor with a Uint8Array and have those same bytes returned as the response of a fetch call. This example should illustrate the issue:
import assert from 'assert/strict';
import { MockAgent, setGlobalDispatcher } from 'undici';
const mockAgent = new MockAgent();
const mockPool = mockAgent.get('https://localhost');
mockAgent.disableNetConnect();
setGlobalDispatcher(mockAgent);
const testData = new TextEncoder().encode('test');
mockPool.intercept({ path: '/' }).reply(200, testData);
const response = await fetch('https://localhost');
const body = await response.bytes();
assert.deepEqual(body, testData);
This use case should ideally be possible but now results in the following error:
This would solve...
It's not currently possible to provide
MockInterceptor
with aUint8Array
and have those same bytes returned as the response of a fetch call. This example should illustrate the issue:This use case should ideally be possible but now results in the following error:
The implementation should look like...
In addition to checking for Buffers here, Undici should also check for a valid
Symbol.toStringTag
ofUint8Array
.I have also considered...
Converting my Uint8Arrays to Buffers.
Additional context
I'd be happy to put up a PR with tests!