Skip to content

Commit fd6bf62

Browse files
authored
Merge pull request #116 from adrianiftode/be-value
Add Should().HaveHeader().And.BeValue(...)
2 parents 91f1172 + 55a413a commit fd6bf62

6 files changed

Lines changed: 276 additions & 83 deletions

File tree

readme.md

Lines changed: 80 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -236,87 +236,88 @@ NewtonsoftJsonSerializerConfig.Options.Converters.Add(new YesNoBooleanJsonConver
236236

237237
## Full API
238238

239-
| *HttpResponseMessageAssertions* | Contains a number of methods to assert that an HttpResponseMessage is in the expected state related to the HTTP content. |
239+
| *HttpResponseMessageAssertions* | Contains a number of methods to assert that an HttpResponseMessage is in the expected state related to the HTTP content. |
240240
| --- | --- |
241-
| **Should().BeAs<TModel>()** | Asserts that HTTP response content can be an equivalent representation of the expected model. |
242-
| **Should().HaveHeader()** | Asserts that an HTTP response has a named header. |
243-
| **Should().NotHaveHeader()** | Asserts that an HTTP response does not have a named header. |
244-
| **Should().HaveHttpStatus()** | Asserts that a HTTP response has a HTTP status with the specified code. |
245-
| **Should().NotHaveHttpStatus()** | that a HTTP response does not have a HTTP status with the specified code. |
246-
| **Should().MatchInContent()** | Asserts that HTTP response has content that matches a wildcard pattern. |
247-
| **Should().Satisfy<TModel>()** | Asserts that an HTTP response content can be a model that satisfies an assertion. |
248-
| **Should().Satisfy<HttpResponseMessage>()** | Asserts that an HTTP response content can be a model that satisfies an assertion. |
249-
250-
| *Should().HaveHeader().And.* | Contains a number of methods to assert that an HttpResponseMessage is in the expected state related to HTTP headers. |
251-
| --- | --- |
252-
| **BeEmpty()** | Asserts that an existing HTTP header in a HTTP response has no values. |
253-
| **NotBeEmpty()** | Asserts that an existing HTTP header in a HTTP response has any values. |
254-
| **BeValues()** | Asserts that an existing HTTP header in a HTTP response has an expected list of header values. |
255-
| **Match()** | Asserts that an existing HTTP header in a HTTP response contains at least a value that matches a wildcard pattern. |
256-
257-
| *Should().Be400BadRequest().And.* | Contains a number of methods to assert that an HttpResponseMessage is in the expected state related to HTTP Bad Request response |
258-
| --- | --- |
259-
| **HaveError()** | Asserts that a Bad Request HTTP response content contains an error message identifiable by an expected field name and a wildcard error text. |
260-
| **OnlyHaveError()** | Asserts that a Bad Request HTTP response content contains only a single error message identifiable by an expected field name and a wildcard error text. |
261-
| **NotHaveError()** | Asserts that a Bad Request HTTP response content does not contain an error message identifiable by an expected field name and a wildcard error text. |
262-
| **HaveErrorMessage()** | Asserts that a Bad Request HTTP response content contains an error message identifiable by an wildcard error text. |
263-
264-
| *Fine grained status assertions.* | |
241+
| **Should().BeAs<TModel>()** | Asserts that HTTP response content can be an equivalent representation of the expected model. |
242+
| **Should().HaveHeader()** | Asserts that an HTTP response has a named header. |
243+
| **Should().NotHaveHeader()** | Asserts that an HTTP response does not have a named header. |
244+
| **Should().HaveHttpStatus()** | Asserts that a HTTP response has a HTTP status with the specified code. |
245+
| **Should().NotHaveHttpStatus()** | that a HTTP response does not have a HTTP status with the specified code. |
246+
| **Should().MatchInContent()** | Asserts that HTTP response has content that matches a wildcard pattern. |
247+
| **Should().Satisfy<TModel>()** | Asserts that an HTTP response content can be a model that satisfies an assertion. |
248+
| **Should().Satisfy<HttpResponseMessage>()** | Asserts that an HTTP response content can be a model that satisfies an assertion. |
249+
250+
| *Should().HaveHeader().And.* | Contains a number of methods to assert that an HttpResponseMessage is in the expected state related to HTTP headers. |
265251
| --- | --- |
266-
| **Should().Be1XXInformational()** | Asserts that a HTTP response has a HTTP status code representing an informational response. |
267-
| **Should().Be2XXSuccessful()** | Asserts that a HTTP response has a successful HTTP status code. |
268-
| **Should().Be4XXClientError()** | Asserts that a HTTP response has a HTTP status code representing a client error. |
269-
| **Should().Be3XXRedirection()** | Asserts that a HTTP response has a HTTP status code representing a redirection response. |
270-
| **Should().Be5XXServerError()** | Asserts that a HTTP response has a HTTP status code representing a server error. |
271-
| **Should().Be100Continue()** | Asserts that a HTTP response has the HTTP status 100 Continue |
272-
| **Should().Be101SwitchingProtocols()** | Asserts that a HTTP response has the HTTP status 101 Switching Protocols |
273-
| **Should().Be200Ok()** | Asserts that a HTTP response has the HTTP status 200 Ok |
274-
| **Should().Be201Created()** | Asserts that a HTTP response has the HTTP status 201 Created |
275-
| **Should().Be202Accepted()** | Asserts that a HTTP response has the HTTP status 202 Accepted |
276-
| **Should().Be203NonAuthoritativeInformation()** | Asserts that a HTTP response has the HTTP status 203 Non Authoritative Information |
277-
| **Should().Be204NoContent()** | Asserts that a HTTP response has the HTTP status 204 No Content |
278-
| **Should().Be205ResetContent()** | Asserts that a HTTP response has the HTTP status 205 Reset Content |
279-
| **Should().Be206PartialContent()** | Asserts that a HTTP response has the HTTP status 206 Partial Content |
280-
| **Should().Be300Ambiguous()** | Asserts that a HTTP response has the HTTP status 300 Ambiguous |
281-
| **Should().Be300MultipleChoices()** | Asserts that a HTTP response has the HTTP status 300 Multiple Choices |
282-
| **Should().Be301Moved()** | Asserts that a HTTP response has the HTTP status 301 Moved Permanently |
283-
| **Should().Be301MovedPermanently()** | Asserts that a HTTP response has the HTTP status 301 Moved Permanently |
284-
| **Should().Be302Found()** | Asserts that a HTTP response has the HTTP status 302 Found |
285-
| **Should().Be302Redirect()** | Asserts that a HTTP response has the HTTP status 302 Redirect |
286-
| **Should().Be303RedirectMethod()** | Asserts that a HTTP response has the HTTP status 303 Redirect Method |
287-
| **Should().Be303SeeOther()** | Asserts that a HTTP response has the HTTP status 303 See Other |
288-
| **Should().Be304NotModified()** | Asserts that a HTTP response has the HTTP status 304 Not Modified |
289-
| **Should().Be305UseProxy()** | Asserts that a HTTP response has the HTTP status 305 Use Proxy |
290-
| **Should().Be306Unused()** | Asserts that a HTTP response has the HTTP status 306 Unused |
291-
| **Should().Be307RedirectKeepVerb()** | Asserts that a HTTP response has the HTTP status 307 Redirect Keep Verb |
292-
| **Should().Be307TemporaryRedirect()** | Asserts that a HTTP response has the HTTP status 307 Temporary Redirect |
293-
| **Should().Be400BadRequest()** | Asserts that a HTTP response has the HTTP status 400 BadRequest |
294-
| **Should().Be401Unauthorized()** | Asserts that a HTTP response has the HTTP status 401 Unauthorized |
295-
| **Should().Be402PaymentRequired()** | Asserts that a HTTP response has the HTTP status 402 Payment Required |
296-
| **Should().Be403Forbidden()** | Asserts that a HTTP response has the HTTP status 403 Forbidden |
297-
| **Should().Be404NotFound()** | Asserts that a HTTP response has the HTTP status 404 Not Found |
298-
| **Should().Be405MethodNotAllowed()** | Asserts that a HTTP response has the HTTP status 405 Method Not Allowed |
299-
| **Should().Be406NotAcceptable()** | Asserts that a HTTP response has the HTTP status 406 Not Acceptable |
300-
| **Should().Be407ProxyAuthenticationRequired()** | Asserts that a HTTP response has the HTTP status 407 Proxy Authentication Required |
301-
| **Should().Be408RequestTimeout()** | Asserts that a HTTP response has the HTTP status 408 Request Timeout |
302-
| **Should().Be409Conflict()** | Asserts that a HTTP response has the HTTP status 409 Conflict |
303-
| **Should().Be410Gone()** | Asserts that a HTTP response has the HTTP status 410 Gone |
304-
| **Should().Be411LengthRequired()** | Asserts that a HTTP response has the HTTP status 411 Length Required |
305-
| **Should().Be412PreconditionFailed()** | Asserts that a HTTP response has the HTTP status 412 Precondition Failed |
306-
| **Should().Be413RequestEntityTooLarge()** | Asserts that a HTTP response has the HTTP status 413 Request Entity Too Large |
307-
| **Should().Be414RequestUriTooLong()** | Asserts that a HTTP response has the HTTP status 414 Request Uri Too Long |
308-
| **Should().Be415UnsupportedMediaType()** | Asserts that a HTTP response has the HTTP status 415 Unsupported Media Type |
309-
| **Should().Be416RequestedRangeNotSatisfiable()** | Asserts that a HTTP response has the HTTP status 416 Requested Range Not Satisfiable |
310-
| **Should().Be417ExpectationFailed()** | Asserts that a HTTP response has the HTTP status 417 Expectation Failed |
311-
| **Should().Be422UnprocessableEntity()** | Asserts that a HTTP response has the HTTP status 422 Unprocessable Entity |
312-
| **Should().Be426UpgradeRequired()** | Asserts that a HTTP response has the HTTP status 426 UpgradeRequired |
313-
| **Should().Be429TooManyRequests()** | Asserts that a HTTP response has the HTTP status 422 Too Many Requests |
314-
| **Should().Be500InternalServerError()** | Asserts that a HTTP response has the HTTP status 500 Internal Server Error |
315-
| **Should().Be501NotImplemented()** | Asserts that a HTTP response has the HTTP status 501 Not Implemented |
316-
| **Should().Be502BadGateway()** | Asserts that a HTTP response has the HTTP status 502 Bad Gateway |
317-
| **Should().Be503ServiceUnavailable()** | Asserts that a HTTP response has the HTTP status 503 Service Unavailable |
318-
| **Should().Be504GatewayTimeout()** | Asserts that a HTTP response has the HTTP status 504 Gateway Timeout |
319-
| **Should().Be505HttpVersionNotSupported()** | Asserts that a HTTP response has the HTTP status 505 Http Version Not Supported |
252+
| **BeEmpty()** | Asserts that an existing HTTP header in a HTTP response has no values. |
253+
| **NotBeEmpty()** | Asserts that an existing HTTP header in a HTTP response has any values. |
254+
| **BeValue()** |Asserts that an existing HTTP header in a HTTP response has an expected value. |
255+
| **BeValues()** | Asserts that an existing HTTP header in a HTTP response has an expected list of header values. |
256+
| **Match()** | Asserts that an existing HTTP header in a HTTP response contains at least a value that matches a wildcard pattern. |
257+
258+
| *Should().Be400BadRequest().And.* | Contains a number of methods to assert that an HttpResponseMessage is in the expected state related to HTTP Bad Request response |
259+
| --- | --- |
260+
| **HaveError()** | Asserts that a Bad Request HTTP response content contains an error message identifiable by an expected field name and a wildcard error text. |
261+
| **OnlyHaveError()** | Asserts that a Bad Request HTTP response content contains only a single error message identifiable by an expected field name and a wildcard error text. |
262+
| **NotHaveError()** | Asserts that a Bad Request HTTP response content does not contain an error message identifiable by an expected field name and a wildcard error text. |
263+
| **HaveErrorMessage()** | Asserts that a Bad Request HTTP response content contains an error message identifiable by an wildcard error text. |
264+
265+
| *Fine grained status assertions.* | |
266+
| --- | --- |
267+
| **Should().Be1XXInformational()** | Asserts that a HTTP response has a HTTP status code representing an informational response. |
268+
| **Should().Be2XXSuccessful()** | Asserts that a HTTP response has a successful HTTP status code. |
269+
| **Should().Be4XXClientError()** | Asserts that a HTTP response has a HTTP status code representing a client error. |
270+
| **Should().Be3XXRedirection()** | Asserts that a HTTP response has a HTTP status code representing a redirection response. |
271+
| **Should().Be5XXServerError()** | Asserts that a HTTP response has a HTTP status code representing a server error. |
272+
| **Should().Be100Continue()** | Asserts that a HTTP response has the HTTP status 100 Continue |
273+
| **Should().Be101SwitchingProtocols()** | Asserts that a HTTP response has the HTTP status 101 Switching Protocols |
274+
| **Should().Be200Ok()** | Asserts that a HTTP response has the HTTP status 200 Ok |
275+
| **Should().Be201Created()** | Asserts that a HTTP response has the HTTP status 201 Created |
276+
| **Should().Be202Accepted()** | Asserts that a HTTP response has the HTTP status 202 Accepted |
277+
| **Should().Be203NonAuthoritativeInformation()** | Asserts that a HTTP response has the HTTP status 203 Non Authoritative Information |
278+
| **Should().Be204NoContent()** | Asserts that a HTTP response has the HTTP status 204 No Content |
279+
| **Should().Be205ResetContent()** | Asserts that a HTTP response has the HTTP status 205 Reset Content |
280+
| **Should().Be206PartialContent()** | Asserts that a HTTP response has the HTTP status 206 Partial Content |
281+
| **Should().Be300Ambiguous()** | Asserts that a HTTP response has the HTTP status 300 Ambiguous |
282+
| **Should().Be300MultipleChoices()** | Asserts that a HTTP response has the HTTP status 300 Multiple Choices |
283+
| **Should().Be301Moved()** | Asserts that a HTTP response has the HTTP status 301 Moved Permanently |
284+
| **Should().Be301MovedPermanently()** | Asserts that a HTTP response has the HTTP status 301 Moved Permanently |
285+
| **Should().Be302Found()** | Asserts that a HTTP response has the HTTP status 302 Found |
286+
| **Should().Be302Redirect()** | Asserts that a HTTP response has the HTTP status 302 Redirect |
287+
| **Should().Be303RedirectMethod()** | Asserts that a HTTP response has the HTTP status 303 Redirect Method |
288+
| **Should().Be303SeeOther()** | Asserts that a HTTP response has the HTTP status 303 See Other |
289+
| **Should().Be304NotModified()** | Asserts that a HTTP response has the HTTP status 304 Not Modified |
290+
| **Should().Be305UseProxy()** | Asserts that a HTTP response has the HTTP status 305 Use Proxy |
291+
| **Should().Be306Unused()** | Asserts that a HTTP response has the HTTP status 306 Unused |
292+
| **Should().Be307RedirectKeepVerb()** | Asserts that a HTTP response has the HTTP status 307 Redirect Keep Verb |
293+
| **Should().Be307TemporaryRedirect()** | Asserts that a HTTP response has the HTTP status 307 Temporary Redirect |
294+
| **Should().Be400BadRequest()** | Asserts that a HTTP response has the HTTP status 400 BadRequest |
295+
| **Should().Be401Unauthorized()** | Asserts that a HTTP response has the HTTP status 401 Unauthorized |
296+
| **Should().Be402PaymentRequired()** | Asserts that a HTTP response has the HTTP status 402 Payment Required |
297+
| **Should().Be403Forbidden()** | Asserts that a HTTP response has the HTTP status 403 Forbidden |
298+
| **Should().Be404NotFound()** | Asserts that a HTTP response has the HTTP status 404 Not Found |
299+
| **Should().Be405MethodNotAllowed()** | Asserts that a HTTP response has the HTTP status 405 Method Not Allowed |
300+
| **Should().Be406NotAcceptable()** | Asserts that a HTTP response has the HTTP status 406 Not Acceptable |
301+
| **Should().Be407ProxyAuthenticationRequired()** | Asserts that a HTTP response has the HTTP status 407 Proxy Authentication Required |
302+
| **Should().Be408RequestTimeout()** | Asserts that a HTTP response has the HTTP status 408 Request Timeout |
303+
| **Should().Be409Conflict()** | Asserts that a HTTP response has the HTTP status 409 Conflict |
304+
| **Should().Be410Gone()** | Asserts that a HTTP response has the HTTP status 410 Gone |
305+
| **Should().Be411LengthRequired()** | Asserts that a HTTP response has the HTTP status 411 Length Required |
306+
| **Should().Be412PreconditionFailed()** | Asserts that a HTTP response has the HTTP status 412 Precondition Failed |
307+
| **Should().Be413RequestEntityTooLarge()** | Asserts that a HTTP response has the HTTP status 413 Request Entity Too Large |
308+
| **Should().Be414RequestUriTooLong()** | Asserts that a HTTP response has the HTTP status 414 Request Uri Too Long |
309+
| **Should().Be415UnsupportedMediaType()** | Asserts that a HTTP response has the HTTP status 415 Unsupported Media Type |
310+
| **Should().Be416RequestedRangeNotSatisfiable()** | Asserts that a HTTP response has the HTTP status 416 Requested Range Not Satisfiable |
311+
| **Should().Be417ExpectationFailed()** | Asserts that a HTTP response has the HTTP status 417 Expectation Failed |
312+
| **Should().Be422UnprocessableEntity()** | Asserts that a HTTP response has the HTTP status 422 Unprocessable Entity |
313+
| **Should().Be426UpgradeRequired()** | Asserts that a HTTP response has the HTTP status 426 UpgradeRequired |
314+
| **Should().Be429TooManyRequests()** | Asserts that a HTTP response has the HTTP status 422 Too Many Requests |
315+
| **Should().Be500InternalServerError()** | Asserts that a HTTP response has the HTTP status 500 Internal Server Error |
316+
| **Should().Be501NotImplemented()** | Asserts that a HTTP response has the HTTP status 501 Not Implemented |
317+
| **Should().Be502BadGateway()** | Asserts that a HTTP response has the HTTP status 502 Bad Gateway |
318+
| **Should().Be503ServiceUnavailable()** | Asserts that a HTTP response has the HTTP status 503 Service Unavailable |
319+
| **Should().Be504GatewayTimeout()** | Asserts that a HTTP response has the HTTP status 504 Gateway Timeout |
320+
| **Should().Be505HttpVersionNotSupported()** | Asserts that a HTTP response has the HTTP status 505 Http Version Not Supported |
320321

321322

322323
### The HttpResponsesMessage assertions from FluentAssertions vs. FluentAssertions.Web

samples/Sample.Api.Shared/Controllers/CommentsController.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ public IEnumerable<Comment> Get() => new[]
1616
};
1717

1818
[HttpGet("{id}")]
19-
public Comment Get(int id) => new Comment { Author = "Adrian", Content = "Hey", CommentId = id };
19+
public Comment Get(int id)
20+
{
21+
Response.Headers.Add("x-vendor", "vendor");
22+
return new Comment { Author = "Adrian", Content = "Hey", CommentId = id };
23+
}
2024

2125
[HttpPost]
2226
public Comment Post([FromBody] Comment value) => value;

samples/Sample.Api.Tests/CommentsControllerTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ public async Task Get_WithCommentId_Returns_Response_That_Satisfies_Several_Asse
106106
);
107107
}
108108

109+
[Fact]
110+
public async Task Get_Returns_Response_With_A_Certain_Header()
111+
{
112+
// Arrange
113+
var client = _factory.CreateClient();
114+
115+
// Act
116+
var response = await client.GetAsync("/api/comments/1");
117+
118+
// Assert
119+
response.Should().HaveHeader("x-vendor").And.BeValue("vendor", "we want to test the header has a specific value");
120+
}
121+
109122
[Fact]
110123
public async Task Post_ReturnsOk()
111124
{

0 commit comments

Comments
 (0)