2014-04-21 16:14:28 -04:00
|
|
|
/**
|
|
|
|
* Test case for XMLHttpRequest HTTP protocol implementation
|
|
|
|
*
|
2015-05-28 09:03:39 -04:00
|
|
|
* Copyright (C) 2014, 2015 LoVullo Associates, Inc.
|
2014-04-21 16:14:28 -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,
|
|
|
|
HttpImpl = dapi.http.HttpImpl,
|
|
|
|
Sut = dapi.http.XhrHttpImpl,
|
|
|
|
|
|
|
|
DummyXhr = function()
|
|
|
|
{
|
|
|
|
this.open = function()
|
|
|
|
{
|
|
|
|
DummyXhr.args = arguments;
|
|
|
|
};
|
2015-05-29 09:59:12 -04:00
|
|
|
};
|
2014-04-21 16:14:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
describe( 'XhrHttpImpl', function()
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Since ECMAScript does not have return typing, we won't know if the ctor
|
|
|
|
* actually returns an XMLHttpRequest until we try.
|
|
|
|
*/
|
|
|
|
it( 'will accept any constructor', function()
|
|
|
|
{
|
|
|
|
expect( function()
|
|
|
|
{
|
|
|
|
Sut( function() {} );
|
|
|
|
} ).to.not.throw( Error );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'is an HttpImpl', function()
|
|
|
|
{
|
|
|
|
var sut = Sut( function() {} );
|
|
|
|
expect( Class.isA( HttpImpl, sut ) ).to.be.ok;
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( '.requestData', function()
|
|
|
|
{
|
2015-05-29 09:35:04 -04:00
|
|
|
it( 'requests a connection using the given method', function()
|
2014-04-21 16:14:28 -04:00
|
|
|
{
|
2015-05-29 09:35:04 -04:00
|
|
|
var method = 'GET',
|
2014-04-21 16:14:28 -04:00
|
|
|
sut = Sut( DummyXhr );
|
|
|
|
|
2015-05-29 09:59:12 -04:00
|
|
|
sut.requestData( 'http://foo', method, "", function() {} );
|
2014-04-21 16:14:28 -04:00
|
|
|
|
|
|
|
var args = DummyXhr.args;
|
|
|
|
expect( args[ 0 ] ).to.equal( method );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Since the request is asynchronous, we should be polite and not return
|
|
|
|
* errors in two different formats; we will catch it and instead pass it
|
|
|
|
* back via the callback.
|
|
|
|
*/
|
|
|
|
it( 'returns XHR open() errors via callback', function( done )
|
|
|
|
{
|
|
|
|
var e = Error( "Test error" ),
|
|
|
|
Xhr = function()
|
|
|
|
{
|
|
|
|
this.open = function()
|
|
|
|
{
|
|
|
|
throw e;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// should not throw an exception
|
|
|
|
expect( function()
|
|
|
|
{
|
|
|
|
// should instead provide to callback
|
|
|
|
Sut( Xhr )
|
2015-05-29 09:59:12 -04:00
|
|
|
.requestData( 'http://foo', 'GET', "", function( err, data )
|
2014-04-21 16:14:28 -04:00
|
|
|
{
|
|
|
|
expect( err ).to.equal( e );
|
|
|
|
expect( data ).to.equal( null );
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} ).to.not.throw( Error );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
it( 'returns XHR response via callback when no error', function( done )
|
|
|
|
{
|
|
|
|
var retdata = "foobar",
|
|
|
|
StubXhr = createStubXhr();
|
|
|
|
|
|
|
|
StubXhr.prototype.responseText = retdata;
|
|
|
|
StubXhr.prototype.readyState = 4; // done
|
|
|
|
StubXhr.prototype.status = 200; // OK
|
|
|
|
|
|
|
|
Sut( StubXhr )
|
2015-05-29 09:59:12 -04:00
|
|
|
.requestData( 'http://bar', 'GET', "", function( err, resp )
|
2014-04-21 16:14:28 -04:00
|
|
|
{
|
|
|
|
expect( err ).to.equal( null );
|
|
|
|
expect( resp ).to.equal( retdata );
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-05-29 09:35:04 -04:00
|
|
|
describe( 'HTTP method is GET', function()
|
|
|
|
{
|
2015-05-29 09:59:12 -04:00
|
|
|
it( 'appends data to URL', function( done )
|
2015-05-29 09:35:04 -04:00
|
|
|
{
|
|
|
|
var url = 'http://bar',
|
2015-05-29 09:59:12 -04:00
|
|
|
src = "moocow%foocow%",
|
2015-05-29 09:35:04 -04:00
|
|
|
StubXhr = createStubXhr();
|
|
|
|
|
|
|
|
StubXhr.prototype.readyState = 4; // done
|
|
|
|
StubXhr.prototype.status = 200; // OK
|
|
|
|
|
|
|
|
StubXhr.prototype.open = function( _, given_url )
|
|
|
|
{
|
2015-05-29 09:59:12 -04:00
|
|
|
// no additional encoding should be performed; it's
|
|
|
|
// assumed to have already been done
|
2015-05-29 09:35:04 -04:00
|
|
|
expect( given_url ).to.equal(
|
2015-05-29 09:59:12 -04:00
|
|
|
url + '?' + src
|
2015-05-29 09:35:04 -04:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
StubXhr.prototype.send = function( data )
|
|
|
|
{
|
|
|
|
// no posting on GET
|
|
|
|
expect( data ).is.equal( undefined );
|
|
|
|
StubXhr.inst.onreadystatechange();
|
|
|
|
};
|
|
|
|
|
|
|
|
Sut( StubXhr )
|
|
|
|
.requestData( url, 'GET', src, done );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-05-29 09:59:12 -04:00
|
|
|
it( 'leaves URL unaltered when data is empty', function( done )
|
2015-05-29 09:35:04 -04:00
|
|
|
{
|
|
|
|
var url = 'http://bar',
|
|
|
|
StubXhr = createStubXhr();
|
|
|
|
|
|
|
|
StubXhr.prototype.readyState = 4; // done
|
|
|
|
StubXhr.prototype.status = 200; // OK
|
|
|
|
|
|
|
|
StubXhr.prototype.open = function( _, given_url )
|
|
|
|
{
|
|
|
|
// unaltered
|
|
|
|
expect( given_url ).to.equal( url );
|
|
|
|
};
|
|
|
|
|
|
|
|
Sut( StubXhr )
|
2015-05-29 09:59:12 -04:00
|
|
|
.requestData( url, 'GET', "", done );
|
2015-05-29 09:35:04 -04:00
|
|
|
} );
|
|
|
|
|
2016-04-14 12:06:43 -04:00
|
|
|
|
|
|
|
it( 'does not set Content-Type', function( done )
|
|
|
|
{
|
|
|
|
var url = 'http://bar',
|
|
|
|
StubXhr = createStubXhr();
|
|
|
|
|
|
|
|
StubXhr.prototype.readyState = 4; // done
|
|
|
|
StubXhr.prototype.status = 200; // OK
|
|
|
|
|
|
|
|
StubXhr.prototype.setRequestHeader = function()
|
|
|
|
{
|
|
|
|
// warning: this is fragile, if additional headers are
|
|
|
|
// ever set
|
|
|
|
throw Error( 'Headers should not be set on GET' );
|
|
|
|
};
|
|
|
|
|
|
|
|
Sut( StubXhr )
|
|
|
|
.requestData( url, 'GET', "", done );
|
|
|
|
} );
|
2015-05-29 09:35:04 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'HTTP method is not GET', function()
|
|
|
|
{
|
2016-04-14 12:06:43 -04:00
|
|
|
it( 'sends data verbatim as x-www-form-urlencoded', function( done )
|
2015-05-29 09:35:04 -04:00
|
|
|
{
|
|
|
|
var url = 'http://bar',
|
|
|
|
src = "moocow",
|
2016-04-14 12:06:43 -04:00
|
|
|
StubXhr = createStubXhr(),
|
|
|
|
|
|
|
|
open_called = false,
|
|
|
|
send_called = false,
|
|
|
|
header_called = false;
|
2015-05-29 09:35:04 -04:00
|
|
|
|
|
|
|
StubXhr.prototype.readyState = 4; // done
|
|
|
|
StubXhr.prototype.status = 200; // OK
|
|
|
|
|
|
|
|
StubXhr.prototype.open = function( _, given_url )
|
|
|
|
{
|
2016-04-14 12:06:43 -04:00
|
|
|
open_called = true;
|
|
|
|
|
2015-05-29 09:35:04 -04:00
|
|
|
// URL should be unchanged
|
|
|
|
expect( given_url ).to.equal( url );
|
|
|
|
};
|
|
|
|
|
|
|
|
StubXhr.prototype.send = function( data )
|
|
|
|
{
|
2016-04-14 12:06:43 -04:00
|
|
|
send_called = true;
|
|
|
|
|
2015-05-29 09:35:04 -04:00
|
|
|
expect( data ).is.equal( src );
|
|
|
|
StubXhr.inst.onreadystatechange();
|
|
|
|
};
|
|
|
|
|
2016-04-14 12:06:43 -04:00
|
|
|
StubXhr.prototype.setRequestHeader = function( name, val )
|
|
|
|
{
|
|
|
|
header_called = true;
|
|
|
|
|
|
|
|
// warning: this is fragile, if additional headers are
|
|
|
|
// ever set
|
|
|
|
expect( name ).to.equal( 'Content-Type' );
|
|
|
|
expect( val ).to.equal(
|
|
|
|
'application/x-www-form-urlencoded'
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-05-29 09:35:04 -04:00
|
|
|
Sut( StubXhr )
|
2016-04-14 12:06:43 -04:00
|
|
|
.requestData( url, 'POST', src, function()
|
|
|
|
{
|
|
|
|
expect( open_called && send_called && header_called )
|
|
|
|
.to.be.true;
|
|
|
|
done();
|
|
|
|
} );
|
2015-05-29 09:35:04 -04:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-05-12 13:53:46 -04:00
|
|
|
describe( 'if return status code is not successful', function()
|
2014-04-21 16:14:28 -04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* This is the default behavior, but can be changed by overriding
|
|
|
|
* the onLoad method.
|
|
|
|
*/
|
2015-05-28 12:07:28 -04:00
|
|
|
it( 'returns error to callback with status code', function( done )
|
2014-04-21 16:14:28 -04:00
|
|
|
{
|
|
|
|
var StubXhr = createStubXhr();
|
|
|
|
StubXhr.prototype.status = 404;
|
|
|
|
|
|
|
|
Sut( StubXhr )
|
|
|
|
.requestData( 'http://foo', 'GET', '', function( err, _ )
|
|
|
|
{
|
|
|
|
expect( err ).to.be.instanceOf( Error );
|
2015-05-28 12:07:28 -04:00
|
|
|
|
2014-04-21 16:14:28 -04:00
|
|
|
expect( err.message ).to.contain(
|
|
|
|
StubXhr.prototype.status
|
|
|
|
);
|
|
|
|
|
2015-05-28 12:07:28 -04:00
|
|
|
expect( err.status ).to.equal(
|
|
|
|
StubXhr.prototype.status
|
|
|
|
);
|
|
|
|
|
2014-04-21 16:14:28 -04:00
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-05-28 12:07:28 -04:00
|
|
|
it( 'returns response text as output', function( done )
|
2014-04-21 16:14:28 -04:00
|
|
|
{
|
|
|
|
var StubXhr = createStubXhr(),
|
|
|
|
status = 404,
|
|
|
|
reply = 'foobunny';
|
|
|
|
|
|
|
|
StubXhr.prototype.responseText = reply;
|
|
|
|
|
|
|
|
Sut( StubXhr )
|
|
|
|
.requestData( 'http://foo', 'GET', '', function( _, resp )
|
|
|
|
{
|
2015-05-28 12:07:28 -04:00
|
|
|
expect( resp ).to.equal( reply );
|
2014-04-21 16:14:28 -04:00
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-05-12 14:06:02 -04:00
|
|
|
it( 'considers any 2xx status to be successful', function( done )
|
|
|
|
{
|
|
|
|
var StubXhr = createStubXhr();
|
|
|
|
StubXhr.prototype.status = 250;
|
|
|
|
|
|
|
|
Sut( StubXhr )
|
|
|
|
.requestData( 'http://foo', 'GET', '', function( err, _ )
|
|
|
|
{
|
|
|
|
expect( err ).to.equal( null );
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-05-12 13:53:46 -04:00
|
|
|
it( 'allows overriding notion of success/failure', function( done )
|
|
|
|
{
|
|
|
|
var chk = 12345;
|
|
|
|
|
|
|
|
// succeed on CHK
|
|
|
|
var StubXhr = createStubXhr();
|
|
|
|
StubXhr.prototype.status = chk;
|
|
|
|
|
|
|
|
Sut.extend(
|
|
|
|
{
|
|
|
|
'override protected isSuccessful': function( status )
|
|
|
|
{
|
|
|
|
return status === chk;
|
|
|
|
},
|
|
|
|
} )( StubXhr )
|
|
|
|
.requestData( 'http://foo', 'GET', '', function( err, resp )
|
|
|
|
{
|
|
|
|
expect( err ).to.equal( null );
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-05-12 14:24:24 -04:00
|
|
|
it( 'allows customizing error', function( done )
|
|
|
|
{
|
|
|
|
var _self = this,
|
|
|
|
chk = {};
|
|
|
|
|
|
|
|
var StubXhr = createStubXhr();
|
|
|
|
StubXhr.prototype.status = 404;
|
|
|
|
|
|
|
|
Sut.extend(
|
|
|
|
{
|
|
|
|
'override protected serveError': function( req, callback )
|
|
|
|
{
|
|
|
|
var e = Error( 'foobunny' );
|
|
|
|
e.foo = true;
|
|
|
|
|
|
|
|
expect( req ).to.be.an.instanceOf( StubXhr );
|
|
|
|
|
|
|
|
callback( e, chk );
|
|
|
|
},
|
|
|
|
} )( StubXhr )
|
|
|
|
.requestData( 'http://foo', 'GET', '', function( err, resp )
|
|
|
|
{
|
|
|
|
expect( ( err || {} ).foo ).to.be.ok;
|
|
|
|
expect( resp ).to.equal( chk );
|
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2014-04-21 16:14:28 -04:00
|
|
|
it( 'returns self', function()
|
|
|
|
{
|
|
|
|
var sut = Sut( function() {} ),
|
|
|
|
ret = sut.requestData(
|
2015-05-29 09:59:12 -04:00
|
|
|
'http://foo', 'GET', "", function() {}
|
2014-04-21 16:14:28 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
expect( ret ).to.equal( sut );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
function createStubXhr()
|
|
|
|
{
|
|
|
|
var StubXhr = function()
|
|
|
|
{
|
|
|
|
StubXhr.inst = this;
|
|
|
|
};
|
|
|
|
|
|
|
|
StubXhr.prototype = {
|
|
|
|
onreadystatechange: null,
|
|
|
|
responseText: '',
|
|
|
|
readyState: 4, // don,
|
|
|
|
status: 200, // O,
|
|
|
|
|
|
|
|
open: function() {},
|
|
|
|
send: function( data )
|
|
|
|
{
|
|
|
|
this.onreadystatechange();
|
2016-04-14 12:06:43 -04:00
|
|
|
},
|
|
|
|
setRequestHeader: function() {},
|
2014-04-21 16:14:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return StubXhr;
|
|
|
|
}
|
|
|
|
|