@@ -1500,6 +1500,25 @@ describe('node-fetch', () => {
15001500 } ) ;
15011501 } ) ;
15021502
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+
15031522 it ( 'should allow POST request with object body' , ( ) => {
15041523 const url = `${ base } inspect` ;
15051524 // Note that fetch simply calls tostring on an object
@@ -1548,6 +1567,21 @@ describe('node-fetch', () => {
15481567 } ) ;
15491568 } ) ;
15501569
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+
15511585 it ( 'should allow POST request with URLSearchParams as body' , ( ) => {
15521586 const parameters = new URLSearchParams ( ) ;
15531587 parameters . append ( 'a' , '1' ) ;
0 commit comments