Class FaultInjectionHandler
A message handler for testing API clients that makes chosen requests fail as an unreliable network or service would: before or after they reach the service, with a response of the test's own, or by cutting off a streaming response after a number of records. Other requests are sent to the inner handler, such as the handler of a test server.
Implements
Namespace: Singulink.Net.Http.Api.Service.Testing
Assembly: Singulink.Net.Http.Api.Service.Testing.dll
Syntax
public sealed class FaultInjectionHandler : DelegatingHandler, IDisposable
Remarks
Each fault applies to the next requests that its predicate matches, as many times as its count, and faults are checked in the order they were added.
The failures are exceptions that API clients treat as transient (see ApiFailures.IsTransient).
Constructors
| Name | Description |
|---|---|
| FaultInjectionHandler() | Initializes a new instance of the FaultInjectionHandler class without an inner handler, which is set with InnerHandler. |
| FaultInjectionHandler(HttpMessageHandler) | Initializes a new instance of the FaultInjectionHandler class with an inner handler, such as the handler of a test server. |
Properties
| Name | Description |
|---|---|
| Requests | Gets the requests that the handler received, in the order it received them, whether it failed them or sent them on. They are recorded when they are received, so they stay readable after the requests are disposed. |
Methods
| Name | Description |
|---|---|
| CutResponseAfter(Func<HttpRequestMessage, bool>, int, int) | Cuts off the responses of the next requests that match after the specified number of records, which are the lines of a streaming response, as a dropped connection would: reading past them throws an HttpIOException, and the service sees the client disconnect. |
| FailAfterSending(Func<HttpRequestMessage, bool>, int) | Fails the next requests that match after the service has handled them, as if the connection dropped before the response arrived: they throw an HttpRequestException instead of returning the response. |
| FailBeforeSending(Func<HttpRequestMessage, bool>, int) | Fails the next requests that match before they are sent: they throw an HttpRequestException and never reach the service. |
| Respond(Func<HttpRequestMessage, bool>, Func<HttpRequestMessage, HttpResponseMessage>, int) | Answers the next requests that match with a response of the test's own instead of sending them, such as a |
| SendAsync(HttpRequestMessage, CancellationToken) | Sends an HTTP request to the inner handler to send to the server as an asynchronous operation. |