Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMike Gerwitz <gerwitm@lovullo.com>2015-05-29 09:35:04 -0400
committerMike Gerwitz <gerwitm@lovullo.com>2015-05-29 09:35:21 -0400
commitf122d8593887cb81e429f9f6eaf1e523a16811e4 (patch)
tree89ab36e2b406e16a471441589fc9b4346d727db4 /test
parent881e740bb9de26e9d62b9806560c833efddf397a (diff)
downloadliza-f122d8593887cb81e429f9f6eaf1e523a16811e4.tar.gz
liza-f122d8593887cb81e429f9f6eaf1e523a16811e4.tar.bz2
liza-f122d8593887cb81e429f9f6eaf1e523a16811e4.zip
XhrHttpImpl modify URL for GET request params and encode key-value
Some of this may be more appropriate to move out of XhrHttpImpl into something like HttpDataApi...
Diffstat (limited to 'test')
-rw-r--r--test/dapi/http/XhrHttpImplTest.js170
1 files changed, 155 insertions, 15 deletions
diff --git a/test/dapi/http/XhrHttpImplTest.js b/test/dapi/http/XhrHttpImplTest.js
index 61a26fb..0a6e780 100644
--- a/test/dapi/http/XhrHttpImplTest.js
+++ b/test/dapi/http/XhrHttpImplTest.js
@@ -31,7 +31,9 @@ var dapi = require( '../../../' ).dapi,
{
DummyXhr.args = arguments;
};
- };
+ },
+
+ _void = function() {};
describe( 'XhrHttpImpl', function()
@@ -58,18 +60,15 @@ describe( 'XhrHttpImpl', function()
describe( '.requestData', function()
{
- it( 'requests a connection using the given url and method', function()
+ it( 'requests a connection using the given method', function()
{
- var url = 'http://foonugget',
- method = 'GET',
+ var method = 'GET',
sut = Sut( DummyXhr );
- sut.requestData( url, method, {}, function() {} );
+ sut.requestData( 'http://foo', method, {}, function() {} );
var args = DummyXhr.args;
expect( args[ 0 ] ).to.equal( method );
- expect( args[ 1 ] ).to.equal( url );
- expect( args[ 1 ] ).to.be.ok; // async
} );
@@ -107,21 +106,14 @@ describe( 'XhrHttpImpl', function()
it( 'returns XHR response via callback when no error', function( done )
{
var retdata = "foobar",
- src = "moocow",
StubXhr = createStubXhr();
StubXhr.prototype.responseText = retdata;
StubXhr.prototype.readyState = 4; // done
StubXhr.prototype.status = 200; // OK
- StubXhr.prototype.send = function( data )
- {
- expect( data ).is.equal( src );
- StubXhr.inst.onreadystatechange();
- };
-
Sut( StubXhr )
- .requestData( 'http://bar', 'GET', src, function( err, resp )
+ .requestData( 'http://bar', 'GET', {}, function( err, resp )
{
expect( err ).to.equal( null );
expect( resp ).to.equal( retdata );
@@ -130,6 +122,154 @@ describe( 'XhrHttpImpl', function()
} );
+ describe( 'HTTP method is GET', function()
+ {
+ it( 'appends encoded non-obj data to URL', function( done )
+ {
+ var url = 'http://bar',
+ src = "moocow %foocow%",
+ StubXhr = createStubXhr();
+
+ StubXhr.prototype.readyState = 4; // done
+ StubXhr.prototype.status = 200; // OK
+
+ StubXhr.prototype.open = function( _, given_url )
+ {
+ expect( given_url ).to.equal(
+ url + '?' + encodeURI( src )
+ );
+ };
+
+ StubXhr.prototype.send = function( data )
+ {
+ // no posting on GET
+ expect( data ).is.equal( undefined );
+ StubXhr.inst.onreadystatechange();
+ };
+
+ Sut( StubXhr )
+ .requestData( url, 'GET', src, done );
+ } );
+
+
+ it( 'appends encoded key-val data to URL', function( done )
+ {
+ var url = 'http://bar',
+ src = { foo: "bar=baz", bar: "moo%cow" },
+ StubXhr = createStubXhr();
+
+ StubXhr.prototype.readyState = 4; // done
+ StubXhr.prototype.status = 200; // OK
+
+ StubXhr.prototype.open = function( _, given_url )
+ {
+ // XXX: the docblock for requestData says "undefined
+ // order", but fundamentally we need to pass in our own
+ // encoder
+ expect( given_url ).to.equal(
+ url + '?foo=' + encodeURIComponent( src.foo ) +
+ '&bar=' + encodeURIComponent( src.bar )
+ );
+ };
+
+ StubXhr.prototype.send = function( data )
+ {
+ // no posting on GET
+ expect( data ).is.equal( undefined );
+ StubXhr.inst.onreadystatechange();
+ };
+
+ Sut( StubXhr )
+ .requestData( url, 'GET', src, done );
+ } );
+
+
+ it( 'leaves URL unaltered with empty data', function( done )
+ {
+ 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 )
+ .requestData( url, 'GET', undefined, _void )
+ .requestData( url, 'GET', null, _void )
+ .requestData( url, 'GET', "", _void )
+ .requestData( url, 'GET', {}, done );
+ } );
+
+ } );
+
+
+ describe( 'HTTP method is not GET', function()
+ {
+ it( 'posts non-object data verbatim', function( done )
+ {
+ var url = 'http://bar',
+ src = "moocow",
+ StubXhr = createStubXhr();
+
+ StubXhr.prototype.readyState = 4; // done
+ StubXhr.prototype.status = 200; // OK
+
+ StubXhr.prototype.open = function( _, given_url )
+ {
+ // URL should be unchanged
+ expect( given_url ).to.equal( url );
+ };
+
+ StubXhr.prototype.send = function( data )
+ {
+ expect( data ).is.equal( src );
+ StubXhr.inst.onreadystatechange();
+ };
+
+ Sut( StubXhr )
+ .requestData( url, 'POST', src, done );
+ } );
+
+
+ it( 'encodes key-value data', function( done )
+ {
+ var url = 'http://bar',
+ src = { foo: "bar=baz", bar: "moo%cow" },
+ 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 );
+ };
+
+ StubXhr.prototype.send = function( data )
+ {
+ // XXX: the docblock for requestData says "undefined
+ // order", but fundamentally we need to pass in our own
+ // encoder
+ expect( data ).to.equal(
+ 'foo=' + encodeURIComponent( src.foo ) +
+ '&bar=' + encodeURIComponent( src.bar )
+ );
+
+ StubXhr.inst.onreadystatechange();
+ };
+
+ Sut( StubXhr )
+ .requestData( url, 'POST', src, done );
+ } );
+ } );
+
+
describe( 'if return status code is not successful', function()
{
/**