Skip to content

Commit 4474638

Browse files
author
Prashant Shubham
committed
Addressed review comments and added tests for form-data mode file upload
1 parent 84e0815 commit 4474638

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

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

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,31 @@ var fs = require('fs'),
22
expect = require('chai').expect,
33
sinon = require('sinon'),
44
IS_BROWSER = typeof window !== 'undefined',
5-
IS_LINUX = process.platform === 'linux',
6-
IS_DARWIN = process.platform === 'darwin',
7-
IS_WIN32 = process.platform === 'win32',
5+
TEST_UPLOAD_FILE_LARGE = 'test/fixtures/upload-file-large.json',
86
// don't import shelljs if tests are running within browser
9-
sh = IS_BROWSER ? null : require('shelljs');
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+
};
1030

1131
describe('file upload in request body', function () {
1232
var testrun;
@@ -651,26 +671,16 @@ describe('file upload in request body', function () {
651671
});
652672
});
653673

654-
(IS_BROWSER ? describe.skip : describe)('should upload large files correctly', function () {
655-
var testUploadFile = 'test/fixtures/upload-file-large.json';
656-
674+
(IS_BROWSER ? describe.skip : describe)('large file upload in request body', function () {
657675
afterEach(function () {
658-
sh.rm('-rf', testUploadFile);
676+
sh.rm('-rf', TEST_UPLOAD_FILE_LARGE);
659677
});
660678

661679
// eslint-disable-next-line mocha/no-sibling-hooks
662680
before(function (done) {
663681
this.enableTimeouts(false);
664-
if (IS_DARWIN) {
665-
sh.exec('mkfile 50M ' + testUploadFile);
666-
}
667-
else if (IS_LINUX) {
668-
sh.exec('dd if=/dev/zero of=' + testUploadFile + ' bs=50M count=1');
669-
}
670-
else if (IS_WIN32) {
671-
// 52428800 bytes corresponds to 50 MB file size as fsutil takes size param in bytes
672-
sh.exec('fsutil file createnew ' + testUploadFile + ' 52428800');
673-
}
682+
createLargeTestFileForPlatform();
683+
674684
this.run({
675685
fileResolver: fs,
676686
collection: {
@@ -680,7 +690,20 @@ describe('file upload in request body', function () {
680690
method: 'POST',
681691
body: {
682692
mode: 'file',
683-
file: {src: testUploadFile}
693+
file: {src: TEST_UPLOAD_FILE_LARGE}
694+
}
695+
}
696+
}, {
697+
request: {
698+
url: 'https://postman-echo.com/post',
699+
method: 'POST',
700+
body: {
701+
mode: 'formdata',
702+
formdata: [{
703+
key: 'file',
704+
src: TEST_UPLOAD_FILE_LARGE,
705+
type: 'file'
706+
}]
684707
}
685708
}
686709
}]
@@ -697,7 +720,7 @@ describe('file upload in request body', function () {
697720
sinon.assert.calledOnce(testrun.start);
698721
sinon.assert.calledOnce(testrun.done);
699722
sinon.assert.calledWith(testrun.done.getCall(0), null);
700-
sinon.assert.callCount(testrun.request, 1);
723+
sinon.assert.callCount(testrun.request, 2);
701724
});
702725

703726
it('should upload the large file correctly', function () {
@@ -710,5 +733,17 @@ describe('file upload in request body', function () {
710733
});
711734
expect(resp.headers['content-type']).to.equal('application/json');
712735
});
736+
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());
741+
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/);
747+
});
713748
});
714749
});

0 commit comments

Comments
 (0)