Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/FluentAssertions.Web/BadRequestAssertionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using FluentAssertions.Web;
using System.Diagnostics;

// ReSharper disable once CheckNamespace
namespace FluentAssertions
{
/// <summary>
/// Contains extension methods for custom assertions in unit tests related to <see cref="BadRequestAssertions"/>.
/// </summary>
[DebuggerNonUserCode]
public static class BadRequestAssertionsExtensions
{
/// <summary>
/// Asserts that a Bad Request HTTP response content contains an error message identifiable by an expected field name and a wildcard error text.
/// </summary>
/// <remarks>
/// This assertion considers the HTTP response content a JSON generated by the ASP.NET Core 3.0 framework or less
/// </remarks>
/// <param name="expectedErrorField">
/// The expected field name.
/// </param>
/// <param name="expectedWildcardErrorMessage">
/// The wildcard pattern with which the error field associated error message is matched, where * and ? have special meanings.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public static AndConstraint<BadRequestAssertions> HaveError(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573
string expectedErrorField, string expectedWildcardErrorMessage,
string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).HaveError(expectedErrorField, expectedWildcardErrorMessage, because, becauseArgs);

/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// This assertion considers the HTTP response content a JSON generated by the ASP.NET Core 3.0 framework or less
/// </remarks>
/// <param name="expectedErrorField">
/// The expected field name.
/// </param>
/// <param name="expectedWildcardErrorMessage">
/// The wildcard pattern with which the error field associated error message is matched, where * and ? have special meanings.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public static AndConstraint<BadRequestAssertions> OnlyHaveError(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573
string expectedErrorField, string expectedWildcardErrorMessage,
string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).OnlyHaveError(expectedErrorField, expectedWildcardErrorMessage, because, becauseArgs);

/// <summary>
/// Asserts that a Bad Request HTTP response content does not contain an error message identifiable by an expected field name.
/// </summary>
/// <remarks>
/// This assertion considers the HTTP response content a JSON generated by the ASP.NET Core 3.0 framework or less
/// </remarks>
/// <param name="expectedErrorField">
/// The expected field name.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public static AndConstraint<BadRequestAssertions> NotHaveError(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573
string expectedErrorField, string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).NotHaveError(expectedErrorField, because, becauseArgs);

/// <summary>
/// Asserts that a Bad Request HTTP response content contains an error message identifiable by an wildcard error text.
/// </summary>
/// <param name="expectedWildcardErrorMessage">
/// The wildcard pattern with which the error field associated error message is matched, where * and ? have special meanings.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public static AndConstraint<BadRequestAssertions> HaveErrorMessage(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573
string expectedWildcardErrorMessage,
string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).HaveErrorMessage(expectedWildcardErrorMessage, because, becauseArgs);
}
}
2 changes: 1 addition & 1 deletion src/FluentAssertions.Web/FluentAssertions.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.1.0" />
<PackageReference Include="FluentAssertions" Version="6.4.0" />
<PackageReference Include="System.Text.Json" Version="5.0.1" />
</ItemGroup>
</Project>
54 changes: 54 additions & 0 deletions src/FluentAssertions.Web/HaveHeaderAssertionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using FluentAssertions.Web;
using System.Diagnostics;

namespace FluentAssertions
{
/// <summary>
/// Contains extension methods for custom assertions in unit tests related to <see cref="HeadersAssertions"/>.
/// </summary>
[DebuggerNonUserCode]
public static class HeadersAssertionsExtensions
{
/// <summary>
/// Asserts that an HTTP response has a named header.
/// </summary>
/// <param name="expectedHeader">
/// The expected header with which the HTTP headers list is matched.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public static AndConstraint<HeadersAssertions> HaveHeader(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573
string expectedHeader,
string because = "", params object[] becauseArgs)
=> new HttpResponseMessageAssertions(parent.Subject).HaveHeader(expectedHeader, because, becauseArgs);

/// <summary>
/// Asserts that an HTTP response does not have a named header.
/// </summary>
/// <param name="expectedHeader">
/// The expected header with which the HTTP headers list is matched.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public static AndConstraint<HttpResponseMessageAssertions> NotHaveHeader(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573
string expectedHeader,
string because = "", params object[] becauseArgs)
=> new HttpResponseMessageAssertions(parent.Subject).NotHaveHeader(expectedHeader, because, becauseArgs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using FluentAssertions.Web;
using System.Diagnostics;

namespace FluentAssertions
{
/// <summary>
/// Contains extension methods for custom assertions in unit tests related to Http Response Content Assertions
/// </summary>
[DebuggerNonUserCode]
public static class HttpResponseContentAssertionsExtensions
{
/// <summary>
/// Asserts that HTTP response content can be an equivalent representation of the expected model.
/// </summary>
/// <param name="expectedModel">
/// The expected model.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public static AndConstraint<HttpResponseMessageAssertions> BeAs<TModel>(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573
TModel expectedModel, string because = "", params object[] becauseArgs)
=> new HttpResponseMessageAssertions(parent.Subject).BeAs(expectedModel, because, becauseArgs);

/// <summary>
/// Asserts that HTTP response has content that matches a wildcard pattern.
/// </summary>
/// <param name="expectedWildcardText">
/// The wildcard pattern with which the subject is matched, where * and ? have special meanings.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
public static AndConstraint<HttpResponseMessageAssertions> MatchInContent(
#pragma warning disable 1573
this Primitives.HttpResponseMessageAssertions<Primitives.HttpResponseMessageAssertions> parent,
#pragma warning restore 1573,
string expectedWildcardText, string because = "", params object[] becauseArgs)
=> new HttpResponseMessageAssertions(parent.Subject).MatchInContent(expectedWildcardText, because, becauseArgs);
}
}

This file was deleted.

5 changes: 5 additions & 0 deletions src/FluentAssertions.Web/HttpStatusCodeAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public partial class HttpResponseMessageAssertions
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
// ReSharper disable once InconsistentNaming
public AndConstraint<HttpResponseMessageAssertions> Be1XXInformational(string because = "", params object[] becauseArgs)
{
ExecuteSubjectNotNull(because, becauseArgs);
Expand All @@ -43,6 +44,7 @@ public AndConstraint<HttpResponseMessageAssertions> Be1XXInformational(string be
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
// ReSharper disable once InconsistentNaming
public AndConstraint<HttpResponseMessageAssertions> Be2XXSuccessful(string because = "", params object[] becauseArgs)
{
ExecuteSubjectNotNull(because, becauseArgs);
Expand All @@ -69,6 +71,7 @@ public AndConstraint<HttpResponseMessageAssertions> Be2XXSuccessful(string becau
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
// ReSharper disable once InconsistentNaming
public AndConstraint<HttpResponseMessageAssertions> Be3XXRedirection(string because = "", params object[] becauseArgs)
{
ExecuteSubjectNotNull(because, becauseArgs);
Expand All @@ -95,6 +98,7 @@ public AndConstraint<HttpResponseMessageAssertions> Be3XXRedirection(string beca
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
// ReSharper disable once InconsistentNaming
public AndConstraint<HttpResponseMessageAssertions> Be4XXClientError(string because = "", params object[] becauseArgs)
{
ExecuteSubjectNotNull(because, becauseArgs);
Expand All @@ -121,6 +125,7 @@ public AndConstraint<HttpResponseMessageAssertions> Be4XXClientError(string beca
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <see paramref="because" />.
/// </param>
// ReSharper disable once InconsistentNaming
public AndConstraint<HttpResponseMessageAssertions> Be5XXServerError(string because = "", params object[] becauseArgs)
{
ExecuteSubjectNotNull(because, becauseArgs);
Expand Down
Loading