Skip to content

Commit

Permalink
Add integration test for large file upload
Browse files Browse the repository at this point in the history
Added integration test for file upload of 50 MB.
  • Loading branch information
Prashant Shubham committed Oct 21, 2020
1 parent ebbf184 commit 9862d37
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
master:
chores:
- GH-1094 Added integration test for large files upload

7.26.7:
date: 2020-10-07
chores:
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/servers/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,27 @@ httpServer.on('/custom-reason', function (req, res) {
res.end();
});

httpServer.on('/upload', function (req, res) {
if (req.method === 'POST') {
let body = [];

req.on('data', function (data) {
body.push(data);
});

req.on('end', function () {
res.writeHead(200, {'content-type': 'application/json'});
let response = {
'received-content-length': Buffer.concat(body).byteLength
};

res.end(JSON.stringify(response));
});
}
else {
res.writeHead(200, {'content-type': 'text/plain'});
res.end('Okay!');
}
});

module.exports = httpServer;
122 changes: 121 additions & 1 deletion test/integration/file-uploads/request-body.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var fs = require('fs'),
expect = require('chai').expect,
sinon = require('sinon'),
IS_BROWSER = typeof window !== 'undefined';
IS_BROWSER = typeof window !== 'undefined',
{Readable} = require('stream');

describe('file upload in request body', function () {
var testrun;
Expand Down Expand Up @@ -645,4 +646,123 @@ describe('file upload in request body', function () {
.to.equal('Binary file load error: file resolver interface mismatch');
});
});

(IS_BROWSER ? describe.skip : describe)('large file upload in request body', function () {
const inStream = new Readable({
// eslint-disable-next-line no-empty-function
read () {}
});

// eslint-disable-next-line mocha/no-sibling-hooks
before(function (done) {
this.run({
// using a custom file-resolver since we don't want to create
// actual file size of 50MB for testing large file uploads
fileResolver: {
stat: function (src, cb) {
cb(null, {isFile: function () { return true; }, mode: 33188});
},
createReadStream: function () {
// creating buffer of size 52428800 bytes corresponds to 50 MB
inStream.push(Buffer.alloc(50 * 1024 * 1024));
inStream.push(null);

return inStream;
}
},
collection: {
item: [{
request: {
url: global.servers.http + '/upload',
method: 'POST',
body: {
mode: 'file',
file: {src: 'test/fixtures/upload-file-large-dummy'}
}
}
}]
}
}, function (err, results) {
testrun = results;
done(err);
});
});

// eslint-disable-next-line mocha/no-identical-title
it('should complete the run', function () {
expect(testrun).to.be.ok;
sinon.assert.calledOnce(testrun.start);
sinon.assert.calledOnce(testrun.done);
sinon.assert.calledWith(testrun.done.getCall(0), null);
sinon.assert.callCount(testrun.request, 1);
});

it('should upload the large file correctly', function () {
var response = testrun.request.getCall(0).args[2];

expect(response.reason()).to.eql('OK');
// 52428800 bytes corresponds to 50 MB
expect(response.json()).to.nested.include({
'received-content-length': 52428800
});
sinon.assert.calledWith(testrun.request.getCall(0), null);
});
});

(IS_BROWSER ? describe.skip : describe)('large file upload in form-data mode', function () {
// eslint-disable-next-line mocha/no-sibling-hooks
before(function (done) {
this.run({
// using a custom file-resolver since we don't want to create
// actual file size of 50MB for testing large file uploads
fileResolver: {
stat: function (src, cb) {
cb(null, {isFile: function () { return true; }, mode: 33188});
},
createReadStream: function () {
// creating buffer of size 52428800 bytes corresponds to 50 MB
return Buffer.alloc(50 * 1024 * 1024);
}
},
collection: {
item: [{
request: {
url: global.servers.http + '/upload',
method: 'POST',
body: {
mode: 'formdata',
formdata: [{
key: 'file',
src: 'test/fixtures/upload-file-large-dummy',
type: 'file'
}]
}
}
}]
}
}, function (err, results) {
testrun = results;
done(err);
});
});

// eslint-disable-next-line mocha/no-identical-title
it('should complete the run', function () {
expect(testrun).to.be.ok;
sinon.assert.calledOnce(testrun.start);
sinon.assert.calledOnce(testrun.done);
sinon.assert.calledWith(testrun.done.getCall(0), null);
sinon.assert.callCount(testrun.request, 1);
});

it('should upload the file in formdata mode correctly', function () {
var response = testrun.request.getCall(0).args[2];

sinon.assert.calledWith(testrun.request.getCall(0), null);
expect(response.reason()).to.eql('OK');
expect(response.json()).to.nested.include({
'received-content-length': 52428999
});
});
});
});

0 comments on commit 9862d37

Please sign in to comment.