testing.d.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @license Angular v8.1.0
  3. * (c) 2010-2019 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import { HttpBackend } from '@angular/common/http';
  7. import { HttpEvent } from '@angular/common/http';
  8. import { HttpHeaders } from '@angular/common/http';
  9. import { HttpRequest } from '@angular/common/http';
  10. import { Observable } from 'rxjs';
  11. import { Observer } from 'rxjs';
  12. /**
  13. * Configures `HttpClientTestingBackend` as the `HttpBackend` used by `HttpClient`.
  14. *
  15. * Inject `HttpTestingController` to expect and flush requests in your tests.
  16. *
  17. * @publicApi
  18. */
  19. export declare class HttpClientTestingModule {
  20. }
  21. /**
  22. * Controller to be injected into tests, that allows for mocking and flushing
  23. * of requests.
  24. *
  25. * @publicApi
  26. */
  27. export declare abstract class HttpTestingController {
  28. /**
  29. * Search for requests that match the given parameter, without any expectations.
  30. */
  31. abstract match(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean)): TestRequest[];
  32. /**
  33. * Expect that a single request has been made which matches the given URL, and return its
  34. * mock.
  35. *
  36. * If no such request has been made, or more than one such request has been made, fail with an
  37. * error message including the given request description, if any.
  38. */
  39. abstract expectOne(url: string, description?: string): TestRequest;
  40. /**
  41. * Expect that a single request has been made which matches the given parameters, and return
  42. * its mock.
  43. *
  44. * If no such request has been made, or more than one such request has been made, fail with an
  45. * error message including the given request description, if any.
  46. */
  47. abstract expectOne(params: RequestMatch, description?: string): TestRequest;
  48. /**
  49. * Expect that a single request has been made which matches the given predicate function, and
  50. * return its mock.
  51. *
  52. * If no such request has been made, or more than one such request has been made, fail with an
  53. * error message including the given request description, if any.
  54. */
  55. abstract expectOne(matchFn: ((req: HttpRequest<any>) => boolean), description?: string): TestRequest;
  56. /**
  57. * Expect that a single request has been made which matches the given condition, and return
  58. * its mock.
  59. *
  60. * If no such request has been made, or more than one such request has been made, fail with an
  61. * error message including the given request description, if any.
  62. */
  63. abstract expectOne(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean), description?: string): TestRequest;
  64. /**
  65. * Expect that no requests have been made which match the given URL.
  66. *
  67. * If a matching request has been made, fail with an error message including the given request
  68. * description, if any.
  69. */
  70. abstract expectNone(url: string, description?: string): void;
  71. /**
  72. * Expect that no requests have been made which match the given parameters.
  73. *
  74. * If a matching request has been made, fail with an error message including the given request
  75. * description, if any.
  76. */
  77. abstract expectNone(params: RequestMatch, description?: string): void;
  78. /**
  79. * Expect that no requests have been made which match the given predicate function.
  80. *
  81. * If a matching request has been made, fail with an error message including the given request
  82. * description, if any.
  83. */
  84. abstract expectNone(matchFn: ((req: HttpRequest<any>) => boolean), description?: string): void;
  85. /**
  86. * Expect that no requests have been made which match the given condition.
  87. *
  88. * If a matching request has been made, fail with an error message including the given request
  89. * description, if any.
  90. */
  91. abstract expectNone(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean), description?: string): void;
  92. /**
  93. * Verify that no unmatched requests are outstanding.
  94. *
  95. * If any requests are outstanding, fail with an error message indicating which requests were not
  96. * handled.
  97. *
  98. * If `ignoreCancelled` is not set (the default), `verify()` will also fail if cancelled requests
  99. * were not explicitly matched.
  100. */
  101. abstract verify(opts?: {
  102. ignoreCancelled?: boolean;
  103. }): void;
  104. }
  105. /**
  106. * Defines a matcher for requests based on URL, method, or both.
  107. *
  108. * @publicApi
  109. */
  110. export declare interface RequestMatch {
  111. method?: string;
  112. url?: string;
  113. }
  114. /**
  115. * A mock requests that was received and is ready to be answered.
  116. *
  117. * This interface allows access to the underlying `HttpRequest`, and allows
  118. * responding with `HttpEvent`s or `HttpErrorResponse`s.
  119. *
  120. * @publicApi
  121. */
  122. export declare class TestRequest {
  123. request: HttpRequest<any>;
  124. private observer;
  125. /**
  126. * Whether the request was cancelled after it was sent.
  127. */
  128. readonly cancelled: boolean;
  129. constructor(request: HttpRequest<any>, observer: Observer<HttpEvent<any>>);
  130. /**
  131. * Resolve the request by returning a body plus additional HTTP information (such as response
  132. * headers) if provided.
  133. * If the request specifies an expected body type, the body is converted into the requested type.
  134. * Otherwise, the body is converted to `JSON` by default.
  135. *
  136. * Both successful and unsuccessful responses can be delivered via `flush()`.
  137. */
  138. flush(body: ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[] | null, opts?: {
  139. headers?: HttpHeaders | {
  140. [name: string]: string | string[];
  141. };
  142. status?: number;
  143. statusText?: string;
  144. }): void;
  145. /**
  146. * Resolve the request by returning an `ErrorEvent` (e.g. simulating a network failure).
  147. */
  148. error(error: ErrorEvent, opts?: {
  149. headers?: HttpHeaders | {
  150. [name: string]: string | string[];
  151. };
  152. status?: number;
  153. statusText?: string;
  154. }): void;
  155. /**
  156. * Deliver an arbitrary `HttpEvent` (such as a progress event) on the response stream for this
  157. * request.
  158. */
  159. event(event: HttpEvent<any>): void;
  160. }
  161. /**
  162. * A testing backend for `HttpClient` which both acts as an `HttpBackend`
  163. * and as the `HttpTestingController`.
  164. *
  165. * `HttpClientTestingBackend` works by keeping a list of all open requests.
  166. * As requests come in, they're added to the list. Users can assert that specific
  167. * requests were made and then flush them. In the end, a verify() method asserts
  168. * that no unexpected requests were made.
  169. *
  170. *
  171. */
  172. export declare class ɵangular_packages_common_http_testing_testing_a implements HttpBackend, HttpTestingController {
  173. /**
  174. * List of pending requests which have not yet been expected.
  175. */
  176. private open;
  177. /**
  178. * Handle an incoming request by queueing it in the list of open requests.
  179. */
  180. handle(req: HttpRequest<any>): Observable<HttpEvent<any>>;
  181. /**
  182. * Helper function to search for requests in the list of open requests.
  183. */
  184. private _match;
  185. /**
  186. * Search for requests in the list of open requests, and return all that match
  187. * without asserting anything about the number of matches.
  188. */
  189. match(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean)): TestRequest[];
  190. /**
  191. * Expect that a single outstanding request matches the given matcher, and return
  192. * it.
  193. *
  194. * Requests returned through this API will no longer be in the list of open requests,
  195. * and thus will not match twice.
  196. */
  197. expectOne(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean), description?: string): TestRequest;
  198. /**
  199. * Expect that no outstanding requests match the given matcher, and throw an error
  200. * if any do.
  201. */
  202. expectNone(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean), description?: string): void;
  203. /**
  204. * Validate that there are no outstanding requests.
  205. */
  206. verify(opts?: {
  207. ignoreCancelled?: boolean;
  208. }): void;
  209. private descriptionFromMatcher;
  210. }
  211. export { }