diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index 12030f383..c8320c696 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -1,3 +1,7 @@ +master: + chores: + - GH-1094 Added integration test for large files upload + 7.26.7: date: 2020-10-07 chores: diff --git a/test/fixtures/servers/http.js b/test/fixtures/servers/http.js index c72c8feb2..b9b45384c 100644 --- a/test/fixtures/servers/http.js +++ b/test/fixtures/servers/http.js @@ -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; diff --git a/test/integration/file-uploads/request-body.test.js b/test/integration/file-uploads/request-body.test.js index ed411feb2..8df22e861 100644 --- a/test/integration/file-uploads/request-body.test.js +++ b/test/integration/file-uploads/request-body.test.js @@ -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; @@ -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 + }); + }); + }); });