From 020ca01458ea69c5b7f90d7715862eff50a319dc Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 7 Aug 2017 11:29:13 -0400 Subject: [PATCH] HttpDataApi: Only serialize JSON on POST * src/dapi/http/HttpDataApi.js (_encodeKeys): JSON.stringify on POST. * test/dapi/http/HttpDataApiTest.js: Update tests accordingly. Add POST test. --- src/dapi/http/HttpDataApi.js | 5 +++++ test/dapi/http/HttpDataApiTest.js | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/dapi/http/HttpDataApi.js b/src/dapi/http/HttpDataApi.js index a72057a..48d498b 100644 --- a/src/dapi/http/HttpDataApi.js +++ b/src/dapi/http/HttpDataApi.js @@ -206,6 +206,11 @@ module.exports = Class( 'HttpDataApi' ) { var uri = ''; + if ( this._method === 'POST' ) + { + return JSON.stringify( obj ); + } + // ES3 support for ( var key in obj ) { diff --git a/test/dapi/http/HttpDataApiTest.js b/test/dapi/http/HttpDataApiTest.js index e402ae4..8e46ca3 100644 --- a/test/dapi/http/HttpDataApiTest.js +++ b/test/dapi/http/HttpDataApiTest.js @@ -105,7 +105,7 @@ describe( 'HttpDataApi', function() */ it( 'delegates to provided HTTP implementation', function() { - var method = 'POST', + var method = 'GET', data = "ribbit", c = function() {}; @@ -127,7 +127,7 @@ describe( 'HttpDataApi', function() { it( 'converts data into encoded string', function() { - var method = 'POST', + var method = 'GET', data = { foo: "bar=baz", '&bar': "moo%cow" }, c = function() {}; @@ -143,7 +143,7 @@ describe( 'HttpDataApi', function() it( 'with no keys, results in empty string', function() { - var method = 'POST', + var method = 'GET', data = {}, c = function() {}; @@ -151,6 +151,20 @@ describe( 'HttpDataApi', function() expect( impl.provided[ 2 ] ).to.equal( "" ); } ); + + + it( 'encodes JSON on POST', () => + { + var method = 'POST', + data = { foo: 'bar' }, + c = () => {}; + + Sut( dummy_url, method, impl ).request( data, c ); + + expect( impl.provided[ 2 ] ).to.equal( + JSON.stringify( data ) + ); + } ); } );