Class Handlers
Factory methods for standard Handler implementations.
Inheritance
Namespace: LaunchDarkly.TestHelpers.HttpTest
Assembly: LaunchDarkly.TestHelpers.dll
Syntax
public static class Handlers : Object
Properties
Default
A Handler
that does nothing but set the status to 200. Useful as a start when
chaining with Then(Handler, Handler).
Declaration
public static Handler Default { get; }
Property Value
Type | Description |
---|---|
Handler |
Methods
AddHeader(String, String)
Creates a Handler that adds a response header, without overwriting any previous values.
Declaration
public static Handler AddHeader(string name, string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | the header name |
System.String | value | the header value |
Returns
Type | Description |
---|---|
Handler | a Handler |
See Also
AsMessageHandler(Handler)
Converts a Handler to a standard .NET System.Net.Http.HttpMessageHandler.
Declaration
public static HttpMessageHandler AsMessageHandler(this Handler handler)
Parameters
Type | Name | Description |
---|---|---|
Handler | handler | the handler to execute |
Returns
Type | Description |
---|---|
System.Net.Http.HttpMessageHandler | an |
Remarks
This allows the various handlers in this library to be used as a convenient,
composable way to simulate responses from an HttpClient
using a custom
HttpMessageHandler
that does not make any network requests. This can be
useful in testing code that makes requests to a real external URI, redirecting it
to your internal fixture. It also allows simulating network errors; see
Error(Exception).
Examples
// Here we will intercept any request from the configured HttpClient so it
// receives a 503 response, while also recording requests.
var messageHandler = Handlers.Record(out var recorder)
.Then(Handlers.Status(503)).AsMessageHandler();
var httpClient = new HttpClient(messageHandler);
// ...Do something that will cause a request to be made...
var request = recorder.RequireRequest();
// Verify the properties of the request that was made
Body(String, Byte[])
Creates a Handler that sends the specified response body.
Declaration
public static Handler Body(string contentType, byte[] body)
Parameters
Type | Name | Description |
---|---|---|
System.String | contentType | response content type |
System.Byte[] | body | response body (null is equivalent to an empty array) |
Returns
Type | Description |
---|---|
Handler | a Handler |
See Also
BodyJson(String, Encoding)
Creates a Handler that sends a response body with JSON content type.
Declaration
public static Handler BodyJson(string jsonBody, Encoding encoding = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | jsonBody | the JSON data |
System.Text.Encoding | encoding | response encoding (defaults to UTF8) |
Returns
Type | Description |
---|---|
Handler | a Handler |
See Also
BodyString(String, String, Encoding)
Creates a Handler that sends the specified response body.
Declaration
public static Handler BodyString(string contentType, string body, Encoding encoding = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | contentType | response content type (used only if body is not null) |
System.String | body | response body (may be null) |
System.Text.Encoding | encoding | character encoding; if not specified, no charset will be included in the Content-Type header, but UTF8 will be used to encode the string |
Returns
Type | Description |
---|---|
Handler | a Handler |
See Also
Delay(TimeSpan)
Creates a Handler that sleeps for the specified amount of time.
Declaration
public static Handler Delay(TimeSpan delay)
Parameters
Type | Name | Description |
---|---|---|
System.TimeSpan | delay | how long to delay |
Returns
Type | Description |
---|---|
Handler | a Handler |
Error(Exception)
Creates a Handler that throws an exception. The effect of this depends on how the handler is being used - see Remarks.
Declaration
public static Handler Error(Exception ex)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | ex |
Returns
Type | Description |
---|---|
Handler |
Remarks
When used in a real end-to-end HTTP scenario with an HttpServer, this simply results in an HTTP 500 response. There is no way to simulate a network error in this framework.
However, when used with AsMessageHandler(Handler),
this causes the exception to be thrown out of the HttpClient
. Therefore, you can
use this handler in test code that needs to simulate an I/O error from HttpClient
.
Examples
var messageHandler = Handlers.Error(new IOException("bad hostname")).AsMessageHandler();
var httpClient = new HttpClient(messageHandler);
Hang()
Creates a Handler that sleeps indefinitely, holding the connection open, until the server is closed.
Declaration
public static Handler Hang()
Returns
Type | Description |
---|---|
Handler | a Handler |
Header(String, String)
Creates a Handler that sets a response header.
Declaration
public static Handler Header(string name, string value)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | the header name |
System.String | value | the header value |
Returns
Type | Description |
---|---|
Handler | a Handler |
Examples
var handler = Handlers.Default.Then(Handlers.Header("Etag", "123")).
Then(Handlers.JsonBody("{}"));
See Also
Record(out RequestRecorder)
Creates a RequestRecorder that captures requests.
Declaration
public static Handler Record(out RequestRecorder recorder)
Parameters
Type | Name | Description |
---|---|---|
RequestRecorder | recorder | receives the new RequestRecorder instance |
Returns
Type | Description |
---|---|
Handler | a Handler that will pass requests to the recorder |
Remarks
You won't normally need to use this directly, since the HttpServer has a RequestRecorder built in, but you could use it to capture a subset of requests.
Examples
var handler = Handlers.Record(out var recorder).Then(Handlers.Status(200));
See Also
Router(out SimpleRouter)
Creates a SimpleRouter for delegating to other handlers based on the request path.
Declaration
public static Handler Router(out SimpleRouter router)
Parameters
Type | Name | Description |
---|---|---|
SimpleRouter | router | receives the new SimpleRouter instance |
Returns
Type | Description |
---|---|
Handler |
Examples
var server = HttpServer.Start(Handlers.Router(out var router));
router.AddPath("/goodpath", Handlers.Status(200));
router.AddPath("/badpath", Handlers.Status(400));
Sequential(Handler[])
Creates a Handler that delegates to each of the specified handlers in sequence as each request is received.
Declaration
public static Handler Sequential(params Handler[] handlers)
Parameters
Type | Name | Description |
---|---|---|
Handler[] | handlers | a list of handlers |
Returns
Type | Description |
---|---|
Handler | a Handler |
Remarks
Any requests that happen after the last handler in the list has been used will receive a 500 error.
See Also
SequentialWithLastRepeating(Handler[])
Creates a Handler that delegates to each of the specified handlers in sequence as each request is received.
Declaration
public static Handler SequentialWithLastRepeating(params Handler[] handlers)
Parameters
Type | Name | Description |
---|---|---|
Handler[] | handlers | a list of handlers |
Returns
Type | Description |
---|---|
Handler | a Handler |
Remarks
Any requests that happen after the last handler in the list has been used will continue to use the last handler.
See Also
StartChunks(String, Encoding)
Creates a Handler that starts writing a chunked response.
Declaration
public static Handler StartChunks(string contentType, Encoding encoding = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | contentType | the content type |
System.Text.Encoding | encoding | character encoding to include in the Content-Type header; if not specified, Content-Type will not specify an encoding |
Returns
Type | Description |
---|---|
Handler | a Handler |
Examples
var handler = Handlers.StartChunks("text/my-stream-data")
.Then(Handlers.WriteChunkString("data1"))
.Then(Handlers.WriteChunkString("data2"));
See Also
Status(Int32)
Creates a Handler that sets the HTTP response status.
Declaration
public static Handler Status(int statusCode)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | statusCode | the status code |
Returns
Type | Description |
---|---|
Handler | a Handler |
Status(HttpStatusCode)
Creates a Handler that sets the HTTP response status.
Declaration
public static Handler Status(HttpStatusCode statusCode)
Parameters
Type | Name | Description |
---|---|---|
System.Net.HttpStatusCode | statusCode | the status code |
Returns
Type | Description |
---|---|
Handler | a Handler |
Switchable(out HandlerSwitcher)
Creates a HandlerSwitcher for changing handler behavior dynamically. It is initially set to delegate to Default.
Declaration
public static Handler Switchable(out HandlerSwitcher switchable)
Parameters
Type | Name | Description |
---|---|---|
HandlerSwitcher | switchable | receives the new HandlerSwitcher instance |
Returns
Type | Description |
---|---|
Handler |
Sync(Action<IRequestContext>)
Wraps a synchronous action in an asynchronous Handler.
Declaration
public static Handler Sync(Action<IRequestContext> action)
Parameters
Type | Name | Description |
---|---|---|
System.Action<IRequestContext> | action | the action to run |
Returns
Type | Description |
---|---|
Handler | a Handler |
Then(Handler, Handler)
Chains another handler to be executed immediately after this one.
Declaration
public static Handler Then(this Handler first, Handler second)
Parameters
Type | Name | Description |
---|---|---|
Handler | first | the first handler to execute |
Handler | second | the next handler to execute |
Returns
Type | Description |
---|---|
Handler | a Handler |
Remarks
Changing the status code or headers after a previous handler has already written a response body will not work.
Examples
var handler = Handlers.Delay(TimeSpan.FromSeconds(2)).Then(Handlers.Status(200));
WriteChunk(Byte[])
Creates a Handler that writes a chunk of response data.
Declaration
public static Handler WriteChunk(byte[] data)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | data | the chunk data |
Returns
Type | Description |
---|---|
Handler | a Handler |
See Also
WriteChunkString(String, Encoding)
Creates a Handler that writes a chunk of response data.
Declaration
public static Handler WriteChunkString(string data, Encoding encoding = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | data | the chunk data as a string |
System.Text.Encoding | encoding | character encoding to use for this chunk (defaults to UTF8) |
Returns
Type | Description |
---|---|
Handler | a Handler |