forked from usebruno/bruno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Multi-part requests: user should be able to set content-type…
… for each part in a multi-part request. usebruno#1602
- Loading branch information
busy-panda
committed
Apr 18, 2024
1 parent
d027d90
commit 88df962
Showing
15 changed files
with
209 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ const Wrapper = styled.div` | |
width: 30%; | ||
} | ||
&:nth-child(3) { | ||
&:nth-child(4) { | ||
width: 70px; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/bruno-tests/collection/multipart/mixed-content-types.bru
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
meta { | ||
name: mixed-content-types | ||
type: http | ||
seq: 1 | ||
} | ||
|
||
post { | ||
url: {{host}}/api/multipart/mixed-content-types | ||
body: multipartForm | ||
auth: none | ||
} | ||
|
||
body:multipart-form { | ||
param1: test | ||
param2: {"test":"i am json"} (Content-Type=application/json) | ||
param3: @file(multipart/small.png) | ||
} | ||
|
||
tests { | ||
test("Status code is 200", function () { | ||
expect(res.getStatus()).to.equal(200); | ||
}); | ||
test("param1 has no content-type", function () { | ||
var param1 = res.body.find(p=>p.name === 'param1') | ||
expect(param1).to.be.an('object'); | ||
expect(param1.contentType).to.be.undefined; | ||
}); | ||
test("param2 has content-type application/json", function () { | ||
var param2 = res.body.find(p=>p.name === 'param2') | ||
expect(param2).to.be.an('object'); | ||
expect(param2.contentType).to.equals('application/json'); | ||
}); | ||
test("param3 has content-type image/png", function () { | ||
var param3 = res.body.find(p=>p.name === 'param3') | ||
expect(param3).to.be.an('object'); | ||
expect(param3.contentType).to.equals('image/png'); | ||
}); | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Instead of using multer for example to parse the multipart form data, we build our own parser | ||
* so that we can verify the content type are set correctly by bruno (for example application/json for json content) | ||
*/ | ||
|
||
const extractParam = function (param, str, delimiter, quote, endDelimiter) { | ||
let regex = new RegExp(`${param}${delimiter}\\s*${quote}(.*?)${quote}${endDelimiter}`); | ||
const found = str.match(regex); | ||
if (found != null && found.length > 1) { | ||
return found[1]; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
|
||
const init = function (app, express) { | ||
app.use(express.raw({ type: 'multipart/form-data' })); | ||
}; | ||
|
||
const parsePart = function (part) { | ||
let result = {}; | ||
const name = extractParam('name', part, '=', '"', ''); | ||
if (name) { | ||
result.name = name; | ||
} | ||
const filename = extractParam('filename', part, '=', '"', ''); | ||
if (filename) { | ||
result.filename = filename; | ||
} | ||
const contentType = extractParam('Content-Type', part, ':', '', ';'); | ||
if (contentType) { | ||
result.contentType = contentType; | ||
} | ||
if (!filename) { | ||
result.value = part.substring(part.indexOf('value=') + 'value='.length); | ||
} | ||
if (contentType === 'application/json') { | ||
result.value = JSON.parse(result.value); | ||
} | ||
return result; | ||
}; | ||
|
||
const parse = function (req) { | ||
const BOUNDARY = 'boundary='; | ||
const contentType = req.headers['content-type']; | ||
const boundary = '--' + contentType.substring(contentType.indexOf(BOUNDARY) + BOUNDARY.length); | ||
const rawBody = req.body.toString(); | ||
let parts = rawBody.split(boundary).filter((part) => part.length > 0); | ||
parts = parts.map((part) => part.trim('\r\n')); | ||
parts = parts.filter((part) => part != '--'); | ||
parts = parts.map((part) => part.replace('\r\n\r\n', ';value=')); | ||
parts = parts.map((part) => part.replace('\r\n', ';')); | ||
parts = parts.map((part) => parsePart(part)); | ||
return parts; | ||
}; | ||
|
||
module.exports.parse = parse; | ||
module.exports.init = init; |
Oops, something went wrong.