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