Why in request.flush(body) the request has no entry in the Headers section?

Viewed 41

I have a HttpInterceptor which adds a header to each request to the API. This works great when I run the app.

Of course, nothing should be done without a clean test. My test looks like this:

import { TestBed } from '@angular/core/testing';
import {
  HttpClientTestingModule,
  HttpTestingController,
  TestRequest,
} from '@angular/common/http/testing';
import { HTTP_INTERCEPTORS, HttpClient, HttpClientModule } from '@angular/common/http';
import { v4 as uuidv4 } from 'uuid';
import { KHttpInterceptor } from './k.http.interceptor';

describe(`AuthHttpInterceptor`, () => {
  let controller: HttpTestingController;
  let client: HttpClient;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule, HttpClientModule],
      providers: [
        {
          provide: HTTP_INTERCEPTORS,
          useClass: KHttpInterceptor,
          multi: true,
        },
      ],
    });

    sessionStorage.setItem('K_MISNG_CLIENT_ID', uuidv4());

    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
    controller = TestBed.inject(HttpTestingController);
    client = TestBed.inject(HttpClient);
  });

  it('header for K_MISNG_CLIENT_ID added', (done) => {
    const body = { camel_case: true };

    client.get('/test').subscribe((response) => {
      done();
    });

    const request: TestRequest = controller.expectOne('/test');
    expect(request.request.responseType).toEqual('json');
    request.flush(body);
    controller.verify();
  });
});

If I now debug the test, the HttpInterceptor will also pass. However, if I check the request after request.flush(body), it has no entry in the "Headers" section.

Am I missing something? The header should have been inserted by the interceptor.

Thanks for your help

0 Answers
Related