Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/bruno-js/src/bruno-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,21 @@ class BrunoRequest {
* Replace the request body.
*
* With a JSON content type, an object is serialized to JSON. Pass `{ raw: true }` to send `data`
* exactly as given. A `Buffer` is always sent as-is.
* exactly as given. A `Buffer`, typed array (such as `Uint8Array`), `DataView` or `ArrayBuffer`
* is always sent as its bytes.
* @param {any} data - The new body.
* @param {{ raw?: boolean }} [options] - `raw: true` skips JSON serialization.
* @example
* req.setBody({ ...req.getBody(), requestedAt: Date.now() });
* @category Body
*/
setBody(data, options = {}) {
if (ArrayBuffer.isView(data) && !Buffer.isBuffer(data)) {
data = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
} else if (data instanceof ArrayBuffer) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
data = Buffer.from(data);
}

if (options.raw || Buffer.isBuffer(data)) {
this.req.data = data;
this.body = data;
Expand Down
10 changes: 7 additions & 3 deletions packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const addBrunoRequestShimToContext = (vm, req) => {
vm.setProp(reqObject, 'getBody', getBody);
getBody.dispose();

// Wrapped by req.setBody below, which sends Buffers to _setBinaryBody instead: vm.dump turns a Buffer into { type: 'Buffer', data: [...] }
// Wrapped by req.setBody below, which sends Buffers, typed arrays, DataViews and ArrayBuffers to _setBinaryBody instead: vm.dump turns them into plain objects such as { type: 'Buffer', data: [...] } or { "0": 1, "1": 2 }
let setBody = vm.newFunction('_setBody', function (data, options) {
req.setBody(vm.dump(data), vm.dump(options));
});
Expand Down Expand Up @@ -210,13 +210,17 @@ const addBrunoRequestShimToContext = (vm, req) => {
vm.evalCode(`{ ${headersEvalCode} }`);
}

// Buffer is only defined once the bundled libraries load, which expression evaluation skips
// ArrayBuffer.isView also covers the bundled Buffer, which is a Uint8Array subclass
vm.evalCode(`
globalThis.req.setBody = (data, options) => {
if (globalThis.Buffer?.isBuffer(data)) {
if (ArrayBuffer.isView(data)) {
globalThis.req._setBinaryBody(data.buffer, data.byteOffset, data.byteLength, options);
return;
}
if (data instanceof ArrayBuffer) {
globalThis.req._setBinaryBody(data, 0, data.byteLength, options);
return;
}
globalThis.req._setBody(data, options);
};
`);
Expand Down
56 changes: 56 additions & 0 deletions packages/bruno-js/tests/script-runtime-request-body.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,62 @@ describe.each(['nodevm', 'quickjs'])('req.setBody() in a pre-request script on %
expect(request.data).toStrictEqual(Buffer.alloc(0));
});

it('sends a Uint8Array body as its bytes', async () => {
const request = makeRequest();

await runRequestScript(runtime, `req.setBody(new Uint8Array(${JSON.stringify(BINARY_BYTES)}))`, request);

expect(request.data).toStrictEqual(Buffer.from(BINARY_BYTES));
});

it('sends a Uint8Array body as its bytes when Content-Type is application/json', async () => {
const request = makeRequest({ headers: { 'content-type': 'application/json' } });

await runRequestScript(runtime, `req.setBody(new Uint8Array(${JSON.stringify(BINARY_BYTES)}))`, request);

expect(request.data).toStrictEqual(Buffer.from(BINARY_BYTES));
});

it('sends a Uint8Array body set with { raw: true } as its bytes', async () => {
const request = makeRequest();

await runRequestScript(runtime, `req.setBody(new Uint8Array(${JSON.stringify(BINARY_BYTES)}), { raw: true })`, request);

expect(request.data).toStrictEqual(Buffer.from(BINARY_BYTES));
});

it('sends only the bytes inside a Uint8Array view', async () => {
const request = makeRequest();

await runRequestScript(runtime, `req.setBody(new Uint8Array([9, 9, 112, 97, 114, 116, 9, 9]).subarray(2, 6))`, request);

expect(request.data).toStrictEqual(Buffer.from('part'));
});

it('sends the bytes of a DataView body', async () => {
const request = makeRequest();

await runRequestScript(runtime, `req.setBody(new DataView(new Uint8Array(${JSON.stringify(BINARY_BYTES)}).buffer))`, request);

expect(request.data).toStrictEqual(Buffer.from(BINARY_BYTES));
});

it('sends the bytes of an ArrayBuffer body', async () => {
const request = makeRequest();

await runRequestScript(runtime, `req.setBody(new Uint8Array(${JSON.stringify(BINARY_BYTES)}).buffer)`, request);

expect(request.data).toStrictEqual(Buffer.from(BINARY_BYTES));
});

it('sends the bytes of an ArrayBuffer body when Content-Type is application/json', async () => {
const request = makeRequest({ headers: { 'content-type': 'application/json' } });

await runRequestScript(runtime, `req.setBody(new Uint8Array(${JSON.stringify(BINARY_BYTES)}).buffer)`, request);

expect(request.data).toStrictEqual(Buffer.from(BINARY_BYTES));
});

it('keeps a string body unchanged', async () => {
const request = makeRequest();

Expand Down
3 changes: 2 additions & 1 deletion packages/bruno-js/types/script-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,8 @@ export interface Request {
* Replace the request body.
*
* With a JSON content type, an object is serialized to JSON. Pass `{ raw: true }` to send `data`
* exactly as given. A `Buffer` is always sent as-is.
* exactly as given. A `Buffer`, typed array (such as `Uint8Array`), `DataView` or `ArrayBuffer`
* is always sent as its bytes.
*
* @param data - The new body.
* @param options - `raw: true` skips JSON serialization.
Expand Down