From a383a36bfc59198739d1c4b4c60db8a679f1e1d2 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 29 May 2015 12:45:02 -0400 Subject: [PATCH] HttpDataApi#request permits null data --- src/dapi/http/HttpDataApi.js | 14 ++++++++++---- test/dapi/http/HttpDataApiTest.js | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/dapi/http/HttpDataApi.js b/src/dapi/http/HttpDataApi.js index 79cf839..e2debb6 100644 --- a/src/dapi/http/HttpDataApi.js +++ b/src/dapi/http/HttpDataApi.js @@ -96,7 +96,8 @@ module.exports = Class( 'HttpDataApi' ) * DATA must be either a string or an object; the latter is treated as a * key-value parameter list, which will have each key and value * individually URI-encoded and converted into a string, delimited by - * ampersands. + * ampersands. `null` may be used to indicate that no data should be + * sent. * * In the event of an error, the first parameter is the error; otherwise, it * is null. The return data shall not be used in the event of an error. @@ -114,6 +115,13 @@ module.exports = Class( 'HttpDataApi' ) */ 'virtual public request': function( data, callback ) { + // null is a good indicator of "I have no intent to send any data"; + // empty strings and objects are not, since those are valid data + if ( data === null ) + { + data = ""; + } + this._validateDataType( data ); this._impl.requestData( @@ -158,9 +166,7 @@ module.exports = Class( 'HttpDataApi' ) { var type = typeof data; - if ( ( data === null ) - || !( ( type === 'string' ) || ( type === 'object' ) ) - ) + if( !( ( type === 'string' ) || ( type === 'object' ) ) ) { throw TypeError( "Data must be a string of raw data or object containing " + diff --git a/test/dapi/http/HttpDataApiTest.js b/test/dapi/http/HttpDataApiTest.js index c1a7dac..a5cdba2 100644 --- a/test/dapi/http/HttpDataApiTest.js +++ b/test/dapi/http/HttpDataApiTest.js @@ -175,11 +175,24 @@ describe( 'HttpDataApi', function() } ); + it( 'accepts null data, converting to empty string', function() + { + expect( function() + { + Sut( dummy_url, 'GET', impl ) + .request( null, function() + { + expect( impl.provided[ 2 ] ).to.equal( "" ); + } ); + } ).to.not.throw( Error ); + } ); + + it( 'rejects all other data types', function() { var sut = Sut( dummy_url, 'GET', impl ); - [ 123, null, Infinity, undefined, NaN, function() {} ] + [ 123, Infinity, undefined, NaN, function() {} ] .forEach( function( data ) { expect( function()