Created
March 24, 2013 02:45
-
-
Save dotMorten/5230251 to your computer and use it in GitHub Desktop.
Custom Http message handler used for mocking web responses in UnitTests relying on HttpClient. Add a resource file named "TestResponses.resw" and add your responses in this, and you can simply use TestMessageHandler.FromResourceResponse(key) and use this handler in you HttpClient.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Windows.ApplicationModel.Resources; | |
namespace TestHarness | |
{ | |
/// <summary> | |
/// A message handler for simulating web requests | |
/// </summary> | |
public class TestMessageHandler : HttpMessageHandler | |
{ | |
//Holds predefined test responses in TestResponses.resw | |
private static ResourceLoader s_TestResponses; | |
public static TestMessageHandler FromResourceResponse(string key) | |
{ | |
if(s_TestResponses == null) | |
s_TestResponses = new Windows.ApplicationModel.Resources.ResourceLoader("TestResponses"); | |
var testResponse = s_TestResponses.GetString(key); | |
return new TestMessageHandler(testResponse); | |
} | |
private HttpResponseMessage m_response; | |
private Exception m_exception; | |
/// <summary> | |
/// Always returns the provided response when using the message handler as StringContent. | |
/// </summary> | |
/// <param name="response"></param> | |
public TestMessageHandler(string response) | |
{ | |
m_response = new HttpResponseMessage(System.Net.HttpStatusCode.OK) | |
{ | |
Content = new System.Net.Http.StringContent(response) | |
}; | |
} | |
/// <summary> | |
/// Always returns the provided response when using the message handler. | |
/// </summary> | |
/// <param name="response"></param> | |
public TestMessageHandler(HttpContent response) | |
{ | |
m_response = new HttpResponseMessage(HttpStatusCode.OK) | |
{ | |
Content = response | |
}; | |
} | |
/// <summary> | |
/// Always returns the provided response when using the message handler. | |
/// </summary> | |
/// <param name="response"></param> | |
public TestMessageHandler(HttpResponseMessage response) | |
{ | |
m_response = response; | |
} | |
/// <summary> | |
/// Throws the provided exception when using this message handler | |
/// </summary> | |
/// <param name="ex"></param> | |
public TestMessageHandler(Exception ex) | |
{ | |
m_exception = ex; | |
} | |
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) | |
{ | |
await Task.Delay(1).ConfigureAwait(continueOnCapturedContext: false); //Simulate the request time slightly | |
if (m_exception != null) | |
throw m_exception; | |
else | |
return m_response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment