feat: add built-in HTTP response assertions#4793
Conversation
Add comprehensive HttpResponseMessage assertions so users don't need to create their own for common HTTP testing patterns. Closes #4769. New assertions: - Specific status codes: IsOk, IsCreated, IsNoContent, IsBadRequest, IsUnauthorized, IsForbidden, IsNotFound, IsConflict - Parameterized: HasStatusCode(HttpStatusCode) - Range checks: IsRedirectStatusCode, IsClientErrorStatusCode, IsServerErrorStatusCode - Content: HasJsonContent, HasContentType(string) - Headers: HasHeader(string) - checks both response and content headers Also updates CloudShop example to use the new built-in assertions, removing the 7 custom assertions that are no longer needed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Code reviewThis PR adds excellent HTTP response assertions that will be very useful! The implementation follows TUnit patterns well and the test coverage is comprehensive. However, there's one blocking issue that needs to be addressed before merging: Missing Snapshot TestsIssue: The PR adds 14 new methods with CLAUDE.md Rule 2 (line 11):
What's needed: Following the established pattern (see
Why this matters: The Example test file structure: public class HttpResponseMessageAssertionGeneratorTests : TestsBase<AssertionDataSourceDrivenGenerator>
{
[Test]
public Task HttpResponseMessage_Assertions() => RunTest(Path.Combine(Git.RootDirectory.FullName,
"TUnit.Assertions",
"Conditions",
"HttpResponseMessageAssertionExtensions.cs"));
}After fixing: This will be ready to merge! The feature itself is well-designed and the HTTP assertions will be a great addition to TUnit. |
Summary
HttpResponseMessageassertions toTUnit.Assertionsso users don't need to create their own for common HTTP integration testing patternsCloses #4769
New assertions
IsOk(),IsCreated(),IsNoContent(),IsBadRequest(),IsUnauthorized(),IsForbidden(),IsNotFound(),IsConflict()HasStatusCode(HttpStatusCode)IsRedirectStatusCode(),IsClientErrorStatusCode(),IsServerErrorStatusCode()HasJsonContent(),HasContentType(string)HasHeader(string)— checks both response and content headersUsage
Note on CloudShop example
The CloudShop example's
CloudShop.Tests.csprojwas changed from a NuGetPackageReferenceto aProjectReferenceso it can use the new built-in assertions before the next release. This should be reverted back to aPackageReferencewhen the next TUnit version is published.Test plan
HttpResponseMessageAssertionTestspassHttpStatusCodeAssertionTestsstill pass (7/7)TUnit.Assertions.Testsbuilds with 0 errors🤖 Generated with Claude Code