@@ -18,6 +18,12 @@ const path = require('path');
18
18
const stream = require ( 'stream' ) ;
19
19
const { parse : parseURL , URLSearchParams } = require ( 'url' ) ;
20
20
const { lookup } = require ( 'dns' ) ;
21
+ const vm = require ( 'vm' ) ;
22
+
23
+ const {
24
+ ArrayBuffer : VMArrayBuffer ,
25
+ Uint8Array : VMUint8Array
26
+ } = vm . runInNewContext ( 'this' ) ;
21
27
22
28
let convert ;
23
29
try { convert = require ( 'encoding' ) . convert ; } catch ( e ) { }
@@ -876,6 +882,27 @@ describe('node-fetch', () => {
876
882
} ) ;
877
883
} ) ;
878
884
885
+ it ( 'should allow POST request with ArrayBuffer body from a VM context' , function ( ) {
886
+ // TODO: Node.js v4 doesn't support ArrayBuffer from other contexts, so we skip this test, drop this check once Node.js v4 support is not needed
887
+ try {
888
+ Buffer . from ( new VMArrayBuffer ( ) ) ;
889
+ } catch ( err ) {
890
+ this . skip ( ) ;
891
+ }
892
+ const url = `${ base } inspect` ;
893
+ const opts = {
894
+ method : 'POST' ,
895
+ body : new VMUint8Array ( Buffer . from ( 'Hello, world!\n' ) ) . buffer
896
+ } ;
897
+ return fetch ( url , opts ) . then ( res => res . json ( ) ) . then ( res => {
898
+ expect ( res . method ) . to . equal ( 'POST' ) ;
899
+ expect ( res . body ) . to . equal ( 'Hello, world!\n' ) ;
900
+ expect ( res . headers [ 'transfer-encoding' ] ) . to . be . undefined ;
901
+ expect ( res . headers [ 'content-type' ] ) . to . be . undefined ;
902
+ expect ( res . headers [ 'content-length' ] ) . to . equal ( '14' ) ;
903
+ } ) ;
904
+ } ) ;
905
+
879
906
it ( 'should allow POST request with ArrayBufferView (Uint8Array) body' , function ( ) {
880
907
const url = `${ base } inspect` ;
881
908
const opts = {
@@ -906,6 +933,27 @@ describe('node-fetch', () => {
906
933
} ) ;
907
934
} ) ;
908
935
936
+ it ( 'should allow POST request with ArrayBufferView (Uint8Array) body from a VM context' , function ( ) {
937
+ // TODO: Node.js v4 doesn't support ArrayBufferView from other contexts, so we skip this test, drop this check once Node.js v4 support is not needed
938
+ try {
939
+ Buffer . from ( new VMArrayBuffer ( ) ) ;
940
+ } catch ( err ) {
941
+ this . skip ( ) ;
942
+ }
943
+ const url = `${ base } inspect` ;
944
+ const opts = {
945
+ method : 'POST' ,
946
+ body : new VMUint8Array ( Buffer . from ( 'Hello, world!\n' ) )
947
+ } ;
948
+ return fetch ( url , opts ) . then ( res => res . json ( ) ) . then ( res => {
949
+ expect ( res . method ) . to . equal ( 'POST' ) ;
950
+ expect ( res . body ) . to . equal ( 'Hello, world!\n' ) ;
951
+ expect ( res . headers [ 'transfer-encoding' ] ) . to . be . undefined ;
952
+ expect ( res . headers [ 'content-type' ] ) . to . be . undefined ;
953
+ expect ( res . headers [ 'content-length' ] ) . to . equal ( '14' ) ;
954
+ } ) ;
955
+ } ) ;
956
+
909
957
// TODO: Node.js v4 doesn't support necessary Buffer API, so we skip this test, drop this check once Node.js v4 support is not needed
910
958
( Buffer . from . length === 3 ? it : it . skip ) ( 'should allow POST request with ArrayBufferView (Uint8Array, offset, length) body' , function ( ) {
911
959
const url = `${ base } inspect` ;
@@ -1919,6 +1967,20 @@ describe('Response', function () {
1919
1967
} ) ;
1920
1968
} ) ;
1921
1969
1970
+ it ( 'should support Uint8Array as body' , function ( ) {
1971
+ const res = new Response ( new Uint8Array ( stringToArrayBuffer ( 'a=1' ) ) ) ;
1972
+ return res . text ( ) . then ( result => {
1973
+ expect ( result ) . to . equal ( 'a=1' ) ;
1974
+ } ) ;
1975
+ } ) ;
1976
+
1977
+ it ( 'should support DataView as body' , function ( ) {
1978
+ const res = new Response ( new DataView ( stringToArrayBuffer ( 'a=1' ) ) ) ;
1979
+ return res . text ( ) . then ( result => {
1980
+ expect ( result ) . to . equal ( 'a=1' ) ;
1981
+ } ) ;
1982
+ } ) ;
1983
+
1922
1984
it ( 'should default to null as body' , function ( ) {
1923
1985
const res = new Response ( ) ;
1924
1986
expect ( res . body ) . to . equal ( null ) ;
@@ -2124,6 +2186,26 @@ describe('Request', function () {
2124
2186
expect ( result ) . to . equal ( 'a=1' ) ;
2125
2187
} ) ;
2126
2188
} ) ;
2189
+
2190
+ it ( 'should support Uint8Array as body' , function ( ) {
2191
+ const req = new Request ( '' , {
2192
+ method : 'POST' ,
2193
+ body : new Uint8Array ( stringToArrayBuffer ( 'a=1' ) )
2194
+ } ) ;
2195
+ return req . text ( ) . then ( result => {
2196
+ expect ( result ) . to . equal ( 'a=1' ) ;
2197
+ } ) ;
2198
+ } ) ;
2199
+
2200
+ it ( 'should support DataView as body' , function ( ) {
2201
+ const req = new Request ( '' , {
2202
+ method : 'POST' ,
2203
+ body : new DataView ( stringToArrayBuffer ( 'a=1' ) )
2204
+ } ) ;
2205
+ return req . text ( ) . then ( result => {
2206
+ expect ( result ) . to . equal ( 'a=1' ) ;
2207
+ } ) ;
2208
+ } ) ;
2127
2209
} ) ;
2128
2210
2129
2211
function streamToPromise ( stream , dataHandler ) {
0 commit comments