-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWLedClientPostTests.cs
More file actions
127 lines (107 loc) · 4.98 KB
/
WLedClientPostTests.cs
File metadata and controls
127 lines (107 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
namespace Kevsoft.WLED.Tests;
public class WLedClientPostTests
{
private readonly Fixture _fixture = new();
[Fact]
public async Task PostEmptyWLedRootRequestData()
{
var mockHttpMessageHandler = new MockHttpMessageHandler();
var baseUri = $"http://{Guid.NewGuid():N}.com";
mockHttpMessageHandler.AppendResponse($"{baseUri}/json");
var client = new WLedClient(mockHttpMessageHandler, baseUri);
var request = new WLedRootRequest();
await client.Post(request);
var (uri, body) = mockHttpMessageHandler.CapturedRequests.Single();
uri.Should().Be($"{baseUri}/json");
var json = JsonDocument.Parse(body!);
json.RootElement.EnumerateObject().Should().HaveCount(0);
}
[Fact]
public async Task PostEmptyStateRequestData()
{
var mockHttpMessageHandler = new MockHttpMessageHandler();
var baseUri = $"http://{Guid.NewGuid():N}.com";
mockHttpMessageHandler.AppendResponse($"{baseUri}/json/state");
var client = new WLedClient(mockHttpMessageHandler, baseUri);
var request = new StateRequest();
await client.Post(request);
var (uri, body) = mockHttpMessageHandler.CapturedRequests.Single();
uri.Should().Be($"{baseUri}/json/state");
var json = JsonDocument.Parse(body!);
json.RootElement.EnumerateObject().Should().HaveCount(0);
}
[Fact]
public async Task PostEmptySingleLedRequestData()
{
var mockHttpMessageHandler = new MockHttpMessageHandler();
var baseUri = $"http://{Guid.NewGuid():N}.com";
mockHttpMessageHandler.AppendResponse($"{baseUri}/json/state");
var client = new WLedClient(mockHttpMessageHandler, baseUri);
List<SingleLedRequest> request = [];
await client.Post(request);
var (uri, body) = mockHttpMessageHandler.CapturedRequests.Single();
uri.Should().Be($"{baseUri}/json/state");
var json = JsonDocument.Parse(body!);
// Expected Request:
// {"on":true,"seg":[{"id":0,"i":[]}]}
json.RootElement.EnumerateObject().Should().HaveCount(2);
json.RootElement.GetProperty("seg").EnumerateArray().Should().HaveCount(1);
json.RootElement.GetProperty("seg")[0].GetProperty("id").GetInt32().Should().Be(0);
json.RootElement.GetProperty("seg")[0].GetProperty("i").EnumerateArray().Should().HaveCount(0);
}
[Fact]
public async Task PostFullWLedRootResponse()
{
var mockHttpMessageHandler = new MockHttpMessageHandler();
var baseUri = $"http://{Guid.NewGuid():N}.com";
mockHttpMessageHandler.AppendResponse($"{baseUri}/json");
var client = new WLedClient(mockHttpMessageHandler, baseUri);
var response = _fixture.Create<WLedRootResponse>();
await client.Post(response);
var (uri, body) = mockHttpMessageHandler.CapturedRequests.Single();
uri.Should().Be($"{baseUri}/json");
var json = JsonDocument.Parse(body!);
var expected = JsonDocument.Parse(JsonBuilder.CreateRootResponse(response));
AssertBeEquivalentTo(json.RootElement.GetProperty("state"), expected.RootElement.GetProperty("state"));
}
[Fact]
public async Task PostFullStateResponse()
{
var mockHttpMessageHandler = new MockHttpMessageHandler();
var baseUri = $"http://{Guid.NewGuid():N}.com";
mockHttpMessageHandler.AppendResponse($"{baseUri}/json/state");
var client = new WLedClient(mockHttpMessageHandler, baseUri);
var response = _fixture.Create<StateResponse>();
await client.Post(response);
var (uri, body) = mockHttpMessageHandler.CapturedRequests.Single();
uri.Should().Be($"{baseUri}/json/state");
var json = JsonDocument.Parse(body!);
var expected = JsonDocument.Parse(JsonBuilder.CreateStateJson(response));
AssertBeEquivalentTo(json.RootElement, expected.RootElement);
}
private static void AssertBeEquivalentTo(JsonElement actualJsonElement, JsonElement expectedJsonElement)
{
if (expectedJsonElement.ValueKind == JsonValueKind.Object)
{
foreach (var jsonProperty in expectedJsonElement.EnumerateObject())
{
var actualValue = actualJsonElement.GetProperty(jsonProperty.Name);
AssertBeEquivalentTo(actualValue, jsonProperty.Value);
}
}
else if (expectedJsonElement.ValueKind == JsonValueKind.Array)
{
actualJsonElement.GetArrayLength().Should().Be(expectedJsonElement.GetArrayLength());
foreach (var (actual, expected) in actualJsonElement.EnumerateArray()
.Zip(expectedJsonElement.EnumerateArray()))
{
AssertBeEquivalentTo(actual, expected);
}
}
else
{
actualJsonElement.ValueKind.Should().Be(expectedJsonElement.ValueKind);
actualJsonElement.GetRawText().Should().Be(expectedJsonElement.GetRawText());
}
}
}