2014-04-24 14:16:12 -04:00
|
|
|
/**
|
|
|
|
* Test case for JSON formatting of API result
|
|
|
|
*
|
2015-05-27 15:24:38 -04:00
|
|
|
* Copyright (C) 2014, 2015 LoVullo Associates, Inc.
|
2014-04-24 14:16:12 -04:00
|
|
|
*
|
|
|
|
* This file is part of the Liza Data Collection Framework
|
|
|
|
*
|
|
|
|
* Liza is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var dapi = require( '../../../' ).dapi,
|
|
|
|
expect = require( 'chai' ).expect,
|
|
|
|
Class = require( 'easejs' ).Class,
|
|
|
|
DataApi = dapi.DataApi,
|
2014-04-24 15:59:31 -04:00
|
|
|
Sut = dapi.format.JsonResponse;
|
2014-04-24 14:16:12 -04:00
|
|
|
|
|
|
|
|
2014-04-24 15:59:31 -04:00
|
|
|
describe( 'dapi.format.JsonRepsonse trait', function()
|
2014-04-24 14:16:12 -04:00
|
|
|
{
|
|
|
|
describe( '.request', function()
|
|
|
|
{
|
|
|
|
it( 'passes data to encapsulated DataApi', function()
|
|
|
|
{
|
2014-04-24 15:59:31 -04:00
|
|
|
var stubs = _createStubbedDapi( null, '0' ),
|
2014-04-24 14:16:12 -04:00
|
|
|
expected = {};
|
|
|
|
|
2014-04-24 15:59:31 -04:00
|
|
|
stubs.request( expected, function() {} );
|
2014-04-24 14:16:12 -04:00
|
|
|
expect( stubs.given ).to.equal( expected );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'converts response to JSON', function( done )
|
|
|
|
{
|
|
|
|
var raw = '{"foo": "bar"}';
|
2014-04-24 15:59:31 -04:00
|
|
|
|
|
|
|
_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();
|
|
|
|
} );
|
2014-04-24 14:16:12 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-05-28 09:42:00 -04:00
|
|
|
describe( 'when JSON parsing fails', function()
|
2014-04-24 14:16:12 -04:00
|
|
|
{
|
2015-05-28 09:42:00 -04:00
|
|
|
it( 'returns error', function( done )
|
|
|
|
{
|
|
|
|
_createStubbedDapi( null, 'ERR' )
|
|
|
|
.request( '', function( err, data )
|
|
|
|
{
|
2015-05-28 09:43:20 -04:00
|
|
|
expect( err ).to.be.instanceOf( SyntaxError );
|
2015-05-28 09:42:00 -04:00
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'provides bad text as object.text', function( done )
|
|
|
|
{
|
|
|
|
var text = 'NOT JSON';
|
|
|
|
|
|
|
|
_createStubbedDapi( null, text )
|
|
|
|
.request( '', function( err, data )
|
|
|
|
{
|
|
|
|
expect( data ).to.be.a( 'object' );
|
|
|
|
expect( data.text ).to.equal( text );
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
2014-04-24 14:16:12 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-05-28 12:16:02 -04:00
|
|
|
describe( 'on request error from supertype', function()
|
2014-04-24 14:16:12 -04:00
|
|
|
{
|
2015-05-28 12:16:02 -04:00
|
|
|
it( 'attempts to format output as JSON', function( done )
|
|
|
|
{
|
|
|
|
var chk = '{"foo": "bar"}';
|
2014-04-24 14:16:12 -04:00
|
|
|
|
2015-05-28 12:16:02 -04:00
|
|
|
_createStubbedDapi( null, chk )
|
|
|
|
.request( '', function( _, data )
|
|
|
|
{
|
|
|
|
expect( data ).to.be.a( 'object' );
|
|
|
|
expect( data.foo ).to.equal( "bar" );
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'proxies error when JSON output valid', function( done )
|
|
|
|
{
|
|
|
|
var e = Error( 'foo' );
|
|
|
|
|
|
|
|
_createStubbedDapi( e, '{}' )
|
|
|
|
.request( '', function( err, _ )
|
|
|
|
{
|
|
|
|
expect( err ).to.equal( e );
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'produces both errors on bad JSON output', function( done )
|
|
|
|
{
|
|
|
|
var e = Error( 'foo' );
|
|
|
|
|
|
|
|
_createStubbedDapi( e, 'BAD JSON' )
|
|
|
|
.request( '', function( err, _ )
|
|
|
|
{
|
|
|
|
// the main error should indicate both
|
|
|
|
expect( err ).to.be.instanceOf( Error );
|
|
|
|
|
|
|
|
// and we should provide references to both
|
|
|
|
expect( err.list[ 0 ] ).to.equal( e );
|
|
|
|
expect( err.list[ 1 ] ).to.be.instanceOf(
|
|
|
|
SyntaxError
|
|
|
|
);
|
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
2014-04-24 14:16:12 -04:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2014-04-24 15:59:31 -04:00
|
|
|
function _createStubbedDapi( err, resp )
|
2014-04-24 14:16:12 -04:00
|
|
|
{
|
|
|
|
return Class.implement( DataApi ).extend(
|
|
|
|
{
|
|
|
|
given: null,
|
|
|
|
|
2014-04-24 15:59:31 -04:00
|
|
|
'virtual public request': function( data, callback )
|
2014-04-24 14:16:12 -04:00
|
|
|
{
|
|
|
|
this.given = data;
|
|
|
|
callback( err, resp );
|
|
|
|
}
|
2014-04-24 15:59:31 -04:00
|
|
|
} ).use( Sut )();
|
2014-04-24 14:16:12 -04:00
|
|
|
}
|
|
|
|
|