Skip to content

Commit 7b7f776

Browse files
authored
Adds possibility to assert on content headers
* Renamed `HasHeader` to `HasRequestHeader` * Adds `HasContentHeader` extension methods * Rename assertion `WithHeader` to `WithRequestHeader` * Adds `WithContentHeader` assertions
1 parent a1cc473 commit 7b7f776

File tree

7 files changed

+421
-58
lines changed

7 files changed

+421
-58
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dotnet_style_prefer_inferred_tuple_names = true:suggestion
3434
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
3535
dotnet_style_prefer_auto_properties = true:silent
3636
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
37-
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
37+
dotnet_style_prefer_conditional_expression_over_return = false:suggestion
3838
dotnet_style_prefer_compound_assignment = true:suggestion
3939

4040
dotnet_style_coalesce_expression = true:suggestion

src/HttpClientTestHelpers/HttpRequestMessageAsserter.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,26 @@ public HttpRequestMessageAsserter WithHttpVersion(Version httpVersion)
154154
/// <summary>
155155
/// Asserts whether requests were made with a specific header name. Values are ignored.
156156
/// </summary>
157+
/// <remarks>This method only asserts headers on <see cref="System.Net.Http.Headers.HttpRequestHeader"/></remarks>
157158
/// <param name="headerName">The name of the header that is expected.</param>
158159
/// <returns>The <seealso cref="HttpRequestMessageAsserter"/> for further assertions.</returns>
159-
public HttpRequestMessageAsserter WithHeader(string headerName)
160+
public HttpRequestMessageAsserter WithRequestHeader(string headerName)
160161
{
161162
if (string.IsNullOrEmpty(headerName))
162163
{
163164
throw new ArgumentNullException(nameof(headerName));
164165
}
165-
return With(x => x.HasHeader(headerName), $"header '{headerName}'");
166+
return With(x => x.HasRequestHeader(headerName), $"request header '{headerName}'");
166167
}
167168

168169
/// <summary>
169170
/// Asserts whether requests were made with a specific header name and value.
170171
/// </summary>
172+
/// <remarks>This method only asserts headers on <see cref="System.Net.Http.Headers.HttpRequestHeader"/></remarks>
171173
/// <param name="headerName">The name of the header that is expected.</param>
172174
/// <param name="headerValue">The value of the expected header, supports wildcards.</param>
173175
/// <returns>The <seealso cref="HttpRequestMessageAsserter"/> for further assertions.</returns>
174-
public HttpRequestMessageAsserter WithHeader(string headerName, string headerValue)
176+
public HttpRequestMessageAsserter WithRequestHeader(string headerName, string headerValue)
175177
{
176178
if (string.IsNullOrEmpty(headerName))
177179
{
@@ -181,7 +183,42 @@ public HttpRequestMessageAsserter WithHeader(string headerName, string headerVal
181183
{
182184
throw new ArgumentNullException(nameof(headerValue));
183185
}
184-
return With(x => x.HasHeader(headerName, headerValue), $"header '{headerName}' and value '{headerValue}'");
186+
return With(x => x.HasRequestHeader(headerName, headerValue), $"request header '{headerName}' and value '{headerValue}'");
187+
}
188+
189+
/// <summary>
190+
/// Asserts whether requests were made with a specific header name. Values are ignored.
191+
/// </summary>
192+
/// <remarks>This method only asserts headers on <see cref="System.Net.Http.Headers.HttpContentHeader"/></remarks>
193+
/// <param name="headerName">The name of the header that is expected.</param>
194+
/// <returns>The <seealso cref="HttpRequestMessageAsserter"/> for further assertions.</returns>
195+
public HttpRequestMessageAsserter WithContentHeader(string headerName)
196+
{
197+
if (string.IsNullOrEmpty(headerName))
198+
{
199+
throw new ArgumentNullException(nameof(headerName));
200+
}
201+
return With(x => x.HasContentHeader(headerName), $"content header '{headerName}'");
202+
}
203+
204+
/// <summary>
205+
/// Asserts whether requests were made with a specific header name and value.
206+
/// </summary>
207+
/// <remarks>This method only asserts headers on <see cref="System.Net.Http.Headers.HttpContentHeader"/></remarks>
208+
/// <param name="headerName">The name of the header that is expected.</param>
209+
/// <param name="headerValue">The value of the expected header, supports wildcards.</param>
210+
/// <returns>The <seealso cref="HttpRequestMessageAsserter"/> for further assertions.</returns>
211+
public HttpRequestMessageAsserter WithContentHeader(string headerName, string headerValue)
212+
{
213+
if (string.IsNullOrEmpty(headerName))
214+
{
215+
throw new ArgumentNullException(nameof(headerName));
216+
}
217+
if (string.IsNullOrEmpty(headerValue))
218+
{
219+
throw new ArgumentNullException(nameof(headerValue));
220+
}
221+
return With(x => x.HasContentHeader(headerName, headerValue), $"content header '{headerName}' and value '{headerValue}'");
185222
}
186223

187224
/// <summary>
@@ -191,7 +228,7 @@ public HttpRequestMessageAsserter WithHeader(string headerName, string headerVal
191228
/// <returns>The <seealso cref="HttpRequestMessageAsserter"/> for further assertions.</returns>
192229
public HttpRequestMessageAsserter Times(int count)
193230
{
194-
if(count < 0)
231+
if (count < 0)
195232
{
196233
throw new ArgumentException("Count should not be less than zero", nameof(count));
197234
}

src/HttpClientTestHelpers/HttpRequestMessageExtensions.cs

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Net.Http;
3+
using System.Net.Http.Headers;
34
using System.Text.RegularExpressions;
45

56
namespace HttpClientTestHelpers
@@ -96,10 +97,11 @@ public static bool HasHttpMethod(this HttpRequestMessage httpRequestMessage, str
9697
/// <summary>
9798
/// Determines whether a specific header is set on a request.
9899
/// </summary>
100+
/// <remarks>This method only checks headers in <see cref="System.Net.Http.Headers.HttpRequestHeaders"/></remarks>
99101
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
100102
/// <param name="headerName">The name of the header to locate on the request.</param>
101103
/// <returns>true when the request contains a header with the specified name; otherwise, false.</returns>
102-
public static bool HasHeader(this HttpRequestMessage httpRequestMessage, string headerName)
104+
public static bool HasRequestHeader(this HttpRequestMessage httpRequestMessage, string headerName)
103105
{
104106
if (httpRequestMessage == null)
105107
{
@@ -111,17 +113,18 @@ public static bool HasHeader(this HttpRequestMessage httpRequestMessage, string
111113
throw new ArgumentNullException(nameof(headerName));
112114
}
113115

114-
return httpRequestMessage.Headers.Contains(headerName);
116+
return httpRequestMessage.Headers.HasHeader(headerName);
115117
}
116118

117119
/// <summary>
118120
/// Determines whether a specific header with a specific value is set on a request.
119121
/// </summary>
122+
/// <remarks>This method only checks headers in <see cref="System.Net.Http.Headers.HttpRequestHeaders"/></remarks>
120123
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
121124
/// <param name="headerName">The name of the header to locate on the request.</param>
122125
/// <param name="headerValue">The value the header should have. Wildcard is supported.</param>
123126
/// <returns>true when the request contains a header with the specified name and value; otherwise, false.</returns>
124-
public static bool HasHeader(this HttpRequestMessage httpRequestMessage, string headerName, string headerValue)
127+
public static bool HasRequestHeader(this HttpRequestMessage httpRequestMessage, string headerName, string headerValue)
125128
{
126129
if (httpRequestMessage == null)
127130
{
@@ -138,7 +141,77 @@ public static bool HasHeader(this HttpRequestMessage httpRequestMessage, string
138141
throw new ArgumentNullException(nameof(headerValue));
139142
}
140143

141-
if (httpRequestMessage.Headers.TryGetValues(headerName, out var values))
144+
return httpRequestMessage.Headers.HasHeader(headerName, headerValue);
145+
}
146+
147+
/// <summary>
148+
/// Determines whether a specific header is set on a request.
149+
/// </summary>
150+
/// <remarks>This method only checks headers in <see cref="System.Net.Http.Headers.HttpContentHeaders"/></remarks>
151+
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
152+
/// <param name="headerName">The name of the header to locate on the request.</param>
153+
/// <returns>true when the request contains a header with the specified name; otherwise, false.</returns>
154+
public static bool HasContentHeader(this HttpRequestMessage httpRequestMessage, string headerName)
155+
{
156+
if (httpRequestMessage == null)
157+
{
158+
throw new ArgumentNullException(nameof(httpRequestMessage));
159+
}
160+
161+
if (string.IsNullOrEmpty(headerName))
162+
{
163+
throw new ArgumentNullException(nameof(headerName));
164+
}
165+
166+
if (httpRequestMessage.Content == null)
167+
{
168+
return false;
169+
}
170+
171+
return httpRequestMessage.Content.Headers.HasHeader(headerName);
172+
}
173+
174+
/// <summary>
175+
/// Determines whether a specific header with a specific value is set on a request.
176+
/// </summary>
177+
/// <remarks>This method only checks headers in <see cref="System.Net.Http.Headers.HttpContentHeaders"/></remarks>
178+
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
179+
/// <param name="headerName">The name of the header to locate on the request.</param>
180+
/// <param name="headerValue">The value the header should have. Wildcard is supported.</param>
181+
/// <returns>true when the request contains a header with the specified name and value; otherwise, false.</returns>
182+
public static bool HasContentHeader(this HttpRequestMessage httpRequestMessage, string headerName, string headerValue)
183+
{
184+
if (httpRequestMessage == null)
185+
{
186+
throw new ArgumentNullException(nameof(httpRequestMessage));
187+
}
188+
189+
if (string.IsNullOrEmpty(headerName))
190+
{
191+
throw new ArgumentNullException(nameof(headerName));
192+
}
193+
194+
if (string.IsNullOrEmpty(headerValue))
195+
{
196+
throw new ArgumentNullException(nameof(headerValue));
197+
}
198+
199+
if (httpRequestMessage.Content == null)
200+
{
201+
return false;
202+
}
203+
204+
return httpRequestMessage.Content.Headers.HasHeader(headerName, headerValue);
205+
}
206+
207+
private static bool HasHeader(this HttpHeaders headers, string headerName)
208+
{
209+
return headers.Contains(headerName);
210+
}
211+
212+
private static bool HasHeader(this HttpHeaders headers, string headerName, string headerValue)
213+
{
214+
if (headers.TryGetValues(headerName, out var values))
142215
{
143216
var value = string.Join(" ", values);
144217
return Matches(value, headerValue);

src/HttpClientTestHelpers/TestableHttpMessageHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
2525
{
2626
httpRequestMessages.Enqueue(request);
2727

28-
if(response is TimeoutHttpResponseMessage)
28+
if (response is TimeoutHttpResponseMessage)
2929
{
3030
throw new TaskCanceledException(new OperationCanceledException().Message);
3131
}

0 commit comments

Comments
 (0)