2014-04-18 10:40:09 -04:00
|
|
|
/**
|
|
|
|
* Test case for data transmission over HTTP(S)
|
|
|
|
*
|
2017-06-08 14:46:51 -04:00
|
|
|
* Copyright (C) 2014, 2015 R-T Specialty, LLC.
|
2014-04-18 10:40:09 -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,
|
|
|
|
Sut = dapi.http.HttpDataApi,
|
|
|
|
|
|
|
|
dummy_url = 'http://foo',
|
|
|
|
dummy_impl = Class
|
|
|
|
.implement( dapi.http.HttpImpl )
|
2017-06-28 14:54:34 -04:00
|
|
|
.extend(
|
|
|
|
{
|
|
|
|
requestData: function( _, __, ___, ____ ) {},
|
|
|
|
setOptions() {},
|
|
|
|
} )(),
|
2014-04-18 10:40:09 -04:00
|
|
|
|
|
|
|
dummy_sut = Sut( dummy_url, 'GET', dummy_impl );
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'HttpDataApi', function()
|
|
|
|
{
|
|
|
|
it( 'is a DataApi', function()
|
|
|
|
{
|
|
|
|
expect( Class.isA( dapi.DataApi, dummy_sut ) ).to.be.ok;
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'permits RFC 2616 HTTP methods', function()
|
|
|
|
{
|
|
|
|
var m = [ 'GET', 'POST', 'PUT', 'HEAD', 'OPTIONS', 'DELETE', 'TRACE' ];
|
|
|
|
|
|
|
|
m.forEach( function( method )
|
|
|
|
{
|
|
|
|
expect( function()
|
|
|
|
{
|
|
|
|
Sut( dummy_url, method, dummy_impl );
|
|
|
|
} ).to.not.throw( Error );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'does not permit non-RFC-2616 HTTP methods', function()
|
|
|
|
{
|
|
|
|
expect( function()
|
|
|
|
{
|
|
|
|
Sut( dummy_url, 'FOO', dummy_impl );
|
|
|
|
} ).to.throw( Error, 'FOO' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'rejects non-HttpImpl objects', function()
|
|
|
|
{
|
|
|
|
expect( function()
|
|
|
|
{
|
|
|
|
Sut( dummy_url, 'GET', {} );
|
|
|
|
} ).to.throw( TypeError, 'HttpImpl' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( '.request', function()
|
|
|
|
{
|
|
|
|
var impl = Class( 'StubHttpImpl' )
|
|
|
|
.implement( dapi.http.HttpImpl )
|
|
|
|
.extend(
|
|
|
|
{
|
|
|
|
provided: [],
|
|
|
|
data: "",
|
|
|
|
err: null,
|
|
|
|
|
|
|
|
requestData: function( url, method, data, c )
|
|
|
|
{
|
|
|
|
this.provided = arguments;
|
|
|
|
c( this.err, this.data );
|
2017-06-28 14:54:34 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
setOptions() {},
|
2014-04-18 10:40:09 -04:00
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The actual request is performed by some underling implementation.
|
|
|
|
* This additional level of indirection allows the general concept of an
|
|
|
|
* "HTTP Data API" to vary from an underyling HTTP protocol
|
|
|
|
* implementation; they are separate concerns, although the distinction
|
|
|
|
* may seem subtle.
|
|
|
|
*/
|
|
|
|
it( 'delegates to provided HTTP implementation', function()
|
|
|
|
{
|
|
|
|
var method = 'POST',
|
2015-05-29 09:59:12 -04:00
|
|
|
data = "ribbit",
|
2014-04-18 10:40:09 -04:00
|
|
|
c = function() {};
|
|
|
|
|
|
|
|
Sut( dummy_url, method, impl ).request( data, c );
|
|
|
|
|
|
|
|
var provided = impl.provided;
|
|
|
|
expect( provided[ 0 ] ).to.equal( dummy_url );
|
|
|
|
expect( provided[ 1 ] ).to.equal( method );
|
|
|
|
expect( provided[ 2 ] ).to.equal( data );
|
|
|
|
expect( provided[ 3 ] ).to.equal( c );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-05-29 09:59:12 -04:00
|
|
|
/**
|
|
|
|
* It's nice to do this for the HttpImpl so that they don't have to
|
|
|
|
* worry about the proper way to handle it, or duplicate the logic.
|
|
|
|
*/
|
|
|
|
describe( 'given key-value data', function()
|
|
|
|
{
|
|
|
|
it( 'converts data into encoded string', function()
|
|
|
|
{
|
|
|
|
var method = 'POST',
|
|
|
|
data = { foo: "bar=baz", '&bar': "moo%cow" },
|
|
|
|
c = function() {};
|
|
|
|
|
|
|
|
Sut( dummy_url, method, impl ).request( data, c );
|
|
|
|
|
|
|
|
expect( impl.provided[ 2 ] ).to.equal(
|
|
|
|
'foo=' + encodeURIComponent( data.foo ) +
|
|
|
|
'&' + encodeURIComponent( '&bar' ) + '=' +
|
|
|
|
encodeURIComponent( data[ '&bar' ] )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'with no keys, results in empty string', function()
|
|
|
|
{
|
|
|
|
var method = 'POST',
|
|
|
|
data = {},
|
|
|
|
c = function() {};
|
|
|
|
|
|
|
|
Sut( dummy_url, method, impl ).request( data, c );
|
|
|
|
|
|
|
|
expect( impl.provided[ 2 ] ).to.equal( "" );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2014-04-18 10:40:09 -04:00
|
|
|
/**
|
|
|
|
* Method chaining
|
|
|
|
*/
|
|
|
|
it( 'returns self', function()
|
|
|
|
{
|
|
|
|
var sut = Sut( dummy_url, 'GET', impl ),
|
|
|
|
ret = sut.request( "", function() {} );
|
|
|
|
|
|
|
|
expect( ret ).to.equal( sut );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* String requests are intended to be raw messages, whereas objects are
|
|
|
|
* treated as key-value params.
|
|
|
|
*/
|
|
|
|
it( 'accepts string and object data', function()
|
|
|
|
{
|
|
|
|
expect( function()
|
|
|
|
{
|
|
|
|
Sut( dummy_url, 'GET', impl )
|
|
|
|
.request( "", function() {} ) // string
|
|
|
|
.request( {}, function() {} ); // object
|
|
|
|
} ).to.not.throw( Error );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-05-29 12:45:02 -04:00
|
|
|
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 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2014-04-18 10:40:09 -04:00
|
|
|
it( 'rejects all other data types', function()
|
|
|
|
{
|
|
|
|
var sut = Sut( dummy_url, 'GET', impl );
|
|
|
|
|
2015-05-29 12:45:02 -04:00
|
|
|
[ 123, Infinity, undefined, NaN, function() {} ]
|
2014-04-18 10:40:09 -04:00
|
|
|
.forEach( function( data )
|
|
|
|
{
|
|
|
|
expect( function()
|
|
|
|
{
|
|
|
|
sut.request( data, function() {} );
|
|
|
|
} ).to.throw( TypeError );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'returns error provided by HTTP implementation', function( done )
|
|
|
|
{
|
|
|
|
impl.err = Error( "Test impl error" );
|
|
|
|
Sut( dummy_url, 'GET', impl ).request( "", function( err, resp )
|
|
|
|
{
|
|
|
|
expect( err ).to.equal( impl.err );
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'returns response provided by HTTP implementation', function( done )
|
|
|
|
{
|
|
|
|
impl.data = {};
|
|
|
|
Sut( dummy_url, 'GET', impl ).request( "", function( err, resp )
|
|
|
|
{
|
|
|
|
expect( resp ).to.equal( impl.data );
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|