Skip to content

Instantly share code, notes, and snippets.

@MichaelaIvanova
Created December 14, 2017 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelaIvanova/4e4ab256046694217fd69f83c7a1d646 to your computer and use it in GitHub Desktop.
Save MichaelaIvanova/4e4ab256046694217fd69f83c7a1d646 to your computer and use it in GitHub Desktop.
HttpClient Unit Testing
public interface ICustomHttpClient
{
Task<HttpResponseMessage> GetAsync(string url, int timeout);
}
//Decorating HttpClient in order to mock it
public class CustomHttpClient : ICustomHttpClient
{
public CustomHttpClient()
{
}
public async Task<HttpResponseMessage> GetAsync(string url, int timeout)
{
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromSeconds(timeout);
return await client.GetAsync(url);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment