@@ -937,7 +937,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
937937 }
938938
939939 // TODO(vojta): change params to: method, url, data, headers, callback
940- function $httpBackend ( method , url , data , callback , headers ) {
940+ function $httpBackend ( method , url , data , callback , headers , timeout ) {
941941 var xhr = new MockXhr ( ) ,
942942 expectation = expectations [ 0 ] ,
943943 wasExpected = false ;
@@ -948,6 +948,28 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
948948 : angular . toJson ( data ) ;
949949 }
950950
951+ function wrapResponse ( wrapped ) {
952+ if ( ! $browser && timeout && timeout . then ) timeout . then ( handleTimeout ) ;
953+
954+ return handleResponse ;
955+
956+ function handleResponse ( ) {
957+ var response = wrapped . response ( method , url , data , headers ) ;
958+ xhr . $$respHeaders = response [ 2 ] ;
959+ callback ( response [ 0 ] , response [ 1 ] , xhr . getAllResponseHeaders ( ) ) ;
960+ }
961+
962+ function handleTimeout ( ) {
963+ for ( var i = 0 , ii = responses . length ; i < ii ; i ++ ) {
964+ if ( responses [ i ] === handleResponse ) {
965+ responses . splice ( i , 1 ) ;
966+ callback ( - 1 , undefined , '' ) ;
967+ break ;
968+ }
969+ }
970+ }
971+ }
972+
951973 if ( expectation && expectation . match ( method , url ) ) {
952974 if ( ! expectation . matchData ( data ) )
953975 throw Error ( 'Expected ' + expectation + ' with different data\n' +
@@ -961,11 +983,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
961983 expectations . shift ( ) ;
962984
963985 if ( expectation . response ) {
964- responses . push ( function ( ) {
965- var response = expectation . response ( method , url , data , headers ) ;
966- xhr . $$respHeaders = response [ 2 ] ;
967- callback ( response [ 0 ] , response [ 1 ] , xhr . getAllResponseHeaders ( ) ) ;
968- } ) ;
986+ responses . push ( wrapResponse ( expectation ) ) ;
969987 return ;
970988 }
971989 wasExpected = true ;
@@ -976,13 +994,9 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
976994 if ( definition . match ( method , url , data , headers || { } ) ) {
977995 if ( definition . response ) {
978996 // if $browser specified, we do auto flush all requests
979- ( $browser ? $browser . defer : responsesPush ) ( function ( ) {
980- var response = definition . response ( method , url , data , headers ) ;
981- xhr . $$respHeaders = response [ 2 ] ;
982- callback ( response [ 0 ] , response [ 1 ] , xhr . getAllResponseHeaders ( ) ) ;
983- } ) ;
997+ ( $browser ? $browser . defer : responsesPush ) ( wrapResponse ( definition ) ) ;
984998 } else if ( definition . passThrough ) {
985- $delegate ( method , url , data , callback , headers ) ;
999+ $delegate ( method , url , data , callback , headers , timeout ) ;
9861000 } else throw Error ( 'No response defined !' ) ;
9871001 return ;
9881002 }
0 commit comments