|
| 1 | +import { HttpClient } from '@angular/common/http'; |
| 2 | +import { |
| 3 | + HttpClientTestingModule, |
| 4 | + HttpTestingController, |
| 5 | +} from '@angular/common/http/testing'; |
| 6 | +import { TestBed } from '@angular/core/testing'; |
| 7 | + |
| 8 | +import { RESTURLCombiner } from '../url-combiner/rest-url-combiner'; |
| 9 | +import { BrowserXSRFService } from './browser-xsrf.service'; |
| 10 | + |
| 11 | +describe(`BrowserXSRFService`, () => { |
| 12 | + let service: BrowserXSRFService; |
| 13 | + let httpClient: HttpClient; |
| 14 | + let httpTestingController: HttpTestingController; |
| 15 | + |
| 16 | + const endpointURL = new RESTURLCombiner('/security/csrf').toString(); |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + TestBed.configureTestingModule({ |
| 20 | + imports: [ HttpClientTestingModule ], |
| 21 | + providers: [ BrowserXSRFService ], |
| 22 | + }); |
| 23 | + httpClient = TestBed.inject(HttpClient); |
| 24 | + httpTestingController = TestBed.inject(HttpTestingController); |
| 25 | + service = TestBed.inject(BrowserXSRFService); |
| 26 | + }); |
| 27 | + |
| 28 | + describe(`initXSRFToken`, () => { |
| 29 | + it(`should perform a GET to the csrf endpoint`, (done: DoneFn) => { |
| 30 | + service.initXSRFToken(httpClient)(); |
| 31 | + |
| 32 | + const req = httpTestingController.expectOne({ |
| 33 | + url: endpointURL, |
| 34 | + method: 'GET', |
| 35 | + }); |
| 36 | + |
| 37 | + req.flush({}); |
| 38 | + httpTestingController.verify(); |
| 39 | + expect().nothing(); |
| 40 | + done(); |
| 41 | + }); |
| 42 | + |
| 43 | + describe(`when the GET succeeds`, () => { |
| 44 | + it(`should set tokenInitialized$ to true`, (done: DoneFn) => { |
| 45 | + service.initXSRFToken(httpClient)(); |
| 46 | + |
| 47 | + const req = httpTestingController.expectOne(endpointURL); |
| 48 | + |
| 49 | + req.flush({}); |
| 50 | + httpTestingController.verify(); |
| 51 | + |
| 52 | + expect(service.tokenInitialized$.getValue()).toBeTrue(); |
| 53 | + done(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + }); |
| 58 | +}); |
0 commit comments