blob: 8901bdeb0ed8b9b721e73e4b876bf0480d440109 [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();
Matteo Scandolo833c7d82016-04-15 13:00:01 -070037 return headers;
Matteo Scandolo6297a262016-03-24 15:11:29 -070038 }).respond(200, {name: 'example' });
39
40 http.post('http://example.com');
41
42 httpBackend.flush();
43 });
Matteo Scandolo89b179c2016-03-24 11:38:50 -070044 });
45
Matteo Scandolof5ab3ff2016-03-24 14:07:49 -070046 it('should set SetCSRFToken interceptor', () => {
Matteo Scandolo89b179c2016-03-24 11:38:50 -070047 expect(httpProviderObj).toBeDefined();
48 expect(httpProviderObj.interceptors).toContain('SetCSRFToken');
49 });
50
Matteo Scandolof0830892015-11-05 11:58:53 +010051});