mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-08 14:35:03 +00:00
feat: add support for file body mode in bruno-cli (#5427)
* feat: add support for `file` body mode in `bruno-cli` Fixes #4336 * fix: Correct await/async on file reading. * fix: update test and fix lint errors --------- Co-authored-by: William Floyd <william.floyd@modopayments.com> Co-authored-by: Bijin Bruno <bijin@usebruno.com>
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
const { get, each, filter } = require('lodash');
|
||||
const get = require('lodash/get');
|
||||
const each = require('lodash/each');
|
||||
const filter = require('lodash/filter');
|
||||
const find = require('lodash/find');
|
||||
const decomment = require('decomment');
|
||||
const crypto = require('node:crypto');
|
||||
const fs = require('node:fs/promises');
|
||||
const { mergeHeaders, mergeScripts, mergeVars, mergeAuth, getTreePathFromCollectionToItem } = require('../utils/collection');
|
||||
const { buildFormUrlEncodedPayload } = require('../utils/form-data');
|
||||
const path = require('node:path');
|
||||
|
||||
const prepareRequest = (item = {}, collection = {}) => {
|
||||
const prepareRequest = async (item = {}, collection = {}) => {
|
||||
const request = item?.request;
|
||||
const brunoConfig = get(collection, 'brunoConfig', {});
|
||||
const collectionPath = collection?.pathname;
|
||||
const headers = {};
|
||||
let contentTypeDefined = false;
|
||||
|
||||
@@ -288,6 +294,32 @@ const prepareRequest = (item = {}, collection = {}) => {
|
||||
axiosRequest.data = request.body.sparql;
|
||||
}
|
||||
|
||||
if (request.body.mode === 'file') {
|
||||
if (!contentTypeDefined) {
|
||||
axiosRequest.headers['content-type'] = 'application/octet-stream'; // Default headers for binary file uploads
|
||||
}
|
||||
|
||||
const bodyFile = find(request.body.file, param => param.selected);
|
||||
if (bodyFile) {
|
||||
let { filePath, contentType } = bodyFile;
|
||||
|
||||
axiosRequest.headers['content-type'] = contentType;
|
||||
|
||||
if (filePath) {
|
||||
if (!path.isAbsolute(filePath)) {
|
||||
filePath = path.join(collectionPath, filePath);
|
||||
}
|
||||
|
||||
try {
|
||||
const fileContent = await fs.readFile(filePath);
|
||||
axiosRequest.data = fileContent;
|
||||
} catch (error) {
|
||||
console.error('Error reading file:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (request.body.mode === 'formUrlEncoded') {
|
||||
if (!contentTypeDefined) {
|
||||
axiosRequest.headers['content-type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
@@ -72,7 +72,7 @@ const runSingleRequest = async function (
|
||||
let preRequestTestResults = [];
|
||||
let postResponseTestResults = [];
|
||||
|
||||
request = prepareRequest(item, collection);
|
||||
request = await prepareRequest(item, collection);
|
||||
|
||||
request.__bruno__executionMode = 'cli';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user