Skip to content

Instantly share code, notes, and snippets.

@DanilloCorvalan
Created March 27, 2014 11:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DanilloCorvalan/9805138 to your computer and use it in GitHub Desktop.
Save DanilloCorvalan/9805138 to your computer and use it in GitHub Desktop.
Node test upload file multi part superagent plus supertest
var request = require('supertest');
var fs = require('fs');
var reqData = {
title: 'Rahh!',
fileName: 'test-image.jpeg'
};
var req = request(context.app) //context.app = your express object
.post('/v1/images')
.set('Authorization', 'Bearer ' + context.token);
req
.part()
.set('Content-Disposition', 'form-data; name="content"; filename="' + reqData.fileName + '"')
.set('Content-Type', 'image/jpeg')
.write(fs.readFileSync((__dirname + '/../fixtures/' + reqData.fileName))); //read the file
req
.part()
.set('Content-Disposition', 'form-data; name="title"')
.write(reqData.title);
req.end(function(err, res) {
//do your tests over here
});
@OctavioBR
Copy link

Thanks, very useful gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment