@@ -1500,6 +1500,25 @@ describe('node-fetch', () => {
1500
1500
} ) ;
1501
1501
} ) ;
1502
1502
1503
+ it ( 'should support URLSearchParams as POST body' , ( ) => {
1504
+ const params = new URLSearchParams ( ) ;
1505
+ params . set ( 'key1' , 'value1' ) ;
1506
+ params . set ( 'key2' , 'value2' ) ;
1507
+
1508
+ const url = `${ base } multipart` ;
1509
+ const options = {
1510
+ method : 'POST' ,
1511
+ body : params
1512
+ } ;
1513
+
1514
+ return fetch ( url , options ) . then ( res => res . json ( ) ) . then ( res => {
1515
+ expect ( res . method ) . to . equal ( 'POST' ) ;
1516
+ expect ( res . headers [ 'content-type' ] ) . to . startWith ( 'application/x-www-form-urlencoded' ) ;
1517
+ expect ( res . body ) . to . contain ( 'key1=' ) ;
1518
+ expect ( res . body ) . to . contain ( 'key2=' ) ;
1519
+ } ) ;
1520
+ } ) ;
1521
+
1503
1522
it ( 'should allow POST request with object body' , ( ) => {
1504
1523
const url = `${ base } inspect` ;
1505
1524
// Note that fetch simply calls tostring on an object
@@ -1548,6 +1567,21 @@ describe('node-fetch', () => {
1548
1567
} ) ;
1549
1568
} ) ;
1550
1569
1570
+ it ( 'constructing a Request with URLSearchParams should provide formData()' , ( ) => {
1571
+ const parameters = new URLSearchParams ( ) ;
1572
+ parameters . append ( 'key' , 'value' ) ;
1573
+ const request = new Request ( base , {
1574
+ method : 'POST' ,
1575
+ headers : {
1576
+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
1577
+ } ,
1578
+ body : parameters ,
1579
+ } ) ;
1580
+ return request . formData ( ) . then ( formData => {
1581
+ expect ( formData . get ( 'key' ) ) . to . equal ( 'value' ) ;
1582
+ } ) ;
1583
+ } ) ;
1584
+
1551
1585
it ( 'should allow POST request with URLSearchParams as body' , ( ) => {
1552
1586
const parameters = new URLSearchParams ( ) ;
1553
1587
parameters . append ( 'a' , '1' ) ;
0 commit comments