Skip to content

Commit 2345a03

Browse files
authored
coverage: add missing Web tests (#618)
Adds missing unit-test coverage for `It.IsHttpContent()` string-body matching `ToString()` descriptions in the Web test suite. ### Key Changes: - Added `ToString()` tests for `WithString(...).Exactly().IgnoringCase()`. - Added `ToString()` tests for `WithStringMatching(...).AsRegex(...)` with `IgnoringCase()` and with explicit `RegexOptions`.
1 parent 2b72467 commit 2345a03

2 files changed

Lines changed: 67 additions & 13 deletions

File tree

Source/Mockolate/Web/HttpClientExtensions.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,9 @@ public void InvokeCallbacks(object? value)
7676

7777
/// <inheritdoc cref="object.ToString()" />
7878
public override string ToString()
79-
{
80-
string prefix = $"{method.Method}-Request";
81-
if (parameters.Length == 0)
82-
{
83-
return prefix;
84-
}
85-
86-
return $"{prefix} with {string.Join(" and ", parameters.Select(p => p.ToString()))}";
87-
}
79+
// At least one parameter is always present: all construction sites in HttpClientExtensions
80+
// (GetAsync, PostAsync, PutAsync, PatchAsync, DeleteAsync) pass at least one IHttpRequestMessageParameter.
81+
=> $"{method.Method}-Request with {string.Join(" and ", parameters.Select(p => p.ToString()))}";
8882
}
8983

9084
private interface IHttpRequestMessageParameter
@@ -118,8 +112,8 @@ public void InvokeCallbacks(HttpRequestMessage value)
118112
}
119113

120114
/// <inheritdoc cref="object.ToString()" />
121-
public override string ToString()
122-
=> parameter.ToString() ?? string.Empty;
115+
public override string? ToString()
116+
=> parameter.ToString();
123117
}
124118

125119
private sealed class HttpStringUriParameter(IParameter<string?> parameter)

Tests/Mockolate.Tests/Web/ItExtensionsTests.IsHttpContentTests.ToStringTests.cs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.RegularExpressions;
12
using Mockolate.Web;
23

34
namespace Mockolate.Tests.Web;
@@ -111,11 +112,14 @@ public async Task WithFormData_StringMultiple_ShouldReturnExpectedValue()
111112
public async Task WithHeaders_Multiple_ShouldReturnExpectedValue()
112113
{
113114
ItExtensions.IHttpContentParameter sut =
114-
It.IsHttpContent().WithHeaders(("x-my-header", "my-value"), ("x-my-other-header", "my-other-value"));
115+
It.IsHttpContent()
116+
.WithHeaders(("x-my-header", "my-value"), ("x-my-other-header", "my-other-value"));
115117

116118
string? result = sut.ToString();
117119

118-
await That(result).IsEqualTo("Http content with headers \"x-my-header: my-value\", \"x-my-other-header: my-other-value\"");
120+
await That(result)
121+
.IsEqualTo(
122+
"Http content with headers \"x-my-header: my-value\", \"x-my-other-header: my-other-value\"");
119123
}
120124

121125
[Fact]
@@ -161,6 +165,17 @@ public async Task WithMediaTypeViaFactory_ShouldReturnExpectedValue()
161165
await That(result).IsEqualTo("Http content with media type \"application/json\"");
162166
}
163167

168+
[Fact]
169+
public async Task WithString_Exactly_IgnoringCase_ShouldReturnExpectedValue()
170+
{
171+
ItExtensions.IStringContentBodyParameter sut = It.IsHttpContent().WithString("foo").Exactly()
172+
.IgnoringCase();
173+
174+
string? result = sut.ToString();
175+
176+
await That(result).IsEqualTo("string content equal to \"foo\" ignoring case");
177+
}
178+
164179
[Fact]
165180
public async Task WithString_Exactly_ShouldReturnExpectedValue()
166181
{
@@ -192,6 +207,29 @@ public async Task WithString_ShouldReturnExpectedValue()
192207
await That(result).IsEqualTo("string content containing \"foo\"");
193208
}
194209

210+
[Fact]
211+
public async Task WithStringMatching_AsRegex_IgnoringCase_ShouldReturnExpectedValue()
212+
{
213+
ItExtensions.IStringContentBodyParameter sut =
214+
It.IsHttpContent().WithStringMatching("^foo").AsRegex().IgnoringCase();
215+
216+
string? result = sut.ToString();
217+
218+
await That(result).IsEqualTo("string content matching regex pattern \"^foo\" ignoring case");
219+
}
220+
221+
[Fact]
222+
public async Task WithStringMatching_AsRegex_IgnoringCase_WithOptions_ShouldReturnExpectedValue()
223+
{
224+
ItExtensions.IStringContentBodyParameter sut =
225+
It.IsHttpContent().WithStringMatching("^foo").AsRegex(RegexOptions.Multiline).IgnoringCase();
226+
227+
string? result = sut.ToString();
228+
229+
await That(result)
230+
.IsEqualTo("string content matching regex pattern \"^foo\" ignoring case with options Multiline");
231+
}
232+
195233
[Fact]
196234
public async Task WithStringMatching_AsRegex_ShouldReturnExpectedValue()
197235
{
@@ -203,6 +241,28 @@ public async Task WithStringMatching_AsRegex_ShouldReturnExpectedValue()
203241
await That(result).IsEqualTo("string content matching regex pattern \"^foo\"");
204242
}
205243

244+
[Fact]
245+
public async Task WithStringMatching_AsRegex_WithOptions_ShouldReturnExpectedValue()
246+
{
247+
ItExtensions.IStringContentBodyParameter sut =
248+
It.IsHttpContent().WithStringMatching("^foo").AsRegex(RegexOptions.Multiline);
249+
250+
string? result = sut.ToString();
251+
252+
await That(result).IsEqualTo("string content matching regex pattern \"^foo\" with options Multiline");
253+
}
254+
255+
[Fact]
256+
public async Task WithStringMatching_IgnoringCase_ShouldReturnExpectedValue()
257+
{
258+
ItExtensions.IStringContentBodyParameter sut =
259+
It.IsHttpContent().WithStringMatching("*foo*").IgnoringCase();
260+
261+
string? result = sut.ToString();
262+
263+
await That(result).IsEqualTo("string content matching pattern \"*foo*\" ignoring case");
264+
}
265+
206266
[Fact]
207267
public async Task WithStringMatching_ShouldReturnExpectedValue()
208268
{

0 commit comments

Comments
 (0)