Matteo Scandolo | 57483dd | 2015-11-05 11:58:53 +0100 | [diff] [blame] | 1 | 'use strict'; |
Matteo Scandolo | ba4c9aa | 2016-02-11 09:35:29 -0800 | [diff] [blame] | 2 | describe('The xos.helper module', function(){ |
Matteo Scandolo | 7dea243 | 2016-03-24 15:11:29 -0700 | [diff] [blame] | 3 | var SetCSRFToken, httpProviderObj, httpBackend, http, cookies; |
Matteo Scandolo | 57483dd | 2015-11-05 11:58:53 +0100 | [diff] [blame] | 4 | |
Matteo Scandolo | 7dea243 | 2016-03-24 15:11:29 -0700 | [diff] [blame] | 5 | const fakeToken = 'aiuhsnds98234ndASd'; |
Matteo Scandolo | ba4c9aa | 2016-02-11 09:35:29 -0800 | [diff] [blame] | 6 | |
Matteo Scandolo | 061094b | 2016-03-24 14:07:49 -0700 | [diff] [blame] | 7 | beforeEach(function() { |
Matteo Scandolo | 7dea243 | 2016-03-24 15:11:29 -0700 | [diff] [blame] | 8 | module( |
| 9 | 'xos.helpers', |
| 10 | function ($httpProvider) { |
| 11 | //save our interceptor |
| 12 | httpProviderObj = $httpProvider; |
| 13 | } |
| 14 | ); |
| 15 | |
| 16 | inject(function (_SetCSRFToken_, _$httpBackend_, _$http_, _$cookies_) { |
| 17 | SetCSRFToken = _SetCSRFToken_; |
| 18 | httpBackend = _$httpBackend_; |
| 19 | http = _$http_; |
| 20 | cookies = _$cookies_ |
| 21 | |
| 22 | // mocking $cookie service |
| 23 | spyOn(cookies, 'get').and.returnValue(fakeToken); |
Matteo Scandolo | 061094b | 2016-03-24 14:07:49 -0700 | [diff] [blame] | 24 | }); |
Matteo Scandolo | 57483dd | 2015-11-05 11:58:53 +0100 | [diff] [blame] | 25 | |
Matteo Scandolo | 061094b | 2016-03-24 14:07:49 -0700 | [diff] [blame] | 26 | }); |
Matteo Scandolo | 8420f62 | 2016-03-24 11:38:50 -0700 | [diff] [blame] | 27 | |
Matteo Scandolo | 7dea243 | 2016-03-24 15:11:29 -0700 | [diff] [blame] | 28 | describe('the SetCSRFToken', () => { |
| 29 | it('should exist', () => { |
| 30 | expect(SetCSRFToken).toBeDefined(); |
| 31 | }); |
| 32 | |
| 33 | it('should attach token the request', (done) => { |
| 34 | httpBackend.when('POST', 'http://example.com', null, function(headers) { |
| 35 | expect(headers['X-CSRFToken']).toBe(fakeToken); |
| 36 | done(); |
Matteo Scandolo | d72d490 | 2016-04-15 13:00:01 -0700 | [diff] [blame] | 37 | return headers; |
Matteo Scandolo | 7dea243 | 2016-03-24 15:11:29 -0700 | [diff] [blame] | 38 | }).respond(200, {name: 'example' }); |
| 39 | |
| 40 | http.post('http://example.com'); |
| 41 | |
| 42 | httpBackend.flush(); |
| 43 | }); |
Matteo Scandolo | 8420f62 | 2016-03-24 11:38:50 -0700 | [diff] [blame] | 44 | }); |
| 45 | |
Matteo Scandolo | 061094b | 2016-03-24 14:07:49 -0700 | [diff] [blame] | 46 | it('should set SetCSRFToken interceptor', () => { |
Matteo Scandolo | 8420f62 | 2016-03-24 11:38:50 -0700 | [diff] [blame] | 47 | expect(httpProviderObj).toBeDefined(); |
| 48 | expect(httpProviderObj.interceptors).toContain('SetCSRFToken'); |
| 49 | }); |
| 50 | |
Matteo Scandolo | 57483dd | 2015-11-05 11:58:53 +0100 | [diff] [blame] | 51 | }); |