From c9b2aa77608b8fec025e5fe9d303aab7f5569158 Mon Sep 17 00:00:00 2001 From: Sanjai Kumar Date: Tue, 4 Feb 2025 17:41:24 +0530 Subject: [PATCH] refactor: rename binaryFile to file and update related references --- .../{Binary => FileBody}/StyledWrapper.js | 0 .../RequestPane/{Binary => FileBody}/index.js | 14 ++--- .../RequestBody/RequestBodyMode/index.js | 2 +- .../RequestPane/RequestBody/index.js | 8 +-- .../ReduxStore/slices/collections/actions.js | 2 +- .../ReduxStore/slices/collections/index.js | 32 +++++------ .../bruno-app/src/utils/codegenerator/har.js | 7 +-- .../bruno-app/src/utils/collections/export.js | 2 +- .../bruno-app/src/utils/collections/index.js | 12 ++-- .../src/utils/curl/curl-to-json.spec.js | 2 +- packages/bruno-app/src/utils/curl/index.js | 6 +- .../bruno-app/src/utils/importers/common.js | 2 +- .../bruno-electron/src/ipc/network/index.js | 6 +- .../src/ipc/network/prepare-request.js | 8 +-- .../bruno-electron/src/utils/collection.js | 4 +- .../bruno-electron/src/utils/filesystem.js | 16 ++---- packages/bruno-lang/v2/src/bruToJson.js | 14 ++--- packages/bruno-lang/v2/src/jsonToBru.js | 10 ++-- .../bruno-lang/v2/tests/fixtures/request.bru | 2 +- .../bruno-lang/v2/tests/fixtures/request.json | 2 +- .../bruno-schema/src/collections/index.js | 6 +- .../binaryFile/binary-file-types.bru | 27 --------- .../collection/binaryFile/binary-file.json | 9 --- .../echo/echo file body/echo file body.bru | 40 ++++++++++++++ .../echo file body/echo image file body.bru | 22 ++++++++ .../echo file body/echo json file body.bru | 55 +++++++++++++++++++ .../echo file body/echo text file body.bru | 30 ++++++++++ .../collection/echo/multiline/echo binary.bru | 4 +- packages/bruno-tests/collection/file.txt | 3 + 29 files changed, 226 insertions(+), 121 deletions(-) rename packages/bruno-app/src/components/RequestPane/{Binary => FileBody}/StyledWrapper.js (100%) rename packages/bruno-app/src/components/RequestPane/{Binary => FileBody}/index.js (93%) delete mode 100644 packages/bruno-tests/collection/binaryFile/binary-file-types.bru delete mode 100644 packages/bruno-tests/collection/binaryFile/binary-file.json create mode 100644 packages/bruno-tests/collection/echo/echo file body/echo file body.bru create mode 100644 packages/bruno-tests/collection/echo/echo file body/echo image file body.bru create mode 100644 packages/bruno-tests/collection/echo/echo file body/echo json file body.bru create mode 100644 packages/bruno-tests/collection/echo/echo file body/echo text file body.bru create mode 100644 packages/bruno-tests/collection/file.txt diff --git a/packages/bruno-app/src/components/RequestPane/Binary/StyledWrapper.js b/packages/bruno-app/src/components/RequestPane/FileBody/StyledWrapper.js similarity index 100% rename from packages/bruno-app/src/components/RequestPane/Binary/StyledWrapper.js rename to packages/bruno-app/src/components/RequestPane/FileBody/StyledWrapper.js diff --git a/packages/bruno-app/src/components/RequestPane/Binary/index.js b/packages/bruno-app/src/components/RequestPane/FileBody/index.js similarity index 93% rename from packages/bruno-app/src/components/RequestPane/Binary/index.js rename to packages/bruno-app/src/components/RequestPane/FileBody/index.js index f4a6eb007..d97953aa5 100644 --- a/packages/bruno-app/src/components/RequestPane/Binary/index.js +++ b/packages/bruno-app/src/components/RequestPane/FileBody/index.js @@ -3,22 +3,22 @@ import { get, cloneDeep, isArray } from 'lodash'; import { IconTrash } from '@tabler/icons'; import { useDispatch } from 'react-redux'; import { useTheme } from 'providers/Theme'; -import { addBinaryFile, updateBinaryFile, deleteBinaryFile } from 'providers/ReduxStore/slices/collections/index'; +import { addFile as _addFile, updateFile, deleteFile } from 'providers/ReduxStore/slices/collections/index'; import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions'; import StyledWrapper from './StyledWrapper'; import FilePickerEditor from 'components/FilePickerEditor/index'; import SingleLineEditor from 'components/SingleLineEditor/index'; -const Binary = ({ item, collection }) => { +const FileBody = ({ item, collection }) => { const dispatch = useDispatch(); const { storedTheme } = useTheme(); - const params = item.draft ? get(item, 'draft.request.body.binaryFile') : get(item, 'request.body.binaryFile'); + const params = item.draft ? get(item, 'draft.request.body.file') : get(item, 'request.body.file'); const [enabledFileUid, setEnableFileUid] = useState(params && params.length ? params[0].uid : ''); const addFile = () => { dispatch( - addBinaryFile({ + _addFile({ itemUid: item.uid, collectionUid: collection.uid, }) @@ -47,7 +47,7 @@ const Binary = ({ item, collection }) => { } } dispatch( - updateBinaryFile({ + updateFile({ param: param, itemUid: item.uid, collectionUid: collection.uid @@ -57,7 +57,7 @@ const Binary = ({ item, collection }) => { const handleRemoveParams = (param) => { dispatch( - deleteBinaryFile({ + deleteFile({ paramUid: param.uid, itemUid: item.uid, collectionUid: collection.uid @@ -161,4 +161,4 @@ const Binary = ({ item, collection }) => { ); }; -export default Binary; \ No newline at end of file +export default FileBody; \ No newline at end of file diff --git a/packages/bruno-app/src/components/RequestPane/RequestBody/RequestBodyMode/index.js b/packages/bruno-app/src/components/RequestPane/RequestBody/RequestBodyMode/index.js index 6e5d575c4..db73597df 100644 --- a/packages/bruno-app/src/components/RequestPane/RequestBody/RequestBodyMode/index.js +++ b/packages/bruno-app/src/components/RequestPane/RequestBody/RequestBodyMode/index.js @@ -132,7 +132,7 @@ const RequestBodyMode = ({ item, collection }) => { className="dropdown-item" onClick={() => { dropdownTippyRef.current.hide(); - onModeChange('binaryFile'); + onModeChange('file'); }} > File / Binary diff --git a/packages/bruno-app/src/components/RequestPane/RequestBody/index.js b/packages/bruno-app/src/components/RequestPane/RequestBody/index.js index 9a71a4ac3..8f7fa8465 100644 --- a/packages/bruno-app/src/components/RequestPane/RequestBody/index.js +++ b/packages/bruno-app/src/components/RequestPane/RequestBody/index.js @@ -8,7 +8,7 @@ import { useTheme } from 'providers/Theme'; import { updateRequestBody } from 'providers/ReduxStore/slices/collections'; import { sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections/actions'; import StyledWrapper from './StyledWrapper'; -import Binary from '../Binary/index'; +import FileBody from '../FileBody/index'; const RequestBody = ({ item, collection }) => { const dispatch = useDispatch(); @@ -63,8 +63,8 @@ const RequestBody = ({ item, collection }) => { ); } - if (bodyMode === 'binaryFile') { - return + if (bodyMode === 'file') { + return } if (bodyMode === 'formUrlEncoded') { @@ -77,4 +77,4 @@ const RequestBody = ({ item, collection }) => { return No Body; }; -export default RequestBody; +export default RequestBody; \ No newline at end of file diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js index 880536019..d251112c7 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js @@ -759,7 +759,7 @@ export const newHttpRequest = (params) => (dispatch, getState) => { sparql: null, multipartForm: null, formUrlEncoded: null, - binaryFile: null + file: null }, auth: auth ?? { mode: 'none' diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js index 25bf4fa68..3ae0fa4e5 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js @@ -897,7 +897,7 @@ export const collectionsSlice = createSlice({ } } }, - addBinaryFile: (state, action) => { + addFile: (state, action) => { const collection = findCollectionByUid(state.collections, action.payload.collectionUid); if (collection) { @@ -907,9 +907,9 @@ export const collectionsSlice = createSlice({ if (!item.draft) { item.draft = cloneDeep(item); } - item.draft.request.body.binaryFile = item.draft.request.body.binaryFile || []; + item.draft.request.body.file = item.draft.request.body.file || []; - item.draft.request.body.binaryFile.push({ + item.draft.request.body.file.push({ uid: uuid(), filePath: '', contentType: '', @@ -918,7 +918,7 @@ export const collectionsSlice = createSlice({ } } }, - updateBinaryFile: (state, action) => { + updateFile: (state, action) => { const collection = findCollectionByUid(state.collections, action.payload.collectionUid); if (collection) { @@ -929,7 +929,7 @@ export const collectionsSlice = createSlice({ item.draft = cloneDeep(item); } - const param = find(item.draft.request.body.binaryFile, (p) => p.uid === action.payload.param.uid); + const param = find(item.draft.request.body.file, (p) => p.uid === action.payload.param.uid); if (param) { const contentType = mime.contentType(path.extname(action.payload.param.filePath)); @@ -937,7 +937,7 @@ export const collectionsSlice = createSlice({ param.contentType = action.payload.param.contentType || contentType || ''; param.selected = action.payload.param.selected; - item.draft.request.body.binaryFile = item.draft.request.body.binaryFile.map((p) => { + item.draft.request.body.file = item.draft.request.body.file.map((p) => { p.selected = p.uid === param.uid; return p; }); @@ -945,7 +945,7 @@ export const collectionsSlice = createSlice({ } } }, - deleteBinaryFile: (state, action) => { + deleteFile: (state, action) => { const collection = findCollectionByUid(state.collections, action.payload.collectionUid); if (collection) { @@ -956,13 +956,13 @@ export const collectionsSlice = createSlice({ item.draft = cloneDeep(item); } - item.draft.request.body.binaryFile = filter( - item.draft.request.body.binaryFile, + item.draft.request.body.file = filter( + item.draft.request.body.file, (p) => p.uid !== action.payload.paramUid ); - if (item.draft.request.body.binaryFile.length > 0) { - item.draft.request.body.binaryFile[0].selected = true; + if (item.draft.request.body.file.length > 0) { + item.draft.request.body.file[0].selected = true; } } } @@ -1023,8 +1023,8 @@ export const collectionsSlice = createSlice({ item.draft.request.body.sparql = action.payload.content; break; } - case 'binaryFile': { - item.draft.request.body.binaryFile = action.payload.content; + case 'file': { + item.draft.request.body.file = action.payload.content; break; } case 'formUrlEncoded': { @@ -2028,9 +2028,9 @@ export const { addMultipartFormParam, updateMultipartFormParam, deleteMultipartFormParam, - addBinaryFile, - updateBinaryFile, - deleteBinaryFile, + addFile, + updateFile, + deleteFile, moveMultipartFormParam, updateRequestAuthMode, updateRequestBodyMode, diff --git a/packages/bruno-app/src/utils/codegenerator/har.js b/packages/bruno-app/src/utils/codegenerator/har.js index 8514588ab..110f82db5 100644 --- a/packages/bruno-app/src/utils/codegenerator/har.js +++ b/packages/bruno-app/src/utils/codegenerator/har.js @@ -14,7 +14,7 @@ const createContentType = (mode) => { return 'application/json'; case 'multipartForm': return 'multipart/form-data'; - case 'binaryFile': + case 'file': return 'application/octet-stream'; default: return ''; @@ -93,8 +93,8 @@ const createPostData = (body, type) => { ...(param.type === 'file' && { fileName: param.value }) })) }; - case 'binaryFile': - const binary = { + case 'file': + return { mimeType: body[body.mode].filter((param) => param.enabled)[0].contentType, params: body[body.mode] .filter((param) => param.selected) @@ -102,7 +102,6 @@ const createPostData = (body, type) => { value: param.filePath, })) }; - return binary; default: return { mimeType: contentType, diff --git a/packages/bruno-app/src/utils/collections/export.js b/packages/bruno-app/src/utils/collections/export.js index b7ca9f5ba..3d15fdd07 100644 --- a/packages/bruno-app/src/utils/collections/export.js +++ b/packages/bruno-app/src/utils/collections/export.js @@ -14,7 +14,7 @@ export const deleteUidsInItems = (items) => { each(get(item, 'request.vars.assertions'), (a) => delete a.uid); each(get(item, 'request.body.multipartForm'), (param) => delete param.uid); each(get(item, 'request.body.formUrlEncoded'), (param) => delete param.uid); - each(get(item, 'request.body.binaryFile'), (param) => delete param.uid); + each(get(item, 'request.body.file'), (param) => delete param.uid); } if (item.items && item.items.length) { diff --git a/packages/bruno-app/src/utils/collections/index.js b/packages/bruno-app/src/utils/collections/index.js index fa4a2acd4..eb53cfb48 100644 --- a/packages/bruno-app/src/utils/collections/index.js +++ b/packages/bruno-app/src/utils/collections/index.js @@ -281,7 +281,7 @@ export const transformCollectionToSaveToExportAsFile = (collection, options = {} }); }; - const copyBinaryFileParams = (params = []) => { + const copyFileParams = (params = []) => { return map(params, (param) => { return { uid: param.uid, @@ -320,7 +320,7 @@ export const transformCollectionToSaveToExportAsFile = (collection, options = {} sparql: si.request.body.sparql, formUrlEncoded: copyFormUrlEncodedParams(si.request.body.formUrlEncoded), multipartForm: copyMultipartFormParams(si.request.body.multipartForm), - binaryFile: copyBinaryFileParams(si.request.body.binaryFile) + file: copyFileParams(si.request.body.file) }, script: si.request.script, vars: si.request.vars, @@ -673,7 +673,7 @@ export const humanizeRequestBodyMode = (mode) => { label = 'SPARQL'; break; } - case 'binaryFile': { + case 'file': { label = 'File / Binary'; break; } @@ -777,7 +777,7 @@ export const refreshUidsInItem = (item) => { each(get(item, 'request.params'), (param) => (param.uid = uuid())); each(get(item, 'request.body.multipartForm'), (param) => (param.uid = uuid())); each(get(item, 'request.body.formUrlEncoded'), (param) => (param.uid = uuid())); - each(get(item, 'request.body.binaryFile'), (param) => (param.uid = uuid())); + each(get(item, 'request.body.file'), (param) => (param.uid = uuid())); return item; }; @@ -788,13 +788,13 @@ export const deleteUidsInItem = (item) => { const headers = get(item, 'request.headers', []); const bodyFormUrlEncoded = get(item, 'request.body.formUrlEncoded', []); const bodyMultipartForm = get(item, 'request.body.multipartForm', []); - const binaryFile = get(item, 'request.body.binaryFile', []); + const file = get(item, 'request.body.file', []); params.forEach((param) => delete param.uid); headers.forEach((header) => delete header.uid); bodyFormUrlEncoded.forEach((param) => delete param.uid); bodyMultipartForm.forEach((param) => delete param.uid); - binaryFile.forEach((param) => delete param.uid); + file.forEach((param) => delete param.uid); return item; }; diff --git a/packages/bruno-app/src/utils/curl/curl-to-json.spec.js b/packages/bruno-app/src/utils/curl/curl-to-json.spec.js index 6f8206139..4c3194c2e 100644 --- a/packages/bruno-app/src/utils/curl/curl-to-json.spec.js +++ b/packages/bruno-app/src/utils/curl/curl-to-json.spec.js @@ -117,7 +117,7 @@ describe('curlToJson', () => { value: ['/path/to/file'], enabled: true, contentType: 'application/json;charset=utf-8', - type: 'binaryFile' + type: 'file' } ] }); diff --git a/packages/bruno-app/src/utils/curl/index.js b/packages/bruno-app/src/utils/curl/index.js index d91588178..ad4f1edf6 100644 --- a/packages/bruno-app/src/utils/curl/index.js +++ b/packages/bruno-app/src/utils/curl/index.js @@ -51,7 +51,7 @@ export const getRequestFromCurlCommand = (curlCommand, requestType = 'http-reque multipartForm: null, formUrlEncoded: null, graphql: null, - binaryFile: null + file: null }; if (parsedBody && contentType && typeof contentType === 'string') { @@ -59,8 +59,8 @@ export const getRequestFromCurlCommand = (curlCommand, requestType = 'http-reque body.mode = 'graphql'; body.graphql = parseGraphQL(parsedBody); } else if (requestType === 'http-request' && request.isDataBinary) { - body.mode = 'binaryFile'; - body.binaryFile = parsedBody; + body.mode = 'file'; + body.file = parsedBody; }else if (contentType.includes('application/json')) { body.mode = 'json'; body.json = convertToCodeMirrorJson(parsedBody); diff --git a/packages/bruno-app/src/utils/importers/common.js b/packages/bruno-app/src/utils/importers/common.js index af187cc82..9d370a455 100644 --- a/packages/bruno-app/src/utils/importers/common.js +++ b/packages/bruno-app/src/utils/importers/common.js @@ -35,7 +35,7 @@ export const updateUidsInCollection = (_collection) => { each(get(item, 'request.assertions'), (a) => (a.uid = uuid())); each(get(item, 'request.body.multipartForm'), (param) => (param.uid = uuid())); each(get(item, 'request.body.formUrlEncoded'), (param) => (param.uid = uuid())); - each(get(item, 'request.body.binaryFile'), (param) => (param.uid = uuid())); + each(get(item, 'request.body.file'), (param) => (param.uid = uuid())); if (item.items && item.items.length) { updateItemUids(item.items); diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index baa9f098b..427b3ab65 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -28,7 +28,7 @@ const { makeAxiosInstance } = require('./axios-instance'); const { addAwsV4Interceptor, resolveAwsV4Credentials } = require('./awsv4auth-helper'); const { addDigestInterceptor } = require('./digestauth-helper'); const { shouldUseProxy, PatchedHttpsProxyAgent } = require('../../utils/proxy-util'); -const { chooseFileToSave, writeBinaryFile, writeFile } = require('../../utils/filesystem'); +const { chooseFileToSave, writeFile } = require('../../utils/filesystem'); const { getCookieStringForUrl, addCookieToJar, getDomainsWithCookies } = require('../../utils/cookies'); const { resolveOAuth2AuthorizationCodeAccessToken, @@ -614,7 +614,7 @@ const registerNetworkIpc = (mainWindow) => { url: request.url, method: request.method, headers: request.headers, - data: request.mode == 'binaryFile'? "": safeParseJSON(safeStringifyJSON(request.data)) , + data: request.mode == 'file'? "": safeParseJSON(safeStringifyJSON(request.data)) , timestamp: Date.now() }, collectionUid, @@ -1371,7 +1371,7 @@ const registerNetworkIpc = (mainWindow) => { if (encoding === 'utf-8') { await writeFile(filePath, data); } else { - await writeBinaryFile(filePath, data); + await writeFile(filePath, data, true); } } } catch (error) { diff --git a/packages/bruno-electron/src/ipc/network/prepare-request.js b/packages/bruno-electron/src/ipc/network/prepare-request.js index 70d8017d1..705d19f22 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-request.js @@ -253,14 +253,14 @@ const prepareRequest = async (item, collection, abortController) => { axiosRequest.data = request.body.sparql; } - if (request.body.mode === 'binaryFile') { + if (request.body.mode === 'file') { if (!contentTypeDefined) { axiosRequest.headers['content-type'] = 'application/octet-stream'; // Default headers for binary file uploads } - const binaryFile = find(request.body.binaryFile, (param) => param.selected); - if (binaryFile) { - let { filePath, contentType } = binaryFile; + const bodyFile = find(request.body.file, (param) => param.selected); + if (bodyFile) { + let { filePath, contentType } = bodyFile; axiosRequest.headers['content-type'] = contentType; if (filePath) { diff --git a/packages/bruno-electron/src/utils/collection.js b/packages/bruno-electron/src/utils/collection.js index 46624677c..bb8d17e97 100644 --- a/packages/bruno-electron/src/utils/collection.js +++ b/packages/bruno-electron/src/utils/collection.js @@ -240,7 +240,7 @@ const hydrateRequestWithUuid = (request, pathname) => { const assertions = get(request, 'request.assertions', []); const bodyFormUrlEncoded = get(request, 'request.body.formUrlEncoded', []); const bodyMultipartForm = get(request, 'request.body.multipartForm', []); - const binaryFile = get(request, 'request.body.binaryFile', []); + const file = get(request, 'request.body.file', []); params.forEach((param) => (param.uid = uuid())); headers.forEach((header) => (header.uid = uuid())); @@ -249,7 +249,7 @@ const hydrateRequestWithUuid = (request, pathname) => { assertions.forEach((assertion) => (assertion.uid = uuid())); bodyFormUrlEncoded.forEach((param) => (param.uid = uuid())); bodyMultipartForm.forEach((param) => (param.uid = uuid())); - binaryFile.forEach((param) => (param.uid = uuid())); + file.forEach((param) => (param.uid = uuid())); return request; }; diff --git a/packages/bruno-electron/src/utils/filesystem.js b/packages/bruno-electron/src/utils/filesystem.js index d37742be4..16a07b6af 100644 --- a/packages/bruno-electron/src/utils/filesystem.js +++ b/packages/bruno-electron/src/utils/filesystem.js @@ -68,20 +68,13 @@ function normalizeWslPath(pathname) { return pathname.replace(/^\/wsl.localhost/, '\\\\wsl.localhost').replace(/\//g, '\\'); } -const writeFile = async (pathname, content) => { +const writeFile = async (pathname, content, isBinary = false) => { try { - fs.writeFileSync(pathname, content, { - encoding: 'utf8' + await fs.writeFile(pathname, content, { + encoding: !isBinary ? "utf-8" : null }); } catch (err) { - return Promise.reject(err); - } -}; - -const writeBinaryFile = async (pathname, content) => { - try { - fs.writeFileSync(pathname, content); - } catch (err) { + console.error(`Error writing file at ${pathname}:`, err); return Promise.reject(err); } }; @@ -265,7 +258,6 @@ module.exports = { isWSLPath, normalizeWslPath, writeFile, - writeBinaryFile, hasJsonExtension, hasBruExtension, createDirectory, diff --git a/packages/bruno-lang/v2/src/bruToJson.js b/packages/bruno-lang/v2/src/bruToJson.js index 3b228036f..26a670681 100644 --- a/packages/bruno-lang/v2/src/bruToJson.js +++ b/packages/bruno-lang/v2/src/bruToJson.js @@ -25,7 +25,7 @@ const grammar = ohm.grammar(`Bru { BruFile = (meta | http | query | params | headers | auths | bodies | varsandassert | script | tests | docs)* auths = authawsv4 | authbasic | authbearer | authdigest | authNTLM | authOAuth2 | authwsse | authapikey bodies = bodyjson | bodytext | bodyxml | bodysparql | bodygraphql | bodygraphqlvars | bodyforms | body - bodyforms = bodyformurlencoded | bodymultipart | bodybinaryfile + bodyforms = bodyformurlencoded | bodymultipart | bodyfile params = paramspath | paramsquery nl = "\\r"? "\\n" @@ -102,7 +102,7 @@ const grammar = ohm.grammar(`Bru { bodyformurlencoded = "body:form-urlencoded" dictionary bodymultipart = "body:multipart-form" dictionary - bodybinaryfile = "body:binary-file" dictionary + bodyfile = "body:file" dictionary script = scriptreq | scriptres scriptreq = "script:pre-request" st* "{" nl* textblock tagend @@ -174,7 +174,7 @@ const multipartExtractContentType = (pair) => { } }; -const binaryFileExtractContentType = (pair) => { +const fileExtractContentType = (pair) => { if (_.isString(pair.value)) { const match = pair.value.match(/^(.*?)\s*@contentType\((.*?)\)\s*$/); if (match && match.length > 2) { @@ -204,10 +204,10 @@ const mapPairListToKeyValPairsMultipart = (pairList = [], parseEnabled = true) = }); }; -const mapPairListToKeyValPairsBinaryFile = (pairList = [], parseEnabled = true) => { +const mapPairListToKeyValPairsFile = (pairList = [], parseEnabled = true) => { const pairs = mapPairListToKeyValPairs(pairList, parseEnabled); return pairs.map((pair) => { - binaryFileExtractContentType(pair); + fileExtractContentType(pair); if (pair.value.startsWith('@file(') && pair.value.endsWith(')')) { let filePath = pair.value.replace(/^@file\(/, '').replace(/\)$/, ''); @@ -609,10 +609,10 @@ const sem = grammar.createSemantics().addAttribute('ast', { } }; }, - bodybinaryfile(_1, dictionary) { + bodyfile(_1, dictionary) { return { body: { - binaryFile: mapPairListToKeyValPairsBinaryFile(dictionary.ast) + file: mapPairListToKeyValPairsFile(dictionary.ast) } }; }, diff --git a/packages/bruno-lang/v2/src/jsonToBru.js b/packages/bruno-lang/v2/src/jsonToBru.js index 164ea6a35..0d0b1d9c2 100644 --- a/packages/bruno-lang/v2/src/jsonToBru.js +++ b/packages/bruno-lang/v2/src/jsonToBru.js @@ -314,13 +314,13 @@ ${indentString(body.sparql)} } - if (body && body.binaryFile && body.binaryFile.length) { - bru += `body:binary-file {`; - const binaryFiles = enabled(body.binaryFile, "selected").concat(disabled(body.binaryFile, "selected")); + if (body && body.file && body.file.length) { + bru += `body:file {`; + const files = enabled(body.file, "selected").concat(disabled(body.file, "selected")); - if (binaryFiles.length) { + if (files.length) { bru += `\n${indentString( - binaryFiles + files .map((item) => { const selected = item.selected ? '' : '~'; const contentType = diff --git a/packages/bruno-lang/v2/tests/fixtures/request.bru b/packages/bruno-lang/v2/tests/fixtures/request.bru index 59f37ac89..ad66c64e8 100644 --- a/packages/bruno-lang/v2/tests/fixtures/request.bru +++ b/packages/bruno-lang/v2/tests/fixtures/request.bru @@ -102,7 +102,7 @@ body:multipart-form { ~message: hello } -body:binary-file { +body:file { file: @file(path/to/file.json) @contentType(application/json) file: @file(path/to/file.json) @contentType(application/json) ~file: @file(path/to/file2.json) @contentType(application/json) diff --git a/packages/bruno-lang/v2/tests/fixtures/request.json b/packages/bruno-lang/v2/tests/fixtures/request.json index 166040509..1cfe98809 100644 --- a/packages/bruno-lang/v2/tests/fixtures/request.json +++ b/packages/bruno-lang/v2/tests/fixtures/request.json @@ -138,7 +138,7 @@ "type": "text" } ], - "binaryFile" : [ + "file" : [ { "filePath": "path/to/file.json", "contentType": "application/json", diff --git a/packages/bruno-schema/src/collections/index.js b/packages/bruno-schema/src/collections/index.js index fdf88b38c..e0d05d167 100644 --- a/packages/bruno-schema/src/collections/index.js +++ b/packages/bruno-schema/src/collections/index.js @@ -75,7 +75,7 @@ const multipartFormSchema = Yup.object({ .strict(); -const binaryFileSchema = Yup.object({ +const fileSchema = Yup.object({ uid: uidSchema, filePath: Yup.string().nullable(), contentType: Yup.string().nullable(), @@ -86,7 +86,7 @@ const binaryFileSchema = Yup.object({ const requestBodySchema = Yup.object({ mode: Yup.string() - .oneOf(['none', 'json', 'text', 'xml', 'formUrlEncoded', 'multipartForm', 'graphql', 'sparql', 'binaryFile']) + .oneOf(['none', 'json', 'text', 'xml', 'formUrlEncoded', 'multipartForm', 'graphql', 'sparql', 'file']) .required('mode is required'), json: Yup.string().nullable(), text: Yup.string().nullable(), @@ -95,7 +95,7 @@ const requestBodySchema = Yup.object({ formUrlEncoded: Yup.array().of(keyValueSchema).nullable(), multipartForm: Yup.array().of(multipartFormSchema).nullable(), graphql: graphqlBodySchema.nullable(), - binaryFile: Yup.array().of(binaryFileSchema).nullable() + file: Yup.array().of(fileSchema).nullable() }) .noUnknown(true) .strict(); diff --git a/packages/bruno-tests/collection/binaryFile/binary-file-types.bru b/packages/bruno-tests/collection/binaryFile/binary-file-types.bru deleted file mode 100644 index 93275971f..000000000 --- a/packages/bruno-tests/collection/binaryFile/binary-file-types.bru +++ /dev/null @@ -1,27 +0,0 @@ -meta { - name: binary-files-types - type: http - seq: 1 -} - -post { - url: {{host}}/api/binaryFile/binary-file-types - body: binaryFile - auth: none -} - -body:binary-file { - file1: @file() @contentType() - file2: @file(binaryFile/binary-file.json) @contentType() - file3: @file(binaryFile/binary-file.json) @contentType(application/json) -} - -assert { - res.status: eq 200 - res.body.find(p=>p.name === 'file1').value[0]: isUndefined - res.body.find(p=>p.name === 'file1').contentType: isUndefined - res.body.find(p=>p.name === 'file2').value[0]: eq binaryFile/binary-file.json - res.body.find(p=>p.name === 'file2').contentType: eq isUndefined - res.body.find(p=>p.name === 'file3').value[0]: eq binaryFile/binary-file.json - res.body.find(p=>p.name === 'file3').contentType: eq application/json -} diff --git a/packages/bruno-tests/collection/binaryFile/binary-file.json b/packages/bruno-tests/collection/binaryFile/binary-file.json deleted file mode 100644 index 2ff269bff..000000000 --- a/packages/bruno-tests/collection/binaryFile/binary-file.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "version": "1", - "name": "bruno-testing", - "type": "collection", - "ignore": [ - "node_modules", - ".git" - ] -} \ No newline at end of file diff --git a/packages/bruno-tests/collection/echo/echo file body/echo file body.bru b/packages/bruno-tests/collection/echo/echo file body/echo file body.bru new file mode 100644 index 000000000..b820e4808 --- /dev/null +++ b/packages/bruno-tests/collection/echo/echo file body/echo file body.bru @@ -0,0 +1,40 @@ +meta { + name: echo file body + type: http + seq: 4 +} + +post { + url: {{echo-host}} + body: file + auth: none +} + +body:file { + file: @file(ping.bru) @contentType(bru) +} + +tests { + test("should return bru file body contents", function() { + const data = res.getBody(); + const expectedData = `meta { + name: ping + type: http + seq: 1 + } + + get { + url: {{host}}/ping + body: none + auth: none + } + `; + expect(res.getBody()).to.eql(expectedData); + }); + + + test("should return proper header", function() { + const contentType = res.getHeader('content-type'); + expect(contentType).to.eql('bru'); + }); +} \ No newline at end of file diff --git a/packages/bruno-tests/collection/echo/echo file body/echo image file body.bru b/packages/bruno-tests/collection/echo/echo file body/echo image file body.bru new file mode 100644 index 000000000..da1899a96 --- /dev/null +++ b/packages/bruno-tests/collection/echo/echo file body/echo image file body.bru @@ -0,0 +1,22 @@ +meta { + name: echo image body + type: http + seq: 1 +} + +post { + url: {{echo-host}} + body: file + auth: none +} + +body:file { + file: @file(bruno.png) @contentType(image/png) +} + +tests { + test("should return proper header", function() { + const contentType = res.getHeader('content-type'); + expect(contentType).to.eql('image/png'); + }); +} diff --git a/packages/bruno-tests/collection/echo/echo file body/echo json file body.bru b/packages/bruno-tests/collection/echo/echo file body/echo json file body.bru new file mode 100644 index 000000000..f2173a4b9 --- /dev/null +++ b/packages/bruno-tests/collection/echo/echo file body/echo json file body.bru @@ -0,0 +1,55 @@ +meta { + name: echo json file body + type: http + seq: 2 +} + +post { + url: {{echo-host}} + body: file + auth: none +} + +body:file { + file: @file(file.json) @contentType(application/json; charset=utf-8) +} + +tests { + test("should return json file body contents buffer", function() { + const data = res.getBody(); + expect(res.getBody()).to.eql({ + "type": "Buffer", + "data": [ + 123, + 10, + 32, + 32, + 34, + 104, + 101, + 108, + 108, + 111, + 34, + 58, + 32, + 34, + 98, + 114, + 117, + 110, + 111, + 34, + 10, + 125, + 10 + ] + }); + }); + + test("should return proper header", function() { + const contentType = res.getHeader('content-type'); + expect(contentType).to.eql('application/json; charset=utf-8'); + }); + +} \ No newline at end of file diff --git a/packages/bruno-tests/collection/echo/echo file body/echo text file body.bru b/packages/bruno-tests/collection/echo/echo file body/echo text file body.bru new file mode 100644 index 000000000..707a096cd --- /dev/null +++ b/packages/bruno-tests/collection/echo/echo file body/echo text file body.bru @@ -0,0 +1,30 @@ +meta { + name: echo text file body + type: http + seq: 3 +} + +post { + url: {{echo-host}} + body: file + auth: none +} + +body:file { + file: @file(file.txt) @contentType(text/plain; charset=utf-8) +} + +tests { + test("should return json file body contents", function() { + const data = res.getBody(); + const expectedData = `file.txt + + hello, bruno`; + expect(res.getBody()).to.eql(expectedData); + }); + + test("should return proper header", function() { + const contentType = res.getHeader('content-type'); + expect(contentType).to.eql('text/plain; charset=utf-8'); + }); +} \ No newline at end of file diff --git a/packages/bruno-tests/collection/echo/multiline/echo binary.bru b/packages/bruno-tests/collection/echo/multiline/echo binary.bru index d11b30413..704419886 100644 --- a/packages/bruno-tests/collection/echo/multiline/echo binary.bru +++ b/packages/bruno-tests/collection/echo/multiline/echo binary.bru @@ -6,10 +6,10 @@ meta { post { url: {{echo-host}} - body: binaryFile + body: file auth: none } -body:binary-file { +body:file { file: @file(bruno.png) @contentType(image/png) } diff --git a/packages/bruno-tests/collection/file.txt b/packages/bruno-tests/collection/file.txt new file mode 100644 index 000000000..0a1443d43 --- /dev/null +++ b/packages/bruno-tests/collection/file.txt @@ -0,0 +1,3 @@ +file.txt + +hello, bruno