File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -44,7 +44,9 @@ function Api(config) {
4444 host : parsed . hostname ,
4545 port : parsed . port || ( isHttps ? 443 : 80 ) ,
4646 withCredentials : false ,
47- headers : { }
47+ headers : { } ,
48+ //=== true to make undefined result in false
49+ alwaysUseGet : config . alwaysUseGet === true
4850 } ;
4951
5052 // These params will be sent with each request
Original file line number Diff line number Diff line change @@ -34,11 +34,17 @@ module.exports = function(Api) {
3434 }
3535
3636 params = params || { } ;
37- util . _extend ( params , this . httpParams ) ;
37+ var options = { } ,
38+ alwaysUseGet = this . httpOptions . alwaysUseGet ;
39+
40+ util . _extend ( options , this . httpOptions ) ;
41+ if ( alwaysUseGet ) {
42+ params . method = method ;
43+ } else {
44+ options . method = method ;
45+ }
3846
39- var options = { } ;
4047 util . _extend ( options , this . httpOptions ) ;
41- options . method = method ;
4248 if ( path . indexOf ( '/' ) === 0 ) {
4349 options . path = this . httpOptions . path + path ;
4450 }
@@ -52,7 +58,7 @@ module.exports = function(Api) {
5258
5359 params = queryString . stringify ( params ) ;
5460 if ( params ) {
55- if ( requestHasData ( options . method ) ) {
61+ if ( ! alwaysUseGet && requestHasData ( options . method ) ) {
5662 options . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
5763 options . headers [ 'Content-Length' ] = params . length ;
5864 }
@@ -89,7 +95,7 @@ module.exports = function(Api) {
8995 } ) ;
9096 } ) ;
9197 request . on ( 'error' , reject ) ;
92- if ( params && requestHasData ( options . method ) ) {
98+ if ( ! alwaysUseGet && params && requestHasData ( options . method ) ) {
9399 request . write ( params ) ;
94100 }
95101 request . end ( ) ;
Original file line number Diff line number Diff line change @@ -212,4 +212,22 @@ describe('Api.request() method', function() {
212212 var promise = api . request ( path , params , fields , method ) ;
213213 expect ( promise ) . to . be . rejectedWith ( { 'message' : 'fail' } ) . and . notify ( done ) ;
214214 } ) ;
215+
216+ it ( 'should only use get if alwaysUseGet is true' , function ( done ) {
217+ var url = 'http://foobar:8080' ,
218+ path = '/test' ,
219+ method = 'DELETE' ;
220+
221+ nock ( url )
222+ . get ( '/attask/api' + path + '?method=DELETE' )
223+ . reply ( 200 , {
224+ data : {
225+ 'got' : 'ok'
226+ }
227+ } ) ;
228+
229+ var api = new Api ( { url : url , alwaysUseGet : true } ) ;
230+ var promise = api . request ( path , undefined , undefined , method ) ;
231+ expect ( promise ) . to . eventually . deep . equal ( { 'got' : 'ok' } ) . and . notify ( done ) ;
232+ } ) ;
215233} ) ;
You can’t perform that action at this time.
0 commit comments