2014-04-18 10:40:09 -04:00
|
|
|
/**
|
|
|
|
* Test case for data transmission over HTTP(S)
|
|
|
|
*
|
2019-08-30 09:41:33 -04:00
|
|
|
* Copyright (C) 2010-2019 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()
|
|
|
|
{
|
2017-08-07 11:29:13 -04:00
|
|
|
var method = 'GET',
|
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 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2017-08-08 11:49:35 -04:00
|
|
|
[
|
|
|
|
// default is urlencoded
|
2015-05-29 09:59:12 -04:00
|
|
|
{
|
2017-08-08 11:49:35 -04:00
|
|
|
enctype: '',
|
|
|
|
method: 'POST',
|
|
|
|
data: { foo: "bar=baz", '&bar': "moo%cow" },
|
|
|
|
expected: 'foo=' + encodeURIComponent( 'bar=baz' ) +
|
|
|
|
'&' + encodeURIComponent( '&bar' ) + '=' +
|
|
|
|
encodeURIComponent( 'moo%cow' )
|
|
|
|
},
|
2015-05-29 09:59:12 -04:00
|
|
|
|
2017-08-08 11:49:35 -04:00
|
|
|
// same as above
|
2015-05-29 09:59:12 -04:00
|
|
|
{
|
2017-08-08 11:49:35 -04:00
|
|
|
enctype: 'application/x-www-form-urlencoded',
|
|
|
|
method: 'POST',
|
|
|
|
data: { foo: "bar=baz", '&bar': "moo%cow" },
|
|
|
|
expected: 'foo=' + encodeURIComponent( 'bar=baz' ) +
|
|
|
|
'&' + encodeURIComponent( '&bar' ) + '=' +
|
|
|
|
encodeURIComponent( 'moo%cow' )
|
|
|
|
},
|
2017-08-07 11:29:13 -04:00
|
|
|
|
2017-08-08 11:49:35 -04:00
|
|
|
// empty string
|
|
|
|
{
|
|
|
|
enctype: 'application/x-www-form-urlencoded',
|
|
|
|
method: 'POST',
|
|
|
|
data: {},
|
|
|
|
expected: "",
|
|
|
|
},
|
2017-08-07 11:29:13 -04:00
|
|
|
|
2017-08-08 11:49:35 -04:00
|
|
|
// json
|
2017-08-07 11:29:13 -04:00
|
|
|
{
|
2017-08-08 11:49:35 -04:00
|
|
|
enctype: 'application/json',
|
|
|
|
method: 'POST',
|
|
|
|
data: { foo: 'bar' },
|
|
|
|
expected: '{"foo":"bar"}',
|
|
|
|
},
|
2017-08-07 11:29:13 -04:00
|
|
|
|
2017-08-08 11:49:35 -04:00
|
|
|
// ignored if GET
|
|
|
|
{
|
|
|
|
enctype: 'application/json',
|
|
|
|
method: 'GET',
|
|
|
|
data: { foo: "bar" },
|
|
|
|
expected: "foo=bar",
|
|
|
|
},
|
|
|
|
].forEach( ( { enctype, method, data, expected }, i ) =>
|
|
|
|
{
|
|
|
|
it( `${method} encodes (${i})`, () =>
|
|
|
|
{
|
|
|
|
Sut( dummy_url, method, impl, enctype )
|
|
|
|
.request( data, _ => {} );
|
2017-08-07 11:29:13 -04:00
|
|
|
|
2017-08-08 11:49:35 -04:00
|
|
|
expect( impl.provided[ 2 ] ).to.equal( expected );
|
2017-08-07 11:29:13 -04:00
|
|
|
} );
|
2015-05-29 09:59:12 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|