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.
feat: adding expressjs route for testing multipart content types (use…
- Loading branch information
Nicolas PUZIN
committed
Apr 17, 2024
1 parent
95ad1db
commit 7eab1b6
Showing
5 changed files
with
87 additions
and
6 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
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,17 @@ | ||
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: @file(multipart/small.png) | ||
param3: {"test":"i am json"} | ||
} |
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,56 @@ | ||
/** | ||
* 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 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) => { | ||
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; | ||
}); | ||
return parts; | ||
}; | ||
|
||
module.exports.parse = parse; | ||
module.exports.init = init; |
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,10 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const formDataParser = require('./form-data-parser'); | ||
|
||
router.post('/mixed-content-types', (req, res) => { | ||
const parts = formDataParser.parse(req); | ||
return res.json(parts); | ||
}); | ||
|
||
module.exports = router; |