File tree Expand file tree Collapse file tree 5 files changed +79
-9
lines changed Expand file tree Collapse file tree 5 files changed +79
-9
lines changed Original file line number Diff line number Diff line change 1
1
var config = {
2
2
baseUrl : 'http://localhost:8000/' ,
3
3
specs : [
4
- 'spec/requestsMade .spec.js'
4
+ 'spec/* .spec.js'
5
5
] ,
6
6
mocks : {
7
7
dir : 'mocks' ,
Original file line number Diff line number Diff line change @@ -228,8 +228,22 @@ function mockTemplate() {
228
228
} ;
229
229
}
230
230
231
+ function addToRequestHistory ( config ) {
232
+ var copy = angular . copy ( config ) ;
233
+
234
+ // This is done to maintain backwards compatability
235
+ // as well as providing a cleaner request history
236
+ if ( angular . equals ( copy . headers , { } ) ) {
237
+ delete copy . headers ;
238
+ }
239
+
240
+ newModule . requests . push ( copy ) ;
241
+ }
242
+
231
243
function httpMock ( config ) {
232
244
var prom ;
245
+
246
+ config . headers = config . headers || { } ;
233
247
var transformedConfig = getTransformedAndInterceptedRequestConfig ( angular . copy ( config ) ) ;
234
248
235
249
return wrapWithSuccessError ( $q . when ( transformedConfig ) . then ( function ( resolvedConfig ) {
@@ -238,7 +252,7 @@ function mockTemplate() {
238
252
if ( expectation ) {
239
253
var deferred = $q . defer ( ) ;
240
254
241
- newModule . requests . push ( resolvedConfig ) ;
255
+ addToRequestHistory ( resolvedConfig ) ;
242
256
243
257
var delay = expectation . response . delay || 0 ;
244
258
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " protractor-http-mock" ,
3
- "version" : " 0.7 .0" ,
3
+ "version" : " 0.8 .0" ,
4
4
"description" : " Mock HTTP calls in your protractor specs." ,
5
5
"main" : " index.js" ,
6
6
"scripts" : {
Original file line number Diff line number Diff line change @@ -63,6 +63,27 @@ describe('interceptors', function(){
63
63
}
64
64
}
65
65
} ] ) ;
66
+
67
+ $httpProvider . interceptors . push ( [ '$q' , function ( $q ) {
68
+ return {
69
+ request : function ( config ) {
70
+ if ( config . url . match ( / w i t h - h e a d e r s / ) ) {
71
+ config . headers [ 'authorization' ] = 'token' ;
72
+ }
73
+
74
+ return config ;
75
+ } ,
76
+ response : function ( response ) {
77
+ if ( response . config . url . match ( / w i t h - h e a d e r s / ) ) {
78
+ response . headers [ 'response-header' ] = 'response-header' ;
79
+
80
+ return $q . when ( response ) ;
81
+ }
82
+
83
+ return response ;
84
+ }
85
+ }
86
+ } ] ) ;
66
87
} ] ) ;
67
88
68
89
it ( 'allows intercepts through service factory functions' , function ( done ) {
@@ -107,4 +128,14 @@ describe('interceptors', function(){
107
128
done ( ) ;
108
129
} ) ;
109
130
} ) ;
131
+
132
+ it ( 'provides default empty object headers if none are set' , function ( done ) {
133
+ http ( {
134
+ method : 'GET' ,
135
+ url : 'test-url.com/with-headers'
136
+ } ) . then ( function ( response ) {
137
+ expect ( response . headers [ 'response-header' ] ) . toBe ( 'response-header' ) ;
138
+ done ( ) ;
139
+ } ) ;
140
+ } ) ;
110
141
} ) ;
Original file line number Diff line number Diff line change @@ -6,20 +6,16 @@ describe('requests', function(){
6
6
} ) ;
7
7
8
8
it ( 'captures and clears requests' , function ( done ) {
9
- http ( {
10
- method : 'GET' ,
11
- url : 'test-api.com/user'
12
- } ) ;
13
-
14
9
http . head ( '/head' ) . then ( function ( ) {
15
- expect ( module . requests . length ) . toBeGreaterThan ( 1 ) ;
10
+ expect ( module . requests . length ) . toBeGreaterThan ( 0 ) ;
16
11
17
12
var found = false ;
18
13
19
14
module . requests . forEach ( function ( request ) {
20
15
if ( request . method === 'HEAD' ) {
21
16
found = true ;
22
17
expect ( request . url ) . toBe ( '/head' ) ;
18
+ expect ( request . headers ) . not . toBeDefined ( ) ;
23
19
}
24
20
} ) ;
25
21
@@ -30,4 +26,33 @@ describe('requests', function(){
30
26
done ( ) ;
31
27
} ) ;
32
28
} ) ;
29
+
30
+ it ( 'captures requests with headers' , function ( done ) {
31
+ http ( {
32
+ method : 'get' ,
33
+ url : '/user' ,
34
+ headers : {
35
+ 'x-auth' : 'pass' ,
36
+ 'gzip-pro' : 'yes'
37
+ }
38
+ } ) . then ( function ( ) {
39
+ expect ( module . requests . length ) . toBeGreaterThan ( 0 ) ;
40
+
41
+ var found = false ;
42
+
43
+ module . requests . forEach ( function ( request ) {
44
+ if ( request . headers ) {
45
+ found = true ;
46
+ expect ( request . url ) . toBe ( '/user' ) ;
47
+ expect ( request . headers ) . toEqual ( {
48
+ 'x-auth' : 'pass' ,
49
+ 'gzip-pro' : 'yes'
50
+ } ) ;
51
+ }
52
+ } ) ;
53
+
54
+ expect ( found ) . toBe ( true ) ;
55
+ done ( ) ;
56
+ } ) ;
57
+ } ) ;
33
58
} ) ;
You can’t perform that action at this time.
0 commit comments