Skip to content

Commit 5d2b9d4

Browse files
author
Prashant Shubham
committed
Addressed review comments
Created dynamic stream and local server for post request.
1 parent b60690d commit 5d2b9d4

File tree

3 files changed

+88
-59
lines changed

3 files changed

+88
-59
lines changed

CHANGELOG.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
master:
2-
date: 2020-10-05
32
chores:
4-
- GH-1094 Added integration test for large files upload.
3+
- GH-1094 Added integration test for large files upload
54

65
7.26.7:
76
date: 2020-10-07

test/fixtures/servers/http.js

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

51+
httpServer.on('/file-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': 'text/plain'});
61+
res.end('received-content-length:' + Buffer.concat(body).byteLength);
62+
});
63+
}
64+
});
65+
5166
module.exports = httpServer;

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

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,7 @@ var fs = require('fs'),
22
expect = require('chai').expect,
33
sinon = require('sinon'),
44
IS_BROWSER = typeof window !== 'undefined',
5-
TEST_UPLOAD_FILE_LARGE = 'test/fixtures/upload-file-large.json',
6-
// don't import shelljs if tests are running within browser
7-
sh = IS_BROWSER ? null : require('shelljs'),
8-
9-
// Creates 50M size file based on current execution platform
10-
createLargeTestFileForPlatform = function () {
11-
switch (process.platform) {
12-
case 'linux':
13-
sh.exec('dd if=/dev/zero of=' + TEST_UPLOAD_FILE_LARGE + ' bs=50M count=1');
14-
break;
15-
16-
case 'win32':
17-
// 52428800 bytes corresponds to 50 MB file size as fsutil takes size param in bytes
18-
sh.exec('fsutil file createnew ' + TEST_UPLOAD_FILE_LARGE + ' 52428800');
19-
break;
20-
21-
case 'darwin':
22-
sh.exec('mkfile 50M ' + TEST_UPLOAD_FILE_LARGE);
23-
break;
24-
25-
default:
26-
// eslint-disable-next-line no-console
27-
console.log('Platform is not supported.');
28-
}
29-
};
5+
{Readable} = require('stream');
306

317
describe('file upload in request body', function () {
328
var testrun;
@@ -672,36 +648,90 @@ describe('file upload in request body', function () {
672648
});
673649

674650
(IS_BROWSER ? describe.skip : describe)('large file upload in request body', function () {
675-
after(function () {
676-
sh.rm('-rf', TEST_UPLOAD_FILE_LARGE);
651+
const inStream = new Readable({
652+
// eslint-disable-next-line no-empty-function
653+
read () {}
677654
});
678655

679656
// eslint-disable-next-line mocha/no-sibling-hooks
680657
before(function (done) {
681-
this.enableTimeouts(false);
682-
createLargeTestFileForPlatform();
683-
684658
this.run({
685-
fileResolver: fs,
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+
},
686673
collection: {
687674
item: [{
688675
request: {
689-
url: 'https://postman-echo.com/post',
676+
url: global.servers.http + '/file-upload',
690677
method: 'POST',
691678
body: {
692679
mode: 'file',
693-
file: {src: TEST_UPLOAD_FILE_LARGE}
680+
file: {src: 'test/fixtures/upload-file-large-dummy'}
694681
}
695682
}
696-
}, {
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.text()).to.include('received-content-length:52428800');
706+
sinon.assert.calledWith(testrun.request.getCall(0), null);
707+
});
708+
});
709+
710+
(IS_BROWSER ? describe.skip : describe)('large file upload in form-data mode', function () {
711+
// eslint-disable-next-line mocha/no-sibling-hooks
712+
before(function (done) {
713+
this.run({
714+
// using a custom file-resolver since we don't want to create
715+
// actual file size of 50MB for testing large file uploads
716+
fileResolver: {
717+
stat: function (src, cb) {
718+
cb(null, {isFile: function () { return true; }, mode: 33188});
719+
},
720+
createReadStream: function () {
721+
// creating buffer of size 52428800 bytes corresponds to 50 MB
722+
return Buffer.alloc(50 * 1024 * 1024);
723+
}
724+
},
725+
collection: {
726+
item: [{
697727
request: {
698-
url: 'https://postman-echo.com/post',
728+
url: global.servers.http + '/file-upload',
699729
method: 'POST',
700730
body: {
701731
mode: 'formdata',
702732
formdata: [{
703733
key: 'file',
704-
src: TEST_UPLOAD_FILE_LARGE,
734+
src: 'test/fixtures/upload-file-large-dummy',
705735
type: 'file'
706736
}]
707737
}
@@ -720,30 +750,15 @@ describe('file upload in request body', function () {
720750
sinon.assert.calledOnce(testrun.start);
721751
sinon.assert.calledOnce(testrun.done);
722752
sinon.assert.calledWith(testrun.done.getCall(0), null);
723-
sinon.assert.callCount(testrun.request, 2);
724-
});
725-
726-
it('should upload the large file correctly', function () {
727-
sinon.assert.calledWith(testrun.request.getCall(0), null);
728-
729-
var resp = JSON.parse(testrun.response.getCall(0).args[2].stream.toString());
730-
731-
expect(resp).to.nested.include({
732-
'headers.content-length': '52428800'
733-
});
734-
expect(resp.headers['content-type']).to.equal('application/json');
753+
sinon.assert.callCount(testrun.request, 1);
735754
});
736755

737-
it('should upload the large file in formdata mode correctly', function () {
738-
sinon.assert.calledWith(testrun.request.getCall(1), null);
739-
740-
var resp = JSON.parse(testrun.response.getCall(1).args[2].stream.toString());
756+
it('should upload the file in formdata mode correctly', function () {
757+
var response = testrun.request.getCall(0).args[2];
741758

742-
expect(resp.files).to.have.property('upload-file-large.json');
743-
expect(resp).to.nested.include({
744-
'headers.content-length': '52429026'
745-
});
746-
expect(resp.headers['content-type']).to.match(/multipart\/form-data/);
759+
sinon.assert.calledWith(testrun.request.getCall(0), null);
760+
expect(response.reason()).to.eql('OK');
761+
expect(response.text()).to.include('received-content-length:52428999');
747762
});
748763
});
749764
});

0 commit comments

Comments
 (0)