Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion packages/bruno-js/src/bruno-request.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { isArrayBuffer } = require('node:util').types;
const { getMediaType } = require('@usebruno/common').utils;
const HeaderList = require('./header-list');
const { parseUrl } = require('./utils/url');
Expand Down Expand Up @@ -351,14 +352,23 @@ 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 = {}) {
// Copy the bytes, as Safe Mode does, so later changes in the script don't change the body
if (ArrayBuffer.isView(data) && !Buffer.isBuffer(data)) {
data = Buffer.from(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
} else if (isArrayBuffer(data)) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// Not `instanceof ArrayBuffer`: in Developer Mode the script's ArrayBuffer comes from the VM's own realm
data = Buffer.from(new Uint8Array(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
84 changes: 84 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,90 @@ 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('sends the bytes of an ArrayBuffer created in the script when Content-Type is application/json', async () => {
const request = makeRequest({ headers: { 'content-type': 'application/json' } });

await runRequestScript(
runtime,
`const body = new ArrayBuffer(${BINARY_BYTES.length}); new Uint8Array(body).set(${JSON.stringify(BINARY_BYTES)}); req.setBody(body)`,
request
);

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

it('sends the bytes a Uint8Array held when req.setBody was called', async () => {
const request = makeRequest();

await runRequestScript(runtime, `const body = new Uint8Array([1]); req.setBody(body); body[0] = 2`, request);

expect(request.data).toStrictEqual(Buffer.from([1]));
});

it('sends the bytes an ArrayBuffer held when req.setBody was called', async () => {
const request = makeRequest();

await runRequestScript(runtime, `const body = new ArrayBuffer(1); req.setBody(body); new Uint8Array(body)[0] = 2`, request);

expect(request.data).toStrictEqual(Buffer.from([0]));
});

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