Skip to content

Commit 9862d37

Browse files
author
Prashant Shubham
committed
Add integration test for large file upload
Added integration test for file upload of 50 MB.
1 parent ebbf184 commit 9862d37

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

CHANGELOG.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
master:
2+
chores:
3+
- GH-1094 Added integration test for large files upload
4+
15
7.26.7:
26
date: 2020-10-07
37
chores:

test/fixtures/servers/http.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,27 @@ httpServer.on('/custom-reason', function (req, res) {
4848
res.end();
4949
});
5050

51+
httpServer.on('/upload', function (req, res) {
52+
if (req.method === 'POST') {
53+
let body = [];
54+
55+
req.on('data', function (data) {
56+
body.push(data);
57+
});
58+
59+
req.on('end', function () {
60+
res.writeHead(200, {'content-type': 'application/json'});
61+
let response = {
62+
'received-content-length': Buffer.concat(body).byteLength
63+
};
64+
65+
res.end(JSON.stringify(response));
66+
});
67+
}
68+
else {
69+
res.writeHead(200, {'content-type': 'text/plain'});
70+
res.end('Okay!');
71+
}
72+
});
73+
5174
module.exports = httpServer;

test/integration/file-uploads/request-body.test.js

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var fs = require('fs'),
22
expect = require('chai').expect,
33
sinon = require('sinon'),
4-
IS_BROWSER = typeof window !== 'undefined';
4+
IS_BROWSER = typeof window !== 'undefined',
5+
{Readable} = require('stream');
56

67
describe('file upload in request body', function () {
78
var testrun;
@@ -645,4 +646,123 @@ describe('file upload in request body', function () {
645646
.to.equal('Binary file load error: file resolver interface mismatch');
646647
});
647648
});
649+
650+
(IS_BROWSER ? describe.skip : describe)('large file upload in request body', function () {
651+
const inStream = new Readable({
652+
// eslint-disable-next-line no-empty-function
653+
read () {}
654+
});
655+
656+
// eslint-disable-next-line mocha/no-sibling-hooks
657+
before(function (done) {
658+
this.run({
659+
// using a custom file-resolver since we don't want to create
660+
// actual file size of 50MB for testing large file uploads
661+
fileResolver: {
662+
stat: function (src, cb) {
663+
cb(null, {isFile: function () { return true; }, mode: 33188});
664+
},
665+
createReadStream: function () {
666+
// creating buffer of size 52428800 bytes corresponds to 50 MB
667+
inStream.push(Buffer.alloc(50 * 1024 * 1024));
668+
inStream.push(null);
669+
670+
return inStream;
671+
}
672+
},
673+
collection: {
674+
item: [{
675+
request: {
676+
url: global.servers.http + '/upload',
677+
method: 'POST',
678+
body: {
679+
mode: 'file',
680+
file: {src: 'test/fixtures/upload-file-large-dummy'}
681+
}
682+
}
683+
}]
684+
}
685+
}, function (err, results) {
686+
testrun = results;
687+
done(err);
688+
});
689+
});
690+
691+
// eslint-disable-next-line mocha/no-identical-title
692+
it('should complete the run', function () {
693+
expect(testrun).to.be.ok;
694+
sinon.assert.calledOnce(testrun.start);
695+
sinon.assert.calledOnce(testrun.done);
696+
sinon.assert.calledWith(testrun.done.getCall(0), null);
697+
sinon.assert.callCount(testrun.request, 1);
698+
});
699+
700+
it('should upload the large file correctly', function () {
701+
var response = testrun.request.getCall(0).args[2];
702+
703+
expect(response.reason()).to.eql('OK');
704+
// 52428800 bytes corresponds to 50 MB
705+
expect(response.json()).to.nested.include({
706+
'received-content-length': 52428800
707+
});
708+
sinon.assert.calledWith(testrun.request.getCall(0), null);
709+
});
710+
});
711+
712+
(IS_BROWSER ? describe.skip : describe)('large file upload in form-data mode', function () {
713+
// eslint-disable-next-line mocha/no-sibling-hooks
714+
before(function (done) {
715+
this.run({
716+
// using a custom file-resolver since we don't want to create
717+
// actual file size of 50MB for testing large file uploads
718+
fileResolver: {
719+
stat: function (src, cb) {
720+
cb(null, {isFile: function () { return true; }, mode: 33188});
721+
},
722+
createReadStream: function () {
723+
// creating buffer of size 52428800 bytes corresponds to 50 MB
724+
return Buffer.alloc(50 * 1024 * 1024);
725+
}
726+
},
727+
collection: {
728+
item: [{
729+
request: {
730+
url: global.servers.http + '/upload',
731+
method: 'POST',
732+
body: {
733+
mode: 'formdata',
734+
formdata: [{
735+
key: 'file',
736+
src: 'test/fixtures/upload-file-large-dummy',
737+
type: 'file'
738+
}]
739+
}
740+
}
741+
}]
742+
}
743+
}, function (err, results) {
744+
testrun = results;
745+
done(err);
746+
});
747+
});
748+
749+
// eslint-disable-next-line mocha/no-identical-title
750+
it('should complete the run', function () {
751+
expect(testrun).to.be.ok;
752+
sinon.assert.calledOnce(testrun.start);
753+
sinon.assert.calledOnce(testrun.done);
754+
sinon.assert.calledWith(testrun.done.getCall(0), null);
755+
sinon.assert.callCount(testrun.request, 1);
756+
});
757+
758+
it('should upload the file in formdata mode correctly', function () {
759+
var response = testrun.request.getCall(0).args[2];
760+
761+
sinon.assert.calledWith(testrun.request.getCall(0), null);
762+
expect(response.reason()).to.eql('OK');
763+
expect(response.json()).to.nested.include({
764+
'received-content-length': 52428999
765+
});
766+
});
767+
});
648768
});

0 commit comments

Comments
 (0)