-
Notifications
You must be signed in to change notification settings - Fork 1
/
interceptorSpec.js
228 lines (190 loc) · 7.76 KB
/
interceptorSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'use strict';
describe('interceptor', function() {
beforeEach(function() {
module('angular-jwt.interceptor');
module('angular-jwt.options');
});
afterEach(inject(function($httpBackend) {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
}));
it('should intercept requests when added to $httpProvider.interceptors and set token', function (done) {
module( function ($httpProvider, jwtInterceptorProvider, jwtOptionsProvider) {
jwtInterceptorProvider.tokenGetter = function() {
return 123;
}
$httpProvider.interceptors.push('jwtInterceptor');
});
inject(function ($http, $httpBackend) {
$http({url: '/hello'}).then(function (data) {
expect(data.data).to.be.equal('hello');
done();
});
$httpBackend.expectGET('/hello', function (headers) {
return headers.Authorization === 'Bearer 123';
}).respond(200, 'hello');
$httpBackend.flush();
});
});
it('should not add Authr headers to Cross Origin requests unless whitelisted', function (done) {
module( function ($httpProvider, jwtOptionsProvider, jwtInterceptorProvider) {
jwtInterceptorProvider.whiteListedDomains = ['whitelisted.Example.com']
jwtInterceptorProvider.tokenGetter = function() {
return 123;
}
$httpProvider.interceptors.push('jwtInterceptor');
});
inject(function ($http, $httpBackend, $q) {
$q.all([
$http({url: 'http://Example.com/hello' }),
$http({url: 'http://www.example.com/hello' }),
$http({url: 'http://wwwXexample.com/hello' }),
$http({url: 'http://whitelisted.example.com.evil.com/hello' }),
$http({url: 'http://whitelisted.example.com/hello' })
]).then(function () {
done();
})
$httpBackend.expectGET('http://Example.com/hello', function (headers) {
return headers.Authorization === undefined;
}).respond(200);
$httpBackend.expectGET('http://www.example.com/hello', function (headers) {
return headers.Authorization === undefined;
}).respond(200);
$httpBackend.expectGET('http://wwwXexample.com/hello', function (headers) {
return headers.Authorization === undefined;
}).respond(200);
$httpBackend.expectGET('http://whitelisted.example.com.evil.com/hello', function (headers) {
return headers.Authorization === undefined;
}).respond(200);
$httpBackend.expectGET('http://whitelisted.example.com/hello', function (headers) {
return headers.Authorization === 'Bearer 123';
}).respond(200);
$httpBackend.flush();
});
})
it('should not add Authr headers to Cross Origin requests unless whitelisted with regexp', function (done) {
module( function ($httpProvider, jwtOptionsProvider, jwtInterceptorProvider) {
jwtInterceptorProvider.whiteListedDomains = [/^whitelisted(-pr-\d+)?\.Example\.com$/i]
jwtInterceptorProvider.tokenGetter = function() {
return 123;
}
$httpProvider.interceptors.push('jwtInterceptor');
});
inject(function ($http, $httpBackend, $q) {
$q.all([
$http({url: 'http://Example.com/hello' }),
$http({url: 'http://www.example.com/hello' }),
$http({url: 'http://whitelisted-pr-123.example.com.evil.com/hello' }),
$http({url: 'http://extrawhitelisted-pr-123.example.com.evil.com/hello' }),
$http({url: 'http://whitelisted-pr-123.example.com/hello' })
]).then(function () {
done();
})
$httpBackend.expectGET('http://Example.com/hello', function (headers) {
return headers.Authorization === undefined;
}).respond(200);
$httpBackend.expectGET('http://www.example.com/hello', function (headers) {
return headers.Authorization === undefined;
}).respond(200);
$httpBackend.expectGET('http://whitelisted-pr-123.example.com.evil.com/hello', function (headers) {
return headers.Authorization === undefined;
}).respond(200);
$httpBackend.expectGET('http://extrawhitelisted-pr-123.example.com.evil.com/hello', function (headers) {
return headers.Authorization === undefined;
}).respond(200);
$httpBackend.expectGET('http://whitelisted-pr-123.example.com/hello', function (headers) {
return headers.Authorization === 'Bearer 123';
}).respond(200);
$httpBackend.flush();
});
})
it('should work with promises', function (done) {
module( function ($httpProvider, jwtOptionsProvider, jwtInterceptorProvider) {
jwtInterceptorProvider.tokenGetter = function($q) {
return $q.when(345);
}
$httpProvider.interceptors.push('jwtInterceptor');
});
inject(function ($http, $httpBackend) {
$http({url: '/hello'}).then(function (data) {
expect(data.data).to.be.equal('hello');
done();
});
$httpBackend.expectGET('/hello', function (headers) {
return headers.Authorization === 'Bearer 345';
}).respond(200, 'hello');
$httpBackend.flush();
});
});
it('should not send it if no tokenGetter', function (done) {
module( function ($httpProvider, jwtInterceptorProvider) {
$httpProvider.interceptors.push('jwtInterceptor');
});
inject(function ($http, $httpBackend) {
$http({url: '/hello'}).then(function (data) {
expect(data.data).to.be.equal('hello');
done();
});
$httpBackend.expectGET('/hello', function (headers) {
return !headers.Authorization;
}).respond(200, 'hello');
$httpBackend.flush();
});
});
it('should add the token to the url params when the configuration option is set', function (done) {
module( function ($httpProvider, jwtOptionsProvider, jwtInterceptorProvider) {
jwtInterceptorProvider.urlParam = 'access_token';
jwtInterceptorProvider.tokenGetter = function() {
return 123;
}
$httpProvider.interceptors.push('jwtInterceptor');
});
inject(function ($http, $httpBackend) {
$http({url: '/hello'}).then(function (data) {
expect(data.data).to.be.equal('hello');
done();
});
$httpBackend.expectGET('/hello?access_token=123', function (headers) {
return headers.Authorization === undefined;
}).respond(200, 'hello');
$httpBackend.flush();
});
});
it('should handle an undefined responseError', function (done) {
module( function($httpProvider, jwtOptionsProvider, jwtInterceptorProvider) {
jwtInterceptorProvider.tokenGetter = function() {
return 123;
}
$httpProvider.interceptors.push('jwtInterceptor');
});
inject(function (jwtInterceptor, $httpBackend) {
jwtInterceptor.responseError(undefined).then(function (data) {
}).catch(function (data) {
expect(data).to.be.equal(undefined);
done();
});
$httpBackend.flush();
});
});
it('should broadcast unauthenticated on 401 response', function (done) {
module( function ($httpProvider, jwtInterceptorProvider, jwtOptionsProvider, $rootScopeProvider) {
jwtInterceptorProvider.tokenGetter = function() {
return 123;
}
$httpProvider.interceptors.push('jwtInterceptor');
});
inject(function ($http, $httpBackend, $rootScope) {
sinon.spy($rootScope, "$broadcast");
$http({url: '/hello'}).then(function (data) {
}).catch(function (data) {
expect(data.status).to.be.equal(401);
expect($rootScope.$broadcast.calledWith("unauthenticated")).to.be.true
done();
});
$httpBackend.expectGET('/hello', function (headers) {
return headers.Authorization === 'Bearer 123';
}).respond(401, {}, {}, 'Error');
$httpBackend.flush();
});
});
});