File tree Expand file tree Collapse file tree
src/internal/observable/dom Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -600,6 +600,26 @@ describe('ajax', () => {
600600expect ( MockXMLHttpRequest . mostRecent . data ) . to . equal ( '{"🌟":"🚀"}' ) ;
601601} ) ;
602602
603+ it ( 'should send json body not mattered on case-sensitivity of HTTP headers' , ( ) => {
604+ const body = {
605+ hello : 'world'
606+ } ;
607+
608+ const requestObj = {
609+ url : '/flibbertyJibbet' ,
610+ method : '' ,
611+ body : body ,
612+ headers : {
613+ 'cOnTeNt-TyPe' : 'application/json; charset=UTF-8'
614+ }
615+ } ;
616+
617+ ajax ( requestObj ) . subscribe ( ) ;
618+
619+ expect ( MockXMLHttpRequest . mostRecent . url ) . to . equal ( '/flibbertyJibbet' ) ;
620+ expect ( MockXMLHttpRequest . mostRecent . data ) . to . equal ( '{"hello":"world"}' ) ;
621+ } ) ;
622+
603623it ( 'should error if send request throws' , ( done : MochaDone ) => {
604624const expected = new Error ( 'xhr send failure' ) ;
605625
Original file line number Diff line number Diff line change @@ -202,17 +202,18 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
202202const headers = request . headers = request . headers || { } ;
203203
204204// force CORS if requested
205- if ( ! request . crossDomain && ! headers [ 'X-Requested-With' ] ) {
205+ if ( ! request . crossDomain && ! this . getHeader ( headers , 'X-Requested-With' ) ) {
206206headers [ 'X-Requested-With' ] = 'XMLHttpRequest' ;
207207}
208208
209209// ensure content type is set
210- if ( ! ( 'Content-Type' in headers ) && ! ( root . FormData && request . body instanceof root . FormData ) && typeof request . body !== 'undefined' ) {
210+ let contentTypeHeader = this . getHeader ( headers , 'Content-Type' ) ;
211+ if ( ! contentTypeHeader && ! ( root . FormData && request . body instanceof root . FormData ) && typeof request . body !== 'undefined' ) {
211212headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded; charset=UTF-8' ;
212213}
213214
214215// properly serialize body
215- request . body = this . serializeBody ( request . body , request . headers [ 'Content-Type' ] ) ;
216+ request . body = this . serializeBody ( request . body , this . getHeader ( request . headers , 'Content-Type' ) ) ;
216217
217218this . send ( ) ;
218219}
@@ -315,6 +316,16 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
315316}
316317}
317318
319+ private getHeader ( headers : { } , headerName : string ) : any {
320+ for ( let key in headers ) {
321+ if ( key . toLowerCase ( ) === headerName . toLowerCase ( ) ) {
322+ return headers [ key ] ;
323+ }
324+ }
325+
326+ return undefined ;
327+ }
328+
318329private setupEvents ( xhr : XMLHttpRequest , request : AjaxRequest ) {
319330const progressSubscriber = request . progressSubscriber ;
320331
You can’t perform that action at this time.
0 commit comments