blob: 200014dde53ff0697cfbe6228a9091d5b989f515 [file] [log] [blame]
Matteo Scandolof0830892015-11-05 11:58:53 +01001'use strict';
Matteo Scandolo32f83922016-02-11 09:35:29 -08002describe('The xos.helper module', function(){
Matteo Scandolo6297a262016-03-24 15:11:29 -07003 var SetCSRFToken, httpProviderObj, httpBackend, http, cookies;
Matteo Scandolof0830892015-11-05 11:58:53 +01004
Matteo Scandolo6297a262016-03-24 15:11:29 -07005 const fakeToken = 'aiuhsnds98234ndASd';
Matteo Scandolo32f83922016-02-11 09:35:29 -08006
Matteo Scandolof5ab3ff2016-03-24 14:07:49 -07007 beforeEach(function() {
Matteo Scandolo6297a262016-03-24 15:11:29 -07008 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 Scandolof5ab3ff2016-03-24 14:07:49 -070024 });
Matteo Scandolof0830892015-11-05 11:58:53 +010025
Matteo Scandolof5ab3ff2016-03-24 14:07:49 -070026 });
Matteo Scandolo89b179c2016-03-24 11:38:50 -070027
Matteo Scandolo6297a262016-03-24 15:11:29 -070028 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();
37 }).respond(200, {name: 'example' });
38
39 http.post('http://example.com');
40
41 httpBackend.flush();
42 });
Matteo Scandolo89b179c2016-03-24 11:38:50 -070043 });
44
Matteo Scandolof5ab3ff2016-03-24 14:07:49 -070045 it('should set SetCSRFToken interceptor', () => {
Matteo Scandolo89b179c2016-03-24 11:38:50 -070046 expect(httpProviderObj).toBeDefined();
47 expect(httpProviderObj.interceptors).toContain('SetCSRFToken');
48 });
49
Matteo Scandolof0830892015-11-05 11:58:53 +010050});