|
| 1 | +import { AzureAdB2CService } from 'src/app/modules/login/services/azure.ad.b2c.service'; |
| 2 | +import { Observable, of } from 'rxjs'; |
| 3 | + |
| 4 | +import { environment } from '../../../../environments/environment'; |
| 5 | +import { InjectTokenInterceptor } from './inject.token.interceptor'; |
| 6 | +import { HttpHandler, HttpRequest, HttpResponse, HttpEvent } from '@angular/common/http'; |
| 7 | + |
| 8 | +describe('InjectTokenInterceptor test', () => { |
| 9 | + |
| 10 | + class MockHttpHandler extends HttpHandler { |
| 11 | + handle(req: HttpRequest<any>): Observable<HttpEvent<any>> { |
| 12 | + return of(new HttpResponse()); |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + const azureAdB2CService: AzureAdB2CService = new AzureAdB2CService(); |
| 17 | + azureAdB2CService.getBearerToken = () => { |
| 18 | + return 'XYZ'; |
| 19 | + }; |
| 20 | + const handler: HttpHandler = new MockHttpHandler(); |
| 21 | + |
| 22 | + it('if request.url is not part of time-tracker-api, then next.handle', () => { |
| 23 | + const interceptor = new InjectTokenInterceptor(azureAdB2CService); |
| 24 | + const request = new HttpRequest('GET', '/foo'); |
| 25 | + spyOn(handler, 'handle'); |
| 26 | + |
| 27 | + interceptor.intercept(request, handler); |
| 28 | + |
| 29 | + expect(handler.handle).toHaveBeenCalledWith(request); |
| 30 | + }); |
| 31 | + |
| 32 | + it('if request.url is part of time-tracker-api, then Authorization header is injected', () => { |
| 33 | + const interceptor = new InjectTokenInterceptor(azureAdB2CService); |
| 34 | + const request = new HttpRequest('GET', environment.timeTrackerApiUrl); |
| 35 | + spyOn(handler, 'handle'); |
| 36 | + const requestWithHeaders = request.clone( |
| 37 | + { |
| 38 | + headers: request.headers.set('Authorization', 'Bearer XYZ') |
| 39 | + }); |
| 40 | + |
| 41 | + interceptor.intercept(request, handler); |
| 42 | + |
| 43 | + expect(handler.handle).toHaveBeenCalledWith(requestWithHeaders); |
| 44 | + }); |
| 45 | + |
| 46 | +}); |
0 commit comments