-
-
Save barrett777/3798bfd2b5f22e6c9419fb2c6591c185 to your computer and use it in GitHub Desktop.
Mocking 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.Net; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace DotUpgrade.Test | |
{ | |
class AlwaysReturns : HttpMessageHandler | |
{ | |
readonly HttpStatusCode StatusCode; | |
public AlwaysReturns(HttpStatusCode statusCode) | |
{ | |
StatusCode = statusCode; | |
} | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
=> Task.FromResult(new HttpResponseMessage(StatusCode)); | |
} | |
} |
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.Net; | |
using System.Net.Http; | |
using Xunit; | |
namespace DotUpgrade.Test | |
{ | |
public class SampleTests | |
{ | |
[Fact] | |
public void Should_Return_Indicated_Code() | |
{ | |
var httpClient = new HttpClient(new AlwaysReturns(HttpStatusCode.Conflict)); | |
var response = httpClient.GetAsync("http://www.google.com").Result; | |
Assert.Equal(HttpStatusCode.Conflict, response.StatusCode); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment