Skip to content

Commit 252292b

Browse files
author
Carlos Atencio
committed
Handling default headers to account for any header references in interceptors
1 parent d8fba2d commit 252292b

File tree

5 files changed

+79
-9
lines changed

5 files changed

+79
-9
lines changed

example/protractor-conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var config = {
22
baseUrl: 'http://localhost:8000/',
33
specs: [
4-
'spec/requestsMade.spec.js'
4+
'spec/*.spec.js'
55
],
66
mocks: {
77
dir: 'mocks',

lib/httpMock.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,22 @@ function mockTemplate() {
228228
};
229229
}
230230

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+
231243
function httpMock(config){
232244
var prom;
245+
246+
config.headers = config.headers || {};
233247
var transformedConfig = getTransformedAndInterceptedRequestConfig(angular.copy(config));
234248

235249
return wrapWithSuccessError($q.when(transformedConfig).then(function(resolvedConfig) {
@@ -238,7 +252,7 @@ function mockTemplate() {
238252
if(expectation){
239253
var deferred = $q.defer();
240254

241-
newModule.requests.push(resolvedConfig);
255+
addToRequestHistory(resolvedConfig);
242256

243257
var delay = expectation.response.delay || 0;
244258

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "protractor-http-mock",
3-
"version": "0.7.0",
3+
"version": "0.8.0",
44
"description": "Mock HTTP calls in your protractor specs.",
55
"main": "index.js",
66
"scripts": {

tests/interceptors.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ describe('interceptors', function(){
6363
}
6464
}
6565
}]);
66+
67+
$httpProvider.interceptors.push(['$q', function($q){
68+
return {
69+
request: function(config){
70+
if(config.url.match(/with-headers/)){
71+
config.headers['authorization'] = 'token';
72+
}
73+
74+
return config;
75+
},
76+
response: function(response){
77+
if(response.config.url.match(/with-headers/)){
78+
response.headers['response-header'] = 'response-header';
79+
80+
return $q.when(response);
81+
}
82+
83+
return response;
84+
}
85+
}
86+
}]);
6687
}]);
6788

6889
it('allows intercepts through service factory functions', function(done){
@@ -107,4 +128,14 @@ describe('interceptors', function(){
107128
done();
108129
});
109130
});
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+
});
110141
});

tests/requests.test.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@ describe('requests', function(){
66
});
77

88
it('captures and clears requests', function(done){
9-
http({
10-
method: 'GET',
11-
url: 'test-api.com/user'
12-
});
13-
149
http.head('/head').then(function(){
15-
expect(module.requests.length).toBeGreaterThan(1);
10+
expect(module.requests.length).toBeGreaterThan(0);
1611

1712
var found = false;
1813

1914
module.requests.forEach(function(request){
2015
if(request.method === 'HEAD'){
2116
found = true;
2217
expect(request.url).toBe('/head');
18+
expect(request.headers).not.toBeDefined();
2319
}
2420
});
2521

@@ -30,4 +26,33 @@ describe('requests', function(){
3026
done();
3127
});
3228
});
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+
});
3358
});

0 commit comments

Comments
 (0)