Angular 10: Модульное тестирование с помощью HttpInterceptor, который изменяет ответ, не получая HttpResponse - PullRequest
1 голос
/ 04 августа 2020

Если поискать ответы в SO и до сих пор, все, что я пробовал, дает ту же недостающую информацию.

Это работает Angular 10 с последней версией Karma / Jasmine.

По сути, у меня есть HTTP-перехватчик, который смотрит на тип содержимого возвращаемого объекта. Если это json, продолжайте как обычно, если это html ... тогда выдает ошибку.

import {
    HttpEvent,
    HttpHandler,
    HttpInterceptor,
    HttpRequest,
    HttpResponse
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { httpStatusCodes } from '../../../shared/enums/httpStatusCodes.enum';
import { errorResponse } from './../../../shared/models/errorResponse.model';

@Injectable()
export class WafErrorInterceptor implements HttpInterceptor {
    intercept(
        request: HttpRequest<any>,
        next: HttpHandler
    ): Observable<HttpEvent<any>> {
        return next.handle(request).pipe(
            map((event: HttpEvent<any>) => {
                console.log(event instanceof HttpResponse);
                if (
                    event instanceof HttpResponse &&
                    event.headers.has('content-type') &&
                    event.headers.get('content-type') === 'application/json'
                ) {
                    return event;
                }

                const throwErrorResponse = new errorResponse(
                    httpStatusCodes.WAF_ERROR,
                    '99999',
                    event instanceof HttpResponse
                        ? event.body
                        : 'unknown error occurred'
                );
                throw throwErrorResponse;
            })
        );
    }
}

И затем в своем модульном тесте я запускаю это:

import {
    HttpClient,
    HttpHeaders,
    HttpResponse,
    HTTP_INTERCEPTORS
} from '@angular/common/http';
import {
    HttpClientTestingModule,
    HttpTestingController
} from '@angular/common/http/testing';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { errorResponse } from './../../../shared/models/errorResponse.model';
import { WafErrorInterceptor } from './waf-error.service';

describe('WafErrorInterceptor', () => {
    let httpMock: HttpTestingController;
    let httpClient: HttpClient;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [HttpClientTestingModule],
            providers: [
                {
                    provide: HTTP_INTERCEPTORS,
                    useClass: WafErrorInterceptor,
                    multi: true
                }
            ]
        });
        httpMock = TestBed.get(HttpTestingController);
        httpClient = TestBed.get(HttpClient);
    });

    afterEach(() => {
        httpMock.verify();
    });

    it('intercept: when no error, then subscribe returns successfully', () => {
        const testData: string = 'test';

        httpClient.get<string>('https://secure.testurl.com/success').subscribe(
            (data) => expect(data).toBeTruthy(),
            (error: errorResponse) => {
                console.log(error);
                fail('error should not have been called');
            }
        );
        tick();
        let req = httpMock.expectOne('https://secure.testurl.com/success');
        tick();

        let httpHeaders = new HttpHeaders();
        httpHeaders.set('content-type', 'application/json');

        const expectedResponse = new HttpResponse<string>({
            status: 200,
            statusText: 'OK',
            body: testData,
            headers: httpHeaders
        });

        //req.flush(expectedResponse);
        req.event(expectedResponse);
    });
});

Я пробовал flu sh, где я просто отправляю данные, где я отправляю обратно данные и заголовки / статус. Где я отправляю ответ httpresponse, et c. Каждый раз, когда он попадает в перехватчик, перехватчик не видит ответа типа httpresponse, а console.log всегда возвращает false.

Я даже только что создал модульный тест, в котором модульный тест отправляет фиктивный объект ... и даже у него та же проблема.

Мысли?

ОБНОВЛЕНО: Итак, приведенный ниже ответ работает для карты, но все еще возникают проблемы с тестированием catchErrorInterceptor. Код работает на моем сайте. Наш API возвращает объект, в котором ошибки содержат массив ошибок. Итак, мы берем первый и используем его.

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
    intercept(
        request: HttpRequest<any>,
        next: HttpHandler
    ): Observable<HttpEvent<any>> {
        return next.handle(request).pipe(
            catchError((error: HttpErrorResponse) => {
                let statusCode = httpStatusCodes.CONFLICT;
                let errorMessage = '';
                let errorCode = '99999';

                statusCode =
                    error.status === 0
                        ? httpStatusCodes.INTERNAL_SERVER_ERROR
                        : error.status;

                if (error.error.errors && error.error.errors.length > 0) {
                    errorCode = error.error.errors[0].code;
                    errorMessage = error.error.errors[0].description;
                } else {
                    errorMessage = error.message;
                }

                const throwErrorResponse = new errorResponse(
                    statusCode,
                    errorCode,
                    errorMessage
                );

                return throwError(throwErrorResponse);
            })
        );
    }
}

Вот один из тестов:

    it('intercept: when delibarate 409, then error returned', (done) => {
        httpClient
            .get<string>('https://secure.go2bank.com/error')
            .pipe(skip(1))
            .subscribe(
                (data) => fail('should have failed with the 404 error'),
                (error: errorResponse) => {
                    expect(error).toBeTruthy(); // check if executed
                    expect(error.httpStatusCodes).toBe(
                        httpStatusCodes.CONFLICT
                    );
                    expect(error.errorCode).toBe('1007');
                    expect(error.errorMessage).toBe(
                        'Validation error Authorization'
                    );
                    done();
                }
            );

        const errorInitEvent: ErrorEventInit = {
            message: null,
            error: {
                errors: [
                    {
                        code: '1007',
                        description: 'Validation error Authorization.',
                        message: null,
                        link: null,
                        additionalinfo: null
                    }
                ]
            }),
            lineno: null,
            colno: null,
            filename: null
        };

        let error = new ErrorEvent('ERROR', errorInitEvent);

        httpMock.expectOne('https://secure.go2bank.com/error').error(error, {
            status: httpStatusCodes.CONFLICT,
            statusText: 'Conflict',
            headers: new HttpHeaders().set('content-type', 'application/json')
        });
    });

Результат этого теста всегда 99999, а не 1007. Итак, я ' m получает ошибку, он ловит ошибку. Но когда я смотрю на него, error.error - это ErrorEvent (isTrusted: // "), и не похоже, что у меня есть массив ошибок внутри Error.

1 Ответ

1 голос
/ 04 августа 2020

Основная проблема здесь в том, что в случае HttpClientTestingModule Angular использует класс HttpClientTestingBackend вместо HttpXhrBackend, чтобы имитировать фактический http-запрос.

Но есть одно большое различие в их реализациях:

HttpClientTestingBackend всегда отправляет событие { type: HttpEventType.Sent }, в то время как HttpXhrBackend обычно отправляет HttpResponse событие.

Это означает, что событие { type: HttpEventType.Sent } является первым событием, которое обрабатывается внутри вашего map rx js, и он завершится ошибкой.

Итак, вам нужно отфильтровать ответ, например:

interceptor.ts

import {
  HttpEventType,
  ...
} from '@angular/common/http';
...

map((event: HttpEvent<any>) => {
  if (event.type === HttpEventType.Sent) { <---------- add this
    return event;
  }
  
  if (
    event instanceof HttpResponse &&
    event.headers.has('content-type') &&
    event.headers.get('content-type') === 'application/json'
  ) {
    return event;
  }
  ...

Обновление : Или было бы еще лучше использовать оператор skip (1) rx js в вашем тесте .

Теперь вернемся к вашему тесту .

Сначала нужно удалить все лишние tick() вызовы.

Тогда flush метод имеет немного другую сигнатуру.

flush(body, opts?: {
    headers?: HttpHeaders | {
        [name: string]: string | string[];
    };
    status?: number;
    statusText?: string;
})

Но вы пытались поставить все в body.

Итак, вот ваш тестовый пример:

interceptor.spe c .ts

it('intercept: when no error, then subscribe returns successfully', () => {
  const testData: string = 'test';

  httpClient.get<string>('https://secure.testurl.com/success').subscribe(
    (data) => expect(data).toBeTruthy(),
    (error: errorResponse) => {
      console.log(error);
      fail('error should not have been called');
    }
  );

  const req = httpMock.expectOne('https://secure.testurl.com/success');

  req.flush(testData, {
    status: 200,
    statusText: 'OK',
    headers: new HttpHeaders().set('content-type', 'application/json')
  });
});
...