JsonDataApi=>JsonResponse trait
This cuts down on the code significantly and makes more sense, since we would not want to veil the API of any classes that want to use this.master
parent
0e9c511117
commit
b4ef6439e9
|
@ -19,51 +19,30 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
var Class = require( 'easejs' ).Class,
|
||||
var Trait = require( 'easejs' ).Trait,
|
||||
DataApi = require( '../DataApi' );
|
||||
|
||||
|
||||
/**
|
||||
* Processes DataApi return data as JSON
|
||||
*/
|
||||
module.exports = Class( 'JsonDataApi' )
|
||||
module.exports = Trait( 'JsonResponse' )
|
||||
.implement( DataApi )
|
||||
.extend(
|
||||
{
|
||||
/**
|
||||
* DataAPI to decorate
|
||||
* @type {DataApi}
|
||||
*/
|
||||
'private _dapi': null,
|
||||
|
||||
|
||||
/**
|
||||
* Decorate provided DataAPI to parse response data as JSON
|
||||
* Processes response as JSON
|
||||
*
|
||||
* @param {DataApi} dapi DataApi to decorate
|
||||
*/
|
||||
__construct: function( dapi )
|
||||
{
|
||||
if ( !( Class.isA( DataApi, dapi ) ) )
|
||||
{
|
||||
throw TypeError( "Expecting DataApi" );
|
||||
}
|
||||
|
||||
this._dapi = dapi;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Proxies request to encapsulated DataApi and parses the result as JSON
|
||||
* Will return an error if the response is not valid JSON.
|
||||
*
|
||||
* @param {string} data binary data to transmit
|
||||
* @param {function(?Error,*)} callback continuation upon reply
|
||||
*
|
||||
* @return {DataApi} self
|
||||
*/
|
||||
'public request': function( data, callback )
|
||||
'abstract override public request': function( data, callback )
|
||||
{
|
||||
this._dapi.request( data, function( err, resp )
|
||||
this.__super( data, function( err, resp )
|
||||
{
|
||||
if ( err !== null )
|
||||
{
|
|
@ -23,37 +23,19 @@ var dapi = require( '../../../' ).dapi,
|
|||
expect = require( 'chai' ).expect,
|
||||
Class = require( 'easejs' ).Class,
|
||||
DataApi = dapi.DataApi,
|
||||
Sut = dapi.format.JsonDataApi;
|
||||
Sut = dapi.format.JsonResponse;
|
||||
|
||||
|
||||
describe( 'JsonDataApi', function()
|
||||
describe( 'dapi.format.JsonRepsonse trait', function()
|
||||
{
|
||||
it( 'errors if not provided a DataApi', function()
|
||||
{
|
||||
expect( function()
|
||||
{
|
||||
Sut( {} );
|
||||
} ).to.throw( TypeError, 'DataApi' );
|
||||
} );
|
||||
|
||||
|
||||
it( 'accepts a DataApi', function()
|
||||
{
|
||||
expect( function()
|
||||
{
|
||||
Sut( _createStubDapi( null, '' ) );
|
||||
} ).to.not.throw( TypeError );
|
||||
} );
|
||||
|
||||
|
||||
describe( '.request', function()
|
||||
{
|
||||
it( 'passes data to encapsulated DataApi', function()
|
||||
{
|
||||
var stubs = _createStubDapi( null, '0' ),
|
||||
var stubs = _createStubbedDapi( null, '0' ),
|
||||
expected = {};
|
||||
|
||||
Sut( stubs ).request( expected, function() {} );
|
||||
stubs.request( expected, function() {} );
|
||||
expect( stubs.given ).to.equal( expected );
|
||||
} );
|
||||
|
||||
|
@ -61,19 +43,21 @@ describe( 'JsonDataApi', function()
|
|||
it( 'converts response to JSON', function( done )
|
||||
{
|
||||
var raw = '{"foo": "bar"}';
|
||||
Sut( _createStubDapi( null, raw ) ).request( '', function( err, data )
|
||||
{
|
||||
// should have been converted to JSON
|
||||
expect( data ).to.deep.equal( { foo: 'bar' } );
|
||||
expect( err ).to.equal( null );
|
||||
done();
|
||||
} );
|
||||
|
||||
_createStubbedDapi( null, raw )
|
||||
.request( '', function( err, data )
|
||||
{
|
||||
// should have been converted to JSON
|
||||
expect( data ).to.deep.equal( { foo: 'bar' } );
|
||||
expect( err ).to.equal( null );
|
||||
done();
|
||||
} );
|
||||
} );
|
||||
|
||||
|
||||
it( 'returns error if JSON parsing fails', function( done )
|
||||
{
|
||||
Sut( _createStubDapi( null, 'ERR' ) )
|
||||
_createStubbedDapi( null, 'ERR' )
|
||||
.request( '', function( err, data )
|
||||
{
|
||||
expect( err ).to.be.instanceOf( Error );
|
||||
|
@ -87,7 +71,7 @@ describe( 'JsonDataApi', function()
|
|||
{
|
||||
var e = Error( 'foo' );
|
||||
|
||||
Sut( _createStubDapi( e, '0' ) )
|
||||
_createStubbedDapi( e, '0' )
|
||||
.request( '', function( err, data )
|
||||
{
|
||||
// data should also be cleared out
|
||||
|
@ -100,17 +84,17 @@ describe( 'JsonDataApi', function()
|
|||
} );
|
||||
|
||||
|
||||
function _createStubDapi( err, resp )
|
||||
function _createStubbedDapi( err, resp )
|
||||
{
|
||||
return Class.implement( DataApi ).extend(
|
||||
{
|
||||
given: null,
|
||||
|
||||
request: function( data, callback )
|
||||
'virtual public request': function( data, callback )
|
||||
{
|
||||
this.given = data;
|
||||
callback( err, resp );
|
||||
}
|
||||
} )();
|
||||
} ).use( Sut )();
|
||||
}
|
||||
|
Loading…
Reference in New Issue