NG.X · Angular 22 practice question

In this functional HTTP interceptor, how does authInterceptor obtain an instance of AuthService?

export function authInterceptor(
  request: HttpRequest<unknown>,
  next: HttpHandlerFn,
): Observable<HttpEvent<unknown>> {
  const authService = inject(AuthService);
  const clonedRequest = request.clone({
    headers: request.headers.append('X-Authentication-Token', authService.getAuthToken()),
  });
  return next(clonedRequest);
}
Choose one answer
Check answer and explanation

Correct answer: By calling inject(AuthService) inside the interceptor function.

Explanation

Functional interceptors run in an injection context, so authInterceptor retrieves AuthService by calling inject(AuthService).

Common trap

This is tempting because dependencies are sometimes passed as function parameters, but HttpInterceptorFn only receives request and next. This is tempting because tests use TestBed.inject(), but production interceptor code must use inject() rather than TestBed.

Source: Angular docs: Test requests — Testing an Interceptor
Checked against the cited text on 2026-11-25.

Report an error in this question