I had to make assumptions about the loading method, taking the body as input instead of multi-part form data. So below is an example where the original body is passed in for download
const request = require('supertest'); const express = require('express'); const fs = require('fs') const app = express(); var bodyParser = require('body-parser') app.use(bodyParser.raw({type: 'application/octet-stream'})) app.post('/user', function(req, res) { res.status(200).json({ name: 'tobi' }); }); testImage = './package.json' resp = request(app) .post('/user') resp.set('Authorization', `Bearer Test`).set('Content-Type', 'application/octet-stream') resp.send(fs.readFileSync(testImage, 'utf-8')) resp.expect(200) .then(response => { console.log("response",response); }).catch((err) => { console.log(err) })
If you use multipart/form-data , then the code below shows an example
const request = require('supertest'); const express = require('express'); const fs = require('fs') const app = express(); app.post('/user', function(req, res) { // capture the encoded form data req.on('data', (data) => { console.log(data.toString()); }); // send a response when finished reading // the encoded form data req.on('end', () => { res.status(200).json({ name: 'tobi' }); }); }); testImage = './package.json' resp = request(app) .post('/user') resp.set('Authorization', `Bearer Test`) resp.attach("file", testImage) resp.expect(200) .then(response => { console.log("response",response); }).catch((err) => { console.log(err) })
source share