Add x-www-form-urlencoded header for XhrHttpImpl
It might be the case that we may want to post raw data in the future; in fact, I can guarantee it. We'll cross that bridge when we come to it. * src/dapi/http/XhrHttpImpl.js (openRequest): Set ContentType header on POST * test/dapi/http/XhrHttpImplTest.js: Modified accordinglymaster
parent
413df53f4f
commit
a6115018d2
|
@ -177,6 +177,14 @@ module.exports = Class( 'XhrHttpImpl' )
|
||||||
{
|
{
|
||||||
// alway async
|
// alway async
|
||||||
req.open( method, url, true );
|
req.open( method, url, true );
|
||||||
|
|
||||||
|
if ( method === 'POST' )
|
||||||
|
{
|
||||||
|
req.setRequestHeader(
|
||||||
|
'Content-Type',
|
||||||
|
'application/x-www-form-urlencoded'
|
||||||
|
);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -170,34 +170,79 @@ describe( 'XhrHttpImpl', function()
|
||||||
.requestData( url, 'GET', "", done );
|
.requestData( url, 'GET', "", done );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
|
||||||
|
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 );
|
||||||
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
|
||||||
describe( 'HTTP method is not GET', function()
|
describe( 'HTTP method is not GET', function()
|
||||||
{
|
{
|
||||||
it( 'sends data verbatim', function( done )
|
it( 'sends data verbatim as x-www-form-urlencoded', function( done )
|
||||||
{
|
{
|
||||||
var url = 'http://bar',
|
var url = 'http://bar',
|
||||||
src = "moocow",
|
src = "moocow",
|
||||||
StubXhr = createStubXhr();
|
StubXhr = createStubXhr(),
|
||||||
|
|
||||||
|
open_called = false,
|
||||||
|
send_called = false,
|
||||||
|
header_called = false;
|
||||||
|
|
||||||
StubXhr.prototype.readyState = 4; // done
|
StubXhr.prototype.readyState = 4; // done
|
||||||
StubXhr.prototype.status = 200; // OK
|
StubXhr.prototype.status = 200; // OK
|
||||||
|
|
||||||
StubXhr.prototype.open = function( _, given_url )
|
StubXhr.prototype.open = function( _, given_url )
|
||||||
{
|
{
|
||||||
|
open_called = true;
|
||||||
|
|
||||||
// URL should be unchanged
|
// URL should be unchanged
|
||||||
expect( given_url ).to.equal( url );
|
expect( given_url ).to.equal( url );
|
||||||
};
|
};
|
||||||
|
|
||||||
StubXhr.prototype.send = function( data )
|
StubXhr.prototype.send = function( data )
|
||||||
{
|
{
|
||||||
|
send_called = true;
|
||||||
|
|
||||||
expect( data ).is.equal( src );
|
expect( data ).is.equal( src );
|
||||||
StubXhr.inst.onreadystatechange();
|
StubXhr.inst.onreadystatechange();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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'
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
Sut( StubXhr )
|
Sut( StubXhr )
|
||||||
.requestData( url, 'POST', src, done );
|
.requestData( url, 'POST', src, function()
|
||||||
|
{
|
||||||
|
console.log( open_called, send_called, header_called );
|
||||||
|
expect( open_called && send_called && header_called )
|
||||||
|
.to.be.true;
|
||||||
|
done();
|
||||||
|
} );
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
@ -346,7 +391,8 @@ function createStubXhr()
|
||||||
send: function( data )
|
send: function( data )
|
||||||
{
|
{
|
||||||
this.onreadystatechange();
|
this.onreadystatechange();
|
||||||
}
|
},
|
||||||
|
setRequestHeader: function() {},
|
||||||
};
|
};
|
||||||
|
|
||||||
return StubXhr;
|
return StubXhr;
|
||||||
|
|
Loading…
Reference in New Issue