Skip to content

Commit 7b8bff0

Browse files
committed
Move logic for assert WithHeader to custom Request class.
1 parent 7050b14 commit 7b8bff0

File tree

3 files changed

+86
-41
lines changed

3 files changed

+86
-41
lines changed

src/TestableHttpClient/HttpRequestMessageAsserter.cs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,28 @@ public HttpRequestMessageAsserter(IEnumerable<HttpRequestMessage> httpRequestMes
2929
/// </summary>
3030
public TestableHttpMessageHandlerOptions Options { get; }
3131

32+
private HttpRequestMessageAsserter Assert(int? expectedCount = null, string condition = "")
33+
{
34+
if (!string.IsNullOrEmpty(condition))
35+
{
36+
_expectedConditions.Add(condition);
37+
}
38+
Assert(expectedCount);
39+
return this;
40+
}
41+
3242
private void Assert(int? expectedCount = null)
3343
{
34-
var actualCount = Requests.Count();
44+
int actualCount;
45+
try
46+
{
47+
actualCount = Requests.Count(expectedRequest.Equals);
48+
}
49+
catch (ObjectDisposedException)
50+
{
51+
throw new HttpRequestMessageAssertionException("Can't validate requests, because one or more requests have content that is already disposed.");
52+
}
53+
3554
var pass = expectedCount switch
3655
{
3756
null => actualCount > 0,
@@ -72,15 +91,9 @@ public IHttpRequestMessagesCheck WithFilter(Func<HttpRequestMessage, bool> reque
7291
_expectedConditions.Add(condition);
7392
}
7493

75-
try
76-
{
77-
Requests = Requests.Where(requestFilter);
78-
Assert(expectedNumberOfRequests);
79-
}
80-
catch (ObjectDisposedException)
81-
{
82-
throw new HttpRequestMessageAssertionException("Can't validate requests, because one or more requests have content that is already disposed.");
83-
}
94+
Requests = Requests.Where(requestFilter);
95+
Assert(expectedNumberOfRequests);
96+
8497
return this;
8598
}
8699

@@ -112,7 +125,7 @@ private IHttpRequestMessagesCheck WithRequestUri(string pattern, int? expectedNu
112125
UriPattern uriPattern = UriPatternParser.Parse(pattern);
113126
expectedRequest = expectedRequest with { RequestUri = uriPattern };
114127

115-
return WithFilter(x => expectedRequest.Equals(x), expectedNumberOfRequests, condition);
128+
return Assert(expectedNumberOfRequests, condition);
116129
}
117130

118131
/// <summary>
@@ -134,7 +147,7 @@ private IHttpRequestMessagesCheck WithHttpMethod(HttpMethod httpMethod, int? exp
134147
{
135148
Guard.ThrowIfNull(httpMethod);
136149
expectedRequest = expectedRequest with { HttpMethod = httpMethod };
137-
return WithFilter(x => expectedRequest.Equals(x), expectedNumberOfRequests, $"HTTP Method '{httpMethod}'");
150+
return Assert(expectedNumberOfRequests, $"HTTP Method '{httpMethod}'");
138151
}
139152

140153
/// <summary>
@@ -159,7 +172,7 @@ private IHttpRequestMessagesCheck WithHttpVersion(Version httpVersion, int? expe
159172

160173
expectedRequest = expectedRequest with { HttpVersion = httpVersion };
161174

162-
return WithFilter(x => expectedRequest.Equals(x), expectedNumberOfRequests, $"HTTP Version '{httpVersion}'");
175+
return Assert(expectedNumberOfRequests, $"HTTP Version '{httpVersion}'");
163176
}
164177

165178
/// <summary>
@@ -181,7 +194,8 @@ private IHttpRequestMessagesCheck WithHeader(string headerName, int? expectedNum
181194
{
182195
Guard.ThrowIfNullOrEmpty(headerName);
183196

184-
return WithFilter(x => x.HasHeader(headerName), expectedNumberOfRequests, $"header '{headerName}'");
197+
expectedRequest = expectedRequest.AddHeader(headerName);
198+
return Assert(expectedNumberOfRequests, $"header '{headerName}'");
185199
}
186200

187201
/// <summary>
@@ -206,7 +220,9 @@ private IHttpRequestMessagesCheck WithHeader(string headerName, string headerVal
206220
Guard.ThrowIfNullOrEmpty(headerName);
207221
Guard.ThrowIfNullOrEmpty(headerValue);
208222

209-
return WithFilter(x => x.HasHeader(headerName, headerValue), expectedNumberOfRequests, $"header '{headerName}' and value '{headerValue}'");
223+
expectedRequest = expectedRequest.AddHeader(headerName, headerValue);
224+
225+
return Assert(expectedNumberOfRequests, $"header '{headerName}' and value '{headerValue}'");
210226
}
211227

212228
/// <summary>
@@ -229,6 +245,6 @@ private IHttpRequestMessagesCheck WithContent(string pattern, int? expectedNumbe
229245
Guard.ThrowIfNull(pattern);
230246

231247
expectedRequest = expectedRequest with { Content = pattern };
232-
return WithFilter(x => expectedRequest.Equals(x), expectedNumberOfRequests, $"content '{pattern}'");
248+
return Assert(expectedNumberOfRequests, $"content '{pattern}'");
233249
}
234250
}

src/TestableHttpClient/HttpRequestMessageExtensions.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/TestableHttpClient/Request.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,39 @@ public Request(UriPatternMatchingOptions uriPatternMatchingOptions)
1313
public UriPattern? RequestUri { get; init; }
1414
public Version? HttpVersion { get; init; }
1515

16+
public List<string>? HeaderNames { get; init; }
17+
public Dictionary<string, string>? HeaderValues { get; init; }
18+
1619
public string? Content { get; init; }
1720

21+
public Request AddHeader(string headerName)
22+
{
23+
if (HeaderNames is null)
24+
{
25+
List<string> headerNames = [headerName];
26+
return this with { HeaderNames = headerNames };
27+
}
28+
else
29+
{
30+
HeaderNames.Add(headerName);
31+
return this;
32+
}
33+
}
34+
35+
public Request AddHeader(string headerName, string headerValue)
36+
{
37+
if (HeaderValues is null)
38+
{
39+
Dictionary<string, string> headerValues = new() { [headerName] = headerValue };
40+
return this with { HeaderValues = headerValues };
41+
}
42+
else
43+
{
44+
HeaderValues[headerName] = headerValue;
45+
return this;
46+
}
47+
}
48+
1849
public bool Equals(HttpRequestMessage? other)
1950
{
2051
if (other is null)
@@ -37,9 +68,31 @@ public bool Equals(HttpRequestMessage? other)
3768
return false;
3869
}
3970

71+
if (HeaderNames is not null)
72+
{
73+
foreach (var headerName in HeaderNames)
74+
{
75+
if (!other.Headers.HasHeader(headerName) && (other.Content is null || !other.Content.Headers.HasHeader(headerName)))
76+
{
77+
return false;
78+
}
79+
}
80+
}
81+
82+
if (HeaderValues is not null)
83+
{
84+
foreach (var keyValuePair in HeaderValues)
85+
{
86+
if (!other.Headers.HasHeader(keyValuePair.Key, keyValuePair.Value) && (other.Content is null || !other.Content.Headers.HasHeader(keyValuePair.Key, keyValuePair.Value)))
87+
{
88+
return false;
89+
}
90+
}
91+
}
92+
4093
if (Content is not null)
4194
{
42-
if(other.Content is null)
95+
if (other.Content is null)
4396
{
4497
return false;
4598
}

0 commit comments

Comments
 (0)