mock-agent.d.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import Agent from './agent'
  2. import Dispatcher from './dispatcher'
  3. import { Interceptable, MockInterceptor } from './mock-interceptor'
  4. import MockDispatch = MockInterceptor.MockDispatch
  5. import { MockCallHistory } from './mock-call-history'
  6. export default MockAgent
  7. interface PendingInterceptor extends MockDispatch {
  8. origin: string;
  9. }
  10. /** A mocked Agent class that implements the Agent API. It allows one to intercept HTTP requests made through undici and return mocked responses instead. */
  11. declare class MockAgent<TMockAgentOptions extends MockAgent.Options = MockAgent.Options> extends Dispatcher {
  12. constructor (options?: TMockAgentOptions)
  13. /** 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. */
  14. get<TInterceptable extends Interceptable>(origin: string): TInterceptable
  15. get<TInterceptable extends Interceptable>(origin: RegExp): TInterceptable
  16. get<TInterceptable extends Interceptable>(origin: ((origin: string) => boolean)): TInterceptable
  17. /** Dispatches a mocked request. */
  18. dispatch (options: Agent.DispatchOptions, handler: Dispatcher.DispatchHandler): boolean
  19. /** Closes the mock agent and waits for registered mock pools and clients to also close before resolving. */
  20. close (): Promise<void>
  21. /** Disables mocking in MockAgent. */
  22. deactivate (): void
  23. /** 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. */
  24. activate (): void
  25. /** Define host matchers so only matching requests that aren't intercepted by the mock dispatchers will be attempted. */
  26. enableNetConnect (): void
  27. enableNetConnect (host: string): void
  28. enableNetConnect (host: RegExp): void
  29. enableNetConnect (host: ((host: string) => boolean)): void
  30. /** Causes all requests to throw when requests are not matched in a MockAgent intercept. */
  31. disableNetConnect (): void
  32. /** get call history. returns the MockAgent call history or undefined if the option is not enabled. */
  33. getCallHistory (): MockCallHistory | undefined
  34. /** clear every call history. Any MockCallHistoryLog will be deleted on the MockCallHistory instance */
  35. clearCallHistory (): void
  36. /** Enable call history. Any subsequence calls will then be registered. */
  37. enableCallHistory (): this
  38. /** Disable call history. Any subsequence calls will then not be registered. */
  39. disableCallHistory (): this
  40. pendingInterceptors (): PendingInterceptor[]
  41. assertNoPendingInterceptors (options?: {
  42. pendingInterceptorsFormatter?: PendingInterceptorsFormatter;
  43. }): void
  44. }
  45. interface PendingInterceptorsFormatter {
  46. format(pendingInterceptors: readonly PendingInterceptor[]): string;
  47. }
  48. declare namespace MockAgent {
  49. /** MockAgent options. */
  50. export interface Options extends Agent.Options {
  51. /** A custom agent to be encapsulated by the MockAgent. */
  52. agent?: Dispatcher;
  53. /** Ignore trailing slashes in the path */
  54. ignoreTrailingSlash?: boolean;
  55. /** Accept URLs with search parameters using non standard syntaxes. default false */
  56. acceptNonStandardSearchParameters?: boolean;
  57. /** Enable call history. you can either call MockAgent.enableCallHistory(). default false */
  58. enableCallHistory?: boolean
  59. }
  60. }