mock-agent.d.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import Agent from './agent'
  2. import Dispatcher from './dispatcher'
  3. import { Interceptable, MockInterceptor } from './mock-interceptor'
  4. import MockDispatch = MockInterceptor.MockDispatch;
  5. export default MockAgent
  6. interface PendingInterceptor extends MockDispatch {
  7. origin: string;
  8. }
  9. /** A mocked Agent class that implements the Agent API. It allows one to intercept HTTP requests made through undici and return mocked responses instead. */
  10. declare class MockAgent<TMockAgentOptions extends MockAgent.Options = MockAgent.Options> extends Dispatcher {
  11. constructor(options?: MockAgent.Options)
  12. /** Creates and retrieves mock Dispatcher instances which can then be used to intercept HTTP requests. If the number of connections on the mock agent is set to 1, a MockClient instance is returned. Otherwise a MockPool instance is returned. */
  13. get<TInterceptable extends Interceptable>(origin: string): TInterceptable;
  14. get<TInterceptable extends Interceptable>(origin: RegExp): TInterceptable;
  15. get<TInterceptable extends Interceptable>(origin: ((origin: string) => boolean)): TInterceptable;
  16. /** Dispatches a mocked request. */
  17. dispatch(options: Agent.DispatchOptions, handler: Dispatcher.DispatchHandlers): boolean;
  18. /** Closes the mock agent and waits for registered mock pools and clients to also close before resolving. */
  19. close(): Promise<void>;
  20. /** Disables mocking in MockAgent. */
  21. deactivate(): void;
  22. /** Enables mocking in a MockAgent instance. When instantiated, a MockAgent is automatically activated. Therefore, this method is only effective after `MockAgent.deactivate` has been called. */
  23. activate(): void;
  24. /** Define host matchers so only matching requests that aren't intercepted by the mock dispatchers will be attempted. */
  25. enableNetConnect(): void;
  26. enableNetConnect(host: string): void;
  27. enableNetConnect(host: RegExp): void;
  28. enableNetConnect(host: ((host: string) => boolean)): void;
  29. /** Causes all requests to throw when requests are not matched in a MockAgent intercept. */
  30. disableNetConnect(): void;
  31. pendingInterceptors(): PendingInterceptor[];
  32. assertNoPendingInterceptors(options?: {
  33. pendingInterceptorsFormatter?: PendingInterceptorsFormatter;
  34. }): void;
  35. }
  36. interface PendingInterceptorsFormatter {
  37. format(pendingInterceptors: readonly PendingInterceptor[]): string;
  38. }
  39. declare namespace MockAgent {
  40. /** MockAgent options. */
  41. export interface Options extends Agent.Options {
  42. /** A custom agent to be encapsulated by the MockAgent. */
  43. agent?: Agent;
  44. }
  45. }