@@ -2,31 +2,7 @@ var fs = require('fs'),
2
2
expect = require ( 'chai' ) . expect ,
3
3
sinon = require ( 'sinon' ) ,
4
4
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' ) ;
30
6
31
7
describe ( 'file upload in request body' , function ( ) {
32
8
var testrun ;
@@ -672,36 +648,90 @@ describe('file upload in request body', function () {
672
648
} ) ;
673
649
674
650
( 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 ( ) { }
677
654
} ) ;
678
655
679
656
// eslint-disable-next-line mocha/no-sibling-hooks
680
657
before ( function ( done ) {
681
- this . enableTimeouts ( false ) ;
682
- createLargeTestFileForPlatform ( ) ;
683
-
684
658
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
+ } ,
686
673
collection : {
687
674
item : [ {
688
675
request : {
689
- url : 'https://postman-echo.com/post ',
676
+ url : global . servers . http + '/file-upload ',
690
677
method : 'POST' ,
691
678
body : {
692
679
mode : 'file' ,
693
- file : { src : TEST_UPLOAD_FILE_LARGE }
680
+ file : { src : 'test/fixtures/upload-file-large-dummy' }
694
681
}
695
682
}
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 : [ {
697
727
request : {
698
- url : 'https://postman-echo.com/post ',
728
+ url : global . servers . http + '/file-upload ',
699
729
method : 'POST' ,
700
730
body : {
701
731
mode : 'formdata' ,
702
732
formdata : [ {
703
733
key : 'file' ,
704
- src : TEST_UPLOAD_FILE_LARGE ,
734
+ src : 'test/fixtures/upload-file-large-dummy' ,
705
735
type : 'file'
706
736
} ]
707
737
}
@@ -720,30 +750,15 @@ describe('file upload in request body', function () {
720
750
sinon . assert . calledOnce ( testrun . start ) ;
721
751
sinon . assert . calledOnce ( testrun . done ) ;
722
752
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 ) ;
735
754
} ) ;
736
755
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 ] ;
741
758
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 ( / m u l t i p a r t \/ f o r m - d a t a / ) ;
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' ) ;
747
762
} ) ;
748
763
} ) ;
749
764
} ) ;
0 commit comments