Created
August 12, 2021 21:20
-
-
Save steve-codemunkies/ebe1192617e5860d32d982d6ac374321 to your computer and use it in GitHub Desktop.
This file contains 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; | |
using FluentAssertions; | |
using HttpClientTest.Class; | |
using Moq; | |
using Xunit; | |
namespace HttpClientTest.Tests | |
{ | |
public class InternetCallerTests | |
{ | |
[Fact] | |
public async Task GivenAConfiguredHttpClient_WhenCallingAUrlAndTheResultIsTheExpectedResult_ThenTrueIsReturned() | |
{ | |
// Arrange | |
var httpClient = new HttpClient(new OkMessageHandler()); | |
var factoryMock = new Mock<IHttpClientFactory>(); | |
ICallTheInternet subject = new InternetCaller(factoryMock.Object); | |
// Act | |
var result = await subject.GetWithClient(httpClient, "https://example.com", HttpStatusCode.OK); | |
// Assert | |
result.Should().BeTrue(); | |
} | |
[Fact] | |
public async Task GivenAHttpClientFactoryConfiguredWithNamedHttpClient_WhenCallingAUrlAndTheResultIsTheExpectedResult_ThenTrueIsReturned() | |
{ | |
// Arrange | |
var httpClient = new HttpClient(new OkMessageHandler()); | |
var factoryMock = new Mock<IHttpClientFactory>(); | |
ICallTheInternet subject = new InternetCaller(factoryMock.Object); | |
var clientName = "TestClient"; | |
factoryMock.Setup(f => f.CreateClient(clientName)).Returns(httpClient); | |
// Act | |
var result = await subject.GetWithNamedClient(clientName, "https://example.com", HttpStatusCode.OK); | |
// Assert | |
result.Should().BeTrue(); | |
} | |
internal class OkMessageHandler : DelegatingHandler | |
{ | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment