@@ -34,6 +34,7 @@ const helpers = {
34
34
setUtf8StringSerializer : function ( resolve ) { resolve ( cordova . plugin . http . setDataSerializer ( 'utf8' ) ) ; } ,
35
35
setUrlEncodedSerializer : function ( resolve ) { resolve ( cordova . plugin . http . setDataSerializer ( 'urlencoded' ) ) ; } ,
36
36
setMultipartSerializer : function ( resolve ) { resolve ( cordova . plugin . http . setDataSerializer ( 'multipart' ) ) ; } ,
37
+ setRawSerializer : function ( resolve ) { resolve ( cordova . plugin . http . setDataSerializer ( 'raw' ) ) ; } ,
37
38
disableFollowingRedirect : function ( resolve ) { resolve ( cordova . plugin . http . setFollowRedirect ( false ) ) ; } ,
38
39
enableFollowingRedirect : function ( resolve ) { resolve ( cordova . plugin . http . setFollowRedirect ( true ) ) ; } ,
39
40
getWithXhr : function ( done , url , type ) {
@@ -845,30 +846,27 @@ const tests = [
845
846
}
846
847
} ,
847
848
{
848
- description : 'should send raw byte array correctly (POST)' ,
849
- expected : 'resolved: {"isArrayBuffer:true,"data":[1,2,3,4,5,6,7],"byteLength":7}' ,
849
+ description : 'should send raw byte array correctly (POST) #291' ,
850
+ expected : 'resolved: {"status":200,"data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUg ...' ,
851
+ before : helpers . setRawSerializer ,
850
852
func : function ( resolve , reject ) {
851
- var url = 'http://httpbin.org/anything' ;
852
- var options = {
853
- method : 'post' ,
854
- serializer : 'raw' ,
855
- data : new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ) ,
856
- responseType : 'arraybuffer'
857
- } ;
858
- var success = function ( response ) {
859
- resolve ( {
860
- isArrayBuffer : response . data . constructor === ArrayBuffer ,
861
- data : new Uint8Array ( response . data ) ,
862
- byteLength : response . data . byteLength
863
- } ) ;
864
- } ;
865
- cordova . plugin . http . sendRequest ( url , options , success , reject ) ;
853
+ helpers . getWithXhr ( function ( buffer ) {
854
+ cordova . plugin . http . post ( 'http://httpbin.org/anything' , buffer , { } , resolve , reject ) ;
855
+ } , './res/cordova_logo.png' , 'arraybuffer' ) ;
866
856
} ,
867
857
validationFunc : function ( driver , result ) {
868
- result . type . should . be . equal ( 'resolved' ) ;
869
- result . data . isArrayBuffer . should . be . equal ( true ) ;
870
- result . data . should . be . eql ( new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ) ) ;
871
- result . data . byteLength . should . be . equal ( 7 ) ;
858
+ helpers . checkResult ( result , 'resolved' ) ;
859
+ result . data . status . should . be . equal ( 200 ) ;
860
+
861
+ // httpbin.org encodes posted binaries in base64 and echoes them back
862
+ // therefore we need to check for base64 string with mime type prefix
863
+ const fs = require ( 'fs' ) ;
864
+ const rawLogo = fs . readFileSync ( './test/e2e-app-template/www/res/cordova_logo.png' ) ;
865
+ const b64Logo = rawLogo . toString ( 'base64' ) ;
866
+ const parsed = JSON . parse ( result . data . data ) ;
867
+
868
+ parsed . headers [ 'Content-Type' ] . should . be . equal ( 'application/octet-stream' ) ;
869
+ parsed . data . should . be . equal ( 'data:application/octet-stream;base64,' + b64Logo ) ;
872
870
}
873
871
} ,
874
872
0 commit comments