-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2121 from end3rbyte/feature/1602-multipart-conten…
…t-type feature: Multi-part requests: user should be able to set content-type for each part in a multi-part request. #1602
- Loading branch information
Showing
18 changed files
with
198 additions
and
17 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
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
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
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
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) | ||
} | ||
|
||
assert { | ||
res.status: eq 200 | ||
res.body.find(p=>p.name === 'param1').contentType: isUndefined | ||
res.body.find(p=>p.name === 'param2').contentType: eq application/json | ||
res.body.find(p=>p.name === 'param3').contentType: eq 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.