Skip to content

Commit

Permalink
fix: make codegenerator not crash
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosadsj committed Jan 6, 2025
1 parent 06e78f2 commit 8cce27c
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 17 deletions.
59 changes: 42 additions & 17 deletions packages/bruno-app/src/utils/codegenerator/har.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const createContentType = (mode) => {
return 'application/json';
case 'multipartForm':
return 'multipart/form-data';
case 'binaryFile':
return 'application/octet-stream';
default:
return '';
}
Expand Down Expand Up @@ -60,26 +62,48 @@ const createPostData = (body, type) => {
}

const contentType = createContentType(body.mode);
if (body.mode === 'formUrlEncoded' || body.mode === 'multipartForm') {
return {
mimeType: contentType,
params: body[body.mode]
.filter((param) => param.enabled)
.map((param) => ({
name: param.name,
value: param.value,
...(param.type === 'file' && { fileName: param.value })
}))
};
} else {
return {
mimeType: contentType,
text: body[body.mode]
};

switch (body.mode) {
case 'formUrlEncoded':
case 'multipartForm':
return {
mimeType: contentType,
params: body[body.mode]
.filter((param) => param.enabled)
.map((param) => ({
name: param.name,
value: param.value,
...(param.type === 'file' && { fileName: param.value })
}))
};
case 'binaryFile':
const binary = {
mimeType: 'application/octet-stream',
// mimeType: body[body.mode].filter((param) => param.enabled)[0].contentType,
params: body[body.mode]
.filter((param) => param.enabled)
.map((param) => ({
name: param.name,
value: param.value,
fileName: param.value
}))
};

console.log('curl-binary', binary);
return binary;
default:
return {
mimeType: contentType,
text: body[body.mode]
};
}
};

export const buildHarRequest = ({ request, headers, type }) => {

console.log('buildHarRequest', request, headers, type);

console.log('buildHarRequest-postData', createPostData(request.body, type));
return {
method: request.method,
url: encodeURI(request.url),
Expand All @@ -89,6 +113,7 @@ export const buildHarRequest = ({ request, headers, type }) => {
queryString: createQuery(request.params),
postData: createPostData(request.body, type),
headersSize: 0,
bodySize: 0
bodySize: 0,
binary: true
};
};
36 changes: 36 additions & 0 deletions packages/bruno-app/src/utils/curl/curl-to-json.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,40 @@ describe('curlToJson', () => {
method: 'get'
});
});

it('should return a parse a curl with a post body with binary file type', () => {
const curlCommand = `curl 'https://www.usebruno.com'
-H 'Accept: application/json, text/plain, */*'
-H 'Accept-Language: en-US,en;q=0.9,hi;q=0.8'
-H 'Content-Type: application/json;charset=utf-8'
-H 'Origin: https://www.usebruno.com'
-H 'Referer: https://www.usebruno.com/'
--data-binary '@/path/to/file'
`;

const result = curlToJson(curlCommand);

expect(result).toEqual({
url: 'https://www.usebruno.com',
raw_url: 'https://www.usebruno.com',
method: 'post',
headers: {
Accept: 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.9,hi;q=0.8',
'Content-Type': 'application/json;charset=utf-8',
Origin: 'https://www.usebruno.com',
Referer: 'https://www.usebruno.com/'
},
isDataBinary: true,
data: [
{
name: 'file',
value: ['/path/to/file'],
enabled: true,
contentType: 'application/json;charset=utf-8',
type: 'binaryFile'
}
]
});
});
});
27 changes: 27 additions & 0 deletions packages/bruno-tests/collection/binaryFile/binary-file-types.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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
}
9 changes: 9 additions & 0 deletions packages/bruno-tests/collection/binaryFile/binary-file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "1",
"name": "bruno-testing",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}

0 comments on commit 8cce27c

Please sign in to comment.