blob: 200014dde53ff0697cfbe6228a9091d5b989f515 [file] [log] [blame]
Matteo Scandolo57483dd2015-11-05 11:58:53 +01001'use strict';
Matteo Scandoloba4c9aa2016-02-11 09:35:29 -08002describe('The xos.helper module', function(){
Matteo Scandolo7dea2432016-03-24 15:11:29 -07003 var SetCSRFToken, httpProviderObj, httpBackend, http, cookies;
Matteo Scandolo57483dd2015-11-05 11:58:53 +01004
Matteo Scandolo7dea2432016-03-24 15:11:29 -07005 const fakeToken = 'aiuhsnds98234ndASd';
Matteo Scandoloba4c9aa2016-02-11 09:35:29 -08006
Matteo Scandolo061094b2016-03-24 14:07:49 -07007 beforeEach(function() {
Matteo Scandolo7dea2432016-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 Scandolo061094b2016-03-24 14:07:49 -070024 });
Matteo Scandolo57483dd2015-11-05 11:58:53 +010025
Matteo Scandolo061094b2016-03-24 14:07:49 -070026 });
Matteo Scandolo8420f622016-03-24 11:38:50 -070027
Matteo Scandolo7dea2432016-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 Scandolo8420f622016-03-24 11:38:50 -070043 });
44
Matteo Scandolo061094b2016-03-24 14:07:49 -070045 it('should set SetCSRFToken interceptor', () => {
Matteo Scandolo8420f622016-03-24 11:38:50 -070046 expect(httpProviderObj).toBeDefined();
47 expect(httpProviderObj.interceptors).toContain('SetCSRFToken');
48 });
49
Matteo Scandolo57483dd2015-11-05 11:58:53 +010050});