2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 import { TestBed } from '@angular/core/testing';
23 HttpClientTestingModule,
24 HttpTestingController,
25 } from '@angular/common/http/testing';
26 import { HTTP_INTERCEPTORS } from '@angular/common/http';
27 import { HttpRequestInterceptor } from './interceptor';
28 import { NotificationService } from '@services/ui/notification.service';
29 import { ProducerService } from '@services/ei/producer.service';
30 import { of } from 'rxjs/observable/of';
32 describe(`HttpRequestInterceptor`, () => {
33 let service: ProducerService;
34 const notificationServiceSpy = jasmine.createSpyObj('NotificationService', [ 'error' ]);
35 let httpMock: HttpTestingController;
39 TestBed.configureTestingModule({
40 imports: [HttpClientTestingModule],
43 provide: NotificationService, useValue: notificationServiceSpy
46 provide: HTTP_INTERCEPTORS,
47 useClass: HttpRequestInterceptor,
54 httpMock = TestBed.inject(HttpTestingController);
55 service = TestBed.inject(ProducerService);
58 it('should create', () => {
59 const interceptors = TestBed.inject(HTTP_INTERCEPTORS);
61 interceptors.forEach(interceptor => {
62 if (interceptor instanceof HttpRequestInterceptor) {
66 expect(found).toBeTruthy();
69 it('should pass through when ok', () => {
70 let response: [ 'roducer1' ];
72 spyOn(service, 'getProducerIds').and.returnValue(of(response));
73 service.getProducerIds().subscribe(() => {
74 ids => expect(ids).toEqual(response);
80 it('should notify when error', () => {
84 service.getProducerIds().subscribe(res => response = res, err => errResponse = err);
86 const data = 'Invalid request parameters';
87 const mockErrorResponse = { status: 400, statusText: 'Bad Request' };
88 httpMock.expectOne(`/data-producer/v1/info-producers`).flush(data, mockErrorResponse);
92 expect(notificationServiceSpy.error).toHaveBeenCalledWith(containsString('Bad Request'));
95 function containsString(msg: string) {
97 asymmetricMatch: function(compareTo: string) {
98 return compareTo.includes(msg);
101 jasmineToString: function() {
102 return '<containsString: ' + msg + '>';