@@ -31,6 +31,51 @@ describe('$http', function() {
31
31
} ) ) ;
32
32
33
33
34
+ describe ( '$httpProvider' , function ( ) {
35
+
36
+ describe ( 'interceptors' , function ( ) {
37
+
38
+ it ( 'should default to an empty array' , inject ( function ( $httpProvider ) {
39
+ expect ( $httpProvider . responseInterceptors ) . toEqual ( [ ] ) ;
40
+ } ) ) ;
41
+
42
+
43
+ it ( 'should pass the responses through interceptors' , inject ( function ( $httpProvider , $q ) {
44
+ // just change the response data and pass the response object along
45
+ $httpProvider . responseInterceptors . push ( function ( httpPromise ) {
46
+ return httpPromise . then ( function ( response ) {
47
+ response . data += '!' ;
48
+ return response ;
49
+ } ) ;
50
+ } ) ;
51
+
52
+ // return a new resolved promise representing modified response object
53
+ $httpProvider . responseInterceptors . push ( function ( httpPromise ) {
54
+ return httpPromise . then ( function ( response ) {
55
+ var deferred = $q . defer ( ) ;
56
+ deferred . resolve ( {
57
+ data : response . data + '?' ,
58
+ status : 209 ,
59
+ headers : response . headers ,
60
+ config : response . config
61
+ } ) ;
62
+ return deferred . promise ;
63
+ } ) ;
64
+ } ) ;
65
+ } , function ( $http , $httpBackend ) {
66
+ $httpBackend . expect ( 'GET' , '/foo' ) . respond ( 201 , 'Hello' ) ;
67
+ $http . get ( '/foo' ) . success ( function ( data , status ) {
68
+ expect ( data ) . toBe ( 'Hello!?' ) ;
69
+ expect ( status ) . toBe ( 209 ) ;
70
+ callback ( ) ;
71
+ } )
72
+ $httpBackend . flush ( ) ;
73
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
74
+ } ) ) ;
75
+ } ) ;
76
+ } ) ;
77
+
78
+
34
79
it ( 'should do basic request' , function ( ) {
35
80
$httpBackend . expect ( 'GET' , '/url' ) . respond ( '' ) ;
36
81
$http ( { url : '/url' , method : 'GET' } ) ;
0 commit comments