-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add httpclient wrapper and extend tests
- Loading branch information
1 parent
c7090ea
commit 5d68c8d
Showing
6 changed files
with
133 additions
and
66 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using BleBoxNetSdk.AirSensor.Endpoints; | ||
using BleBoxNetSdk.AirSensor.Enums; | ||
using BleBoxNetSdk.Common.Endpoints; | ||
using BleBoxNetSdk.Common.Enums; | ||
|
||
namespace BleBoxNetSdk.Tests; | ||
|
||
public class ResponseRequestTestCases | ||
{ | ||
internal static IEnumerable<TestCaseData> GetResponses() | ||
{ | ||
//Common | ||
yield return new TestCaseData(new Info.ResponseResult(), Samples.InfoResponse); | ||
yield return new TestCaseData(new DeviceUptime.ResponseResult(), Samples.UptimeResponse); | ||
yield return new TestCaseData(new PerformUpdate.ResponseResult(), Samples.PerformUpdateResponse); | ||
yield return new TestCaseData(new NetworkInformation.ResponseResult(), Samples.NetworkInformationResponse); | ||
yield return new TestCaseData(new SetNetwork.ResponseResult(), Samples.SetAPResponse); | ||
yield return new TestCaseData(new WiFiScan.ResponseResult(), Samples.PerformWiFiScanResponse); | ||
yield return new TestCaseData(new ConnectWiFi.ResponseResult(), Samples.PerformWiFiConnectResponse); | ||
yield return new TestCaseData(new DisconnectWiFi.ResponseResult(), Samples.PerformWiFiDisconnectResponse); | ||
|
||
//AirSensor | ||
yield return new TestCaseData(new DeviceState.ResponseResult(), Samples.AirSensorDeviceStateResponse); | ||
yield return new TestCaseData(new ExtendedDeviceState.ResponseResult(), Samples.AirSensorExtendedResponse); | ||
yield return new TestCaseData(new SensorRuntime.ResponseResult(), Samples.AirSensorRuntimeResponse); | ||
yield return new TestCaseData(new ForceMeasurement.ResponseResult(), Samples.AirSensorMeasurementResponse); | ||
yield return new TestCaseData(new SettingsState.ResponseResult(), Samples.AirSensorSettingsResponse); | ||
yield return new TestCaseData(new SettingsSet.ResponseResult(), Samples.AirSensorSetSettingsResponse); | ||
} | ||
|
||
internal static IEnumerable<TestCaseData> GetRequests() | ||
{ | ||
//Common | ||
yield return new TestCaseData(new SetNetwork.Request(true, "shutterBox-g650e32d2217", "my_secret_password"), Samples.SetAPRequest); | ||
yield return new TestCaseData(new ConnectWiFi.Request("WiFi_Name", "my_secret_password"), Samples.PerformWiFiConnectRequest); | ||
|
||
//AirSensor | ||
yield return new TestCaseData(new SettingsSet.Request( | ||
"My BleBox device name", | ||
Toggle.Enabled, | ||
Toggle.Enabled, | ||
Toggle.Enabled, | ||
Geolocation.Accurate, | ||
Mounting.Outside, | ||
Toggle.Enabled), Samples.AirSensorSetSettingsRequest); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using BleBoxNetSdk.Common.Endpoints; | ||
using BleBoxNetSdk.Services; | ||
using BleBoxNetSdk.Wrappers; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
|
||
namespace BleBoxNetSdk.Tests.Services; | ||
|
||
internal class ApiHttpClientTests : ResponseRequestTestCases | ||
{ | ||
[TestCaseSource(nameof(GetResponses))] | ||
public async Task should_send_request<TResponse>(TResponse _, string json) | ||
{ | ||
var uri = new Uri("http://127.0.0.1/"); | ||
var request = new PerformUpdate.Request(); | ||
var httpClientMock = new Mock<IHttpClient>(); | ||
var apiHttpClient = PrepareApiHttpClient(httpClientMock.Object); | ||
var responseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.OK) { Content = new StringContent(json) }; | ||
httpClientMock.Setup(h => h.SendAsync(It.IsAny<HttpRequestMessage>(), CancellationToken.None)).Returns(Task.FromResult(responseMessage)); | ||
|
||
var result = await apiHttpClient.Send<TResponse>(uri, request, CancellationToken.None); | ||
|
||
result.Should().NotBeNull(); | ||
} | ||
|
||
[Test] | ||
public void should_throw_after_failed_trysends() | ||
{ | ||
var uri = new Uri("http://127.0.0.1/"); | ||
var request = new PerformUpdate.Request(); | ||
var httpClientMock = new Mock<IHttpClient>(); | ||
var apiHttpClient = PrepareApiHttpClient(httpClientMock.Object); | ||
var responseMessage = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized); | ||
var totalCalls = 3; | ||
httpClientMock.Setup(h => h.SendAsync(It.IsAny<HttpRequestMessage>(), CancellationToken.None)).Returns(Task.FromResult(responseMessage)); | ||
|
||
Assert.ThrowsAsync<Exception>( async () => await apiHttpClient.Send<PerformUpdate.ResponseResult>(uri, request, CancellationToken.None)); | ||
|
||
httpClientMock.Verify(m => m.SendAsync(It.IsAny<HttpRequestMessage>(), CancellationToken.None), Times.Exactly(totalCalls)); | ||
} | ||
|
||
private ApiHttpClient PrepareApiHttpClient(IHttpClient? httpClient = null) | ||
{ | ||
var apiHttpClient = new ApiHttpClient( | ||
httpClient ?? Mock.Of<IHttpClient>(), | ||
Mock.Of<ILogger<ApiHttpClient>>(), | ||
new Serializer()); | ||
|
||
return apiHttpClient; | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace BleBoxNetSdk.Wrappers; | ||
|
||
public interface IHttpClient | ||
{ | ||
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token = default); | ||
} | ||
|
||
public class BleHttpClient : IHttpClient | ||
{ | ||
private readonly HttpClient _httpClient; | ||
|
||
public BleHttpClient() | ||
{ | ||
_httpClient = new HttpClient(); | ||
} | ||
|
||
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token = default) | ||
=> _httpClient.SendAsync(request, token); | ||
} |