From 0e36272d26bd2b3604193084c4d69273a52cab06 Mon Sep 17 00:00:00 2001
From: mu88 <4560672+mu88@users.noreply.github.com>
Date: Tue, 9 Nov 2021 13:44:37 +0100
Subject: [PATCH 01/19] Introduce new assertions for StatusCode of
HttpResponseMessage (#1698)
---
Src/FluentAssertions/AssertionExtensions.cs | 19 ++
.../HttpResponseMessageAssertions.cs | 181 ++++++++++++
.../FluentAssertions/net47.verified.txt | 22 ++
.../netcoreapp2.1.verified.txt | 22 ++
.../netcoreapp3.0.verified.txt | 22 ++
.../netstandard2.0.verified.txt | 22 ++
.../netstandard2.1.verified.txt | 22 ++
Tests/Benchmarks/Benchmarks.csproj | 1 +
.../HttpResponseMessageAssertionSpecs.cs | 261 ++++++++++++++++++
9 files changed, 572 insertions(+)
create mode 100644 Src/FluentAssertions/Primitives/HttpResponseMessageAssertions.cs
create mode 100644 Tests/FluentAssertions.Specs/Primitives/HttpResponseMessageAssertionSpecs.cs
diff --git a/Src/FluentAssertions/AssertionExtensions.cs b/Src/FluentAssertions/AssertionExtensions.cs
index 9eb3e85dfc..4d1d4e6093 100644
--- a/Src/FluentAssertions/AssertionExtensions.cs
+++ b/Src/FluentAssertions/AssertionExtensions.cs
@@ -5,6 +5,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq.Expressions;
+using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml.Linq;
@@ -302,6 +303,16 @@ public static NullableBooleanAssertions Should(this bool? actualValue)
return new NullableBooleanAssertions(actualValue);
}
+ ///
+ /// Returns an object that can be used to assert the
+ /// current .
+ ///
+ [Pure]
+ public static HttpResponseMessageAssertions Should(this HttpResponseMessage actualValue)
+ {
+ return new HttpResponseMessageAssertions(actualValue);
+ }
+
///
/// Returns an object that can be used to assert the
/// current .
@@ -865,6 +876,14 @@ public static void Should(this BooleanAssertions _)
InvalidShouldCall();
}
+ ///
+ [Obsolete("You are asserting the 'AndConstraint' itself. Remove the 'Should()' method directly following 'And'", error: true)]
+ public static void Should(this HttpResponseMessageAssertions _)
+ where TAssertions : HttpResponseMessageAssertions
+ {
+ InvalidShouldCall();
+ }
+
///
[Obsolete("You are asserting the 'AndConstraint' itself. Remove the 'Should()' method directly following 'And'", error: true)]
public static void Should(this DateTimeAssertions _)
diff --git a/Src/FluentAssertions/Primitives/HttpResponseMessageAssertions.cs b/Src/FluentAssertions/Primitives/HttpResponseMessageAssertions.cs
new file mode 100644
index 0000000000..2c692ce1cd
--- /dev/null
+++ b/Src/FluentAssertions/Primitives/HttpResponseMessageAssertions.cs
@@ -0,0 +1,181 @@
+using System.Diagnostics;
+using System.Net;
+using System.Net.Http;
+using FluentAssertions.Execution;
+
+namespace FluentAssertions.Primitives
+{
+ ///
+ /// Contains a number of methods to assert that a is in the expected state.
+ ///
+ [DebuggerNonUserCode]
+ public class HttpResponseMessageAssertions
+ : HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(HttpResponseMessage value)
+ : base(value)
+ {
+ }
+ }
+
+ ///
+ /// Contains a number of methods to assert that a is in the expected state.
+ ///
+ [DebuggerNonUserCode]
+ public class HttpResponseMessageAssertions
+ where TAssertions : HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(HttpResponseMessage value) => Subject = value;
+
+ ///
+ /// Gets the object which value is being asserted.
+ ///
+ public HttpResponseMessage Subject { get; }
+
+ ///
+ /// Asserts that the is successful.
+ ///
+ ///
+ /// A formatted phrase as is supported by explaining why the assertion
+ /// is needed. If the phrase does not start with the word because, it is prepended automatically.
+ ///
+ ///
+ /// Zero or more objects to format using the placeholders in .
+ ///
+ public AndConstraint BeSuccessful(string because = "", params object[] becauseArgs)
+ {
+ Execute.Assertion
+ .ForCondition(Subject.IsSuccessStatusCode)
+ .BecauseOf(because, becauseArgs)
+ .FailWith("Expected HttpStatusCode to be successful (2xx){reason}, but found {0}.", Subject.StatusCode);
+
+ return new AndConstraint((TAssertions)this);
+ }
+
+ ///
+ /// Asserts that the is redirection.
+ ///
+ ///
+ /// A formatted phrase as is supported by explaining why the assertion
+ /// is needed. If the phrase does not start with the word because, it is prepended automatically.
+ ///
+ ///
+ /// Zero or more objects to format using the placeholders in .
+ ///
+ public AndConstraint BeRedirection(string because = "", params object[] becauseArgs)
+ {
+ Execute.Assertion
+ .ForCondition((int)Subject.StatusCode >= 300 && (int)Subject.StatusCode <= 399)
+ .BecauseOf(because, becauseArgs)
+ .FailWith("Expected HttpStatusCode to be redirection (3xx){reason}, but found {0}.", Subject.StatusCode);
+
+ return new AndConstraint((TAssertions)this);
+ }
+
+ ///
+ /// Asserts that the is either client (4xx) or server error (5xx).
+ ///
+ ///
+ /// A formatted phrase as is supported by explaining why the assertion
+ /// is needed. If the phrase does not start with the word because, it is prepended automatically.
+ ///
+ ///
+ /// Zero or more objects to format using the placeholders in .
+ ///
+ public AndConstraint BeError(string because = "", params object[] becauseArgs)
+ {
+ Execute.Assertion
+ .ForCondition(IsClientError() || IsServerError())
+ .BecauseOf(because, becauseArgs)
+ .FailWith("Expected HttpStatusCode to be an error {reason}, but found {0}.", Subject.StatusCode);
+
+ return new AndConstraint((TAssertions)this);
+ }
+
+ ///
+ /// Asserts that the is client error (4xx).
+ ///
+ ///
+ /// A formatted phrase as is supported by explaining why the assertion
+ /// is needed. If the phrase does not start with the word because, it is prepended automatically.
+ ///
+ ///
+ /// Zero or more objects to format using the placeholders in .
+ ///
+ public AndConstraint BeClientError(string because = "", params object[] becauseArgs)
+ {
+ Execute.Assertion
+ .ForCondition(IsClientError())
+ .BecauseOf(because, becauseArgs)
+ .FailWith("Expected HttpStatusCode to be client error (4xx){reason}, but found {0}.", Subject.StatusCode);
+
+ return new AndConstraint((TAssertions)this);
+ }
+
+ ///
+ /// Asserts that the is server error (5xx).
+ ///
+ ///
+ /// A formatted phrase as is supported by explaining why the assertion
+ /// is needed. If the phrase does not start with the word because, it is prepended automatically.
+ ///
+ ///
+ /// Zero or more objects to format using the placeholders in .
+ ///
+ public AndConstraint BeServerError(string because = "", params object[] becauseArgs)
+ {
+ Execute.Assertion
+ .ForCondition(IsServerError())
+ .BecauseOf(because, becauseArgs)
+ .FailWith("Expected HttpStatusCode to be server error (5xx){reason}, but found {0}.", Subject.StatusCode);
+
+ return new AndConstraint((TAssertions)this);
+ }
+
+ ///
+ /// Asserts that the value is equal to the specified value.
+ ///
+ /// The expected value
+ ///
+ /// A formatted phrase as is supported by explaining why the assertion
+ /// is needed. If the phrase does not start with the word because, it is prepended automatically.
+ ///
+ ///
+ /// Zero or more objects to format using the placeholders in .
+ ///
+ public AndConstraint HaveStatusCode(HttpStatusCode expected, string because = "", params object[] becauseArgs)
+ {
+ Execute.Assertion
+ .ForCondition(Subject.StatusCode == expected)
+ .BecauseOf(because, becauseArgs)
+ .FailWith("Expected HttpStatusCode to be {0}{reason}, but found {1}.", expected, Subject.StatusCode);
+
+ return new AndConstraint((TAssertions)this);
+ }
+
+ ///
+ /// Asserts that the value is not equal to the specified value.
+ ///
+ /// The unexpected value
+ ///
+ /// A formatted phrase as is supported by explaining why the assertion
+ /// is needed. If the phrase does not start with the word because, it is prepended automatically.
+ ///
+ ///
+ /// Zero or more objects to format using the placeholders in .
+ ///
+ public AndConstraint NotHaveStatusCode(HttpStatusCode unexpected, string because = "", params object[] becauseArgs)
+ {
+ Execute.Assertion
+ .ForCondition(Subject.StatusCode != unexpected)
+ .BecauseOf(because, becauseArgs)
+ .FailWith("Expected HttpStatusCode not to be {0}{reason}, but found {1}.", unexpected, Subject.StatusCode);
+
+ return new AndConstraint((TAssertions)this);
+ }
+
+ private bool IsServerError() => (int)Subject.StatusCode >= 500 && (int)Subject.StatusCode <= 599;
+
+ private bool IsClientError() => (int)Subject.StatusCode >= 400 && (int)Subject.StatusCode <= 499;
+ }
+}
diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt
index 1cbd954a9f..8f11ea0439 100644
--- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt
+++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt
@@ -66,6 +66,7 @@ namespace FluentAssertions
public static FluentAssertions.Primitives.NullableGuidAssertions Should(this System.Guid? actualValue) { }
public static FluentAssertions.Streams.BufferedStreamAssertions Should(this System.IO.BufferedStream actualValue) { }
public static FluentAssertions.Streams.StreamAssertions Should(this System.IO.Stream actualValue) { }
+ public static FluentAssertions.Primitives.HttpResponseMessageAssertions Should(this System.Net.Http.HttpResponseMessage actualValue) { }
public static FluentAssertions.Reflection.AssemblyAssertions Should(this System.Reflection.Assembly assembly) { }
public static FluentAssertions.Types.ConstructorInfoAssertions Should(this System.Reflection.ConstructorInfo constructorInfo) { }
public static FluentAssertions.Types.MethodInfoAssertions Should(this System.Reflection.MethodInfo methodInfo) { }
@@ -120,6 +121,10 @@ namespace FluentAssertions
where TAssertions : FluentAssertions.Primitives.GuidAssertions { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
+ public static void Should(this FluentAssertions.Primitives.HttpResponseMessageAssertions _)
+ where TAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions { }
+ [System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
+ "ly following \'And\'", true)]
public static void Should(this FluentAssertions.Primitives.SimpleTimeSpanAssertions _)
where TAssertions : FluentAssertions.Primitives.SimpleTimeSpanAssertions { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
@@ -1901,6 +1906,23 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint NotBe(string unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint NotBeEmpty(string because = "", params object[] becauseArgs) { }
}
+ public class HttpResponseMessageAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(System.Net.Http.HttpResponseMessage value) { }
+ }
+ public class HttpResponseMessageAssertions
+ where TAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(System.Net.Http.HttpResponseMessage value) { }
+ public System.Net.Http.HttpResponseMessage Subject { get; }
+ public FluentAssertions.AndConstraint BeClientError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeRedirection(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeServerError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeSuccessful(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint HaveStatusCode(System.Net.HttpStatusCode expected, string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint NotHaveStatusCode(System.Net.HttpStatusCode unexpected, string because = "", params object[] becauseArgs) { }
+ }
public class NullableBooleanAssertions : FluentAssertions.Primitives.NullableBooleanAssertions
{
public NullableBooleanAssertions(bool? value) { }
diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt
index 54d8ce0fa1..e1bce05e5c 100644
--- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt
+++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt
@@ -66,6 +66,7 @@ namespace FluentAssertions
public static FluentAssertions.Primitives.NullableGuidAssertions Should(this System.Guid? actualValue) { }
public static FluentAssertions.Streams.BufferedStreamAssertions Should(this System.IO.BufferedStream actualValue) { }
public static FluentAssertions.Streams.StreamAssertions Should(this System.IO.Stream actualValue) { }
+ public static FluentAssertions.Primitives.HttpResponseMessageAssertions Should(this System.Net.Http.HttpResponseMessage actualValue) { }
public static FluentAssertions.Reflection.AssemblyAssertions Should(this System.Reflection.Assembly assembly) { }
public static FluentAssertions.Types.ConstructorInfoAssertions Should(this System.Reflection.ConstructorInfo constructorInfo) { }
public static FluentAssertions.Types.MethodInfoAssertions Should(this System.Reflection.MethodInfo methodInfo) { }
@@ -120,6 +121,10 @@ namespace FluentAssertions
where TAssertions : FluentAssertions.Primitives.GuidAssertions { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
+ public static void Should(this FluentAssertions.Primitives.HttpResponseMessageAssertions _)
+ where TAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions { }
+ [System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
+ "ly following \'And\'", true)]
public static void Should(this FluentAssertions.Primitives.SimpleTimeSpanAssertions _)
where TAssertions : FluentAssertions.Primitives.SimpleTimeSpanAssertions { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
@@ -1901,6 +1906,23 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint NotBe(string unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint NotBeEmpty(string because = "", params object[] becauseArgs) { }
}
+ public class HttpResponseMessageAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(System.Net.Http.HttpResponseMessage value) { }
+ }
+ public class HttpResponseMessageAssertions
+ where TAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(System.Net.Http.HttpResponseMessage value) { }
+ public System.Net.Http.HttpResponseMessage Subject { get; }
+ public FluentAssertions.AndConstraint BeClientError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeRedirection(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeServerError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeSuccessful(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint HaveStatusCode(System.Net.HttpStatusCode expected, string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint NotHaveStatusCode(System.Net.HttpStatusCode unexpected, string because = "", params object[] becauseArgs) { }
+ }
public class NullableBooleanAssertions : FluentAssertions.Primitives.NullableBooleanAssertions
{
public NullableBooleanAssertions(bool? value) { }
diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt
index 3fcf43b8c0..320a3c3188 100644
--- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt
+++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt
@@ -66,6 +66,7 @@ namespace FluentAssertions
public static FluentAssertions.Primitives.NullableGuidAssertions Should(this System.Guid? actualValue) { }
public static FluentAssertions.Streams.BufferedStreamAssertions Should(this System.IO.BufferedStream actualValue) { }
public static FluentAssertions.Streams.StreamAssertions Should(this System.IO.Stream actualValue) { }
+ public static FluentAssertions.Primitives.HttpResponseMessageAssertions Should(this System.Net.Http.HttpResponseMessage actualValue) { }
public static FluentAssertions.Reflection.AssemblyAssertions Should(this System.Reflection.Assembly assembly) { }
public static FluentAssertions.Types.ConstructorInfoAssertions Should(this System.Reflection.ConstructorInfo constructorInfo) { }
public static FluentAssertions.Types.MethodInfoAssertions Should(this System.Reflection.MethodInfo methodInfo) { }
@@ -120,6 +121,10 @@ namespace FluentAssertions
where TAssertions : FluentAssertions.Primitives.GuidAssertions { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
+ public static void Should(this FluentAssertions.Primitives.HttpResponseMessageAssertions _)
+ where TAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions { }
+ [System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
+ "ly following \'And\'", true)]
public static void Should(this FluentAssertions.Primitives.SimpleTimeSpanAssertions _)
where TAssertions : FluentAssertions.Primitives.SimpleTimeSpanAssertions { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
@@ -1901,6 +1906,23 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint NotBe(string unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint NotBeEmpty(string because = "", params object[] becauseArgs) { }
}
+ public class HttpResponseMessageAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(System.Net.Http.HttpResponseMessage value) { }
+ }
+ public class HttpResponseMessageAssertions
+ where TAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(System.Net.Http.HttpResponseMessage value) { }
+ public System.Net.Http.HttpResponseMessage Subject { get; }
+ public FluentAssertions.AndConstraint BeClientError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeRedirection(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeServerError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeSuccessful(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint HaveStatusCode(System.Net.HttpStatusCode expected, string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint NotHaveStatusCode(System.Net.HttpStatusCode unexpected, string because = "", params object[] becauseArgs) { }
+ }
public class NullableBooleanAssertions : FluentAssertions.Primitives.NullableBooleanAssertions
{
public NullableBooleanAssertions(bool? value) { }
diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt
index 6d5c80ceb5..2dc2108562 100644
--- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt
+++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt
@@ -65,6 +65,7 @@ namespace FluentAssertions
public static FluentAssertions.Primitives.NullableGuidAssertions Should(this System.Guid? actualValue) { }
public static FluentAssertions.Streams.BufferedStreamAssertions Should(this System.IO.BufferedStream actualValue) { }
public static FluentAssertions.Streams.StreamAssertions Should(this System.IO.Stream actualValue) { }
+ public static FluentAssertions.Primitives.HttpResponseMessageAssertions Should(this System.Net.Http.HttpResponseMessage actualValue) { }
public static FluentAssertions.Reflection.AssemblyAssertions Should(this System.Reflection.Assembly assembly) { }
public static FluentAssertions.Types.ConstructorInfoAssertions Should(this System.Reflection.ConstructorInfo constructorInfo) { }
public static FluentAssertions.Types.MethodInfoAssertions Should(this System.Reflection.MethodInfo methodInfo) { }
@@ -119,6 +120,10 @@ namespace FluentAssertions
where TAssertions : FluentAssertions.Primitives.GuidAssertions { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
+ public static void Should(this FluentAssertions.Primitives.HttpResponseMessageAssertions _)
+ where TAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions { }
+ [System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
+ "ly following \'And\'", true)]
public static void Should(this FluentAssertions.Primitives.SimpleTimeSpanAssertions _)
where TAssertions : FluentAssertions.Primitives.SimpleTimeSpanAssertions { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
@@ -1854,6 +1859,23 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint NotBe(string unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint NotBeEmpty(string because = "", params object[] becauseArgs) { }
}
+ public class HttpResponseMessageAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(System.Net.Http.HttpResponseMessage value) { }
+ }
+ public class HttpResponseMessageAssertions
+ where TAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(System.Net.Http.HttpResponseMessage value) { }
+ public System.Net.Http.HttpResponseMessage Subject { get; }
+ public FluentAssertions.AndConstraint BeClientError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeRedirection(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeServerError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeSuccessful(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint HaveStatusCode(System.Net.HttpStatusCode expected, string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint NotHaveStatusCode(System.Net.HttpStatusCode unexpected, string because = "", params object[] becauseArgs) { }
+ }
public class NullableBooleanAssertions : FluentAssertions.Primitives.NullableBooleanAssertions
{
public NullableBooleanAssertions(bool? value) { }
diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt
index 530ed216a8..551c07d87a 100644
--- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt
+++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt
@@ -66,6 +66,7 @@ namespace FluentAssertions
public static FluentAssertions.Primitives.NullableGuidAssertions Should(this System.Guid? actualValue) { }
public static FluentAssertions.Streams.BufferedStreamAssertions Should(this System.IO.BufferedStream actualValue) { }
public static FluentAssertions.Streams.StreamAssertions Should(this System.IO.Stream actualValue) { }
+ public static FluentAssertions.Primitives.HttpResponseMessageAssertions Should(this System.Net.Http.HttpResponseMessage actualValue) { }
public static FluentAssertions.Reflection.AssemblyAssertions Should(this System.Reflection.Assembly assembly) { }
public static FluentAssertions.Types.ConstructorInfoAssertions Should(this System.Reflection.ConstructorInfo constructorInfo) { }
public static FluentAssertions.Types.MethodInfoAssertions Should(this System.Reflection.MethodInfo methodInfo) { }
@@ -120,6 +121,10 @@ namespace FluentAssertions
where TAssertions : FluentAssertions.Primitives.GuidAssertions { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
"ly following \'And\'", true)]
+ public static void Should(this FluentAssertions.Primitives.HttpResponseMessageAssertions _)
+ where TAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions { }
+ [System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
+ "ly following \'And\'", true)]
public static void Should(this FluentAssertions.Primitives.SimpleTimeSpanAssertions _)
where TAssertions : FluentAssertions.Primitives.SimpleTimeSpanAssertions { }
[System.Obsolete("You are asserting the \'AndConstraint\' itself. Remove the \'Should()\' method direct" +
@@ -1901,6 +1906,23 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint NotBe(string unexpected, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint NotBeEmpty(string because = "", params object[] becauseArgs) { }
}
+ public class HttpResponseMessageAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(System.Net.Http.HttpResponseMessage value) { }
+ }
+ public class HttpResponseMessageAssertions
+ where TAssertions : FluentAssertions.Primitives.HttpResponseMessageAssertions
+ {
+ public HttpResponseMessageAssertions(System.Net.Http.HttpResponseMessage value) { }
+ public System.Net.Http.HttpResponseMessage Subject { get; }
+ public FluentAssertions.AndConstraint BeClientError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeRedirection(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeServerError(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint BeSuccessful(string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint HaveStatusCode(System.Net.HttpStatusCode expected, string because = "", params object[] becauseArgs) { }
+ public FluentAssertions.AndConstraint NotHaveStatusCode(System.Net.HttpStatusCode unexpected, string because = "", params object[] becauseArgs) { }
+ }
public class NullableBooleanAssertions : FluentAssertions.Primitives.NullableBooleanAssertions
{
public NullableBooleanAssertions(bool? value) { }
diff --git a/Tests/Benchmarks/Benchmarks.csproj b/Tests/Benchmarks/Benchmarks.csproj
index 51b8040720..29d901653b 100644
--- a/Tests/Benchmarks/Benchmarks.csproj
+++ b/Tests/Benchmarks/Benchmarks.csproj
@@ -16,5 +16,6 @@
+
\ No newline at end of file
diff --git a/Tests/FluentAssertions.Specs/Primitives/HttpResponseMessageAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Primitives/HttpResponseMessageAssertionSpecs.cs
new file mode 100644
index 0000000000..51b5303649
--- /dev/null
+++ b/Tests/FluentAssertions.Specs/Primitives/HttpResponseMessageAssertionSpecs.cs
@@ -0,0 +1,261 @@
+using System;
+using System.Collections.Generic;
+using System.Net;
+using System.Net.Http;
+using Xunit;
+using Xunit.Sdk;
+
+namespace FluentAssertions.Specs.Primitives
+{
+ public class HttpResponseMessageAssertionSpecs
+ {
+ [Theory]
+ [MemberData(nameof(GetSuccessStatusCodes))]
+ public void Should_succeed_when_asserting_statuscode_is_successful(HttpStatusCode statusCodeOfResponse)
+ {
+ // Arrange
+ var testee = new HttpResponseMessage(statusCodeOfResponse);
+
+ // Act / Assert
+ testee.Should().BeSuccessful();
+ }
+
+ [Theory]
+ [MemberData(nameof(GetRedirectionStatusCodes))]
+ public void Should_succeed_when_asserting_statuscode_is_redirect(HttpStatusCode statusCodeOfResponse)
+ {
+ // Arrange
+ var testee = new HttpResponseMessage(statusCodeOfResponse);
+
+ // Act / Assert
+ testee.Should().BeRedirection();
+ }
+
+ [Theory]
+ [MemberData(nameof(GetClientErrorStatusCodes))]
+ public void Should_succeed_when_asserting_statuscode_is_client_error(HttpStatusCode statusCodeOfResponse)
+ {
+ // Arrange
+ var testee = new HttpResponseMessage(statusCodeOfResponse);
+
+ // Act / Assert
+ testee.Should().BeClientError();
+ }
+
+ [Theory]
+ [MemberData(nameof(GetServerErrorStatusCodes))]
+ public void Should_succeed_when_asserting_statuscode_is_server_error(HttpStatusCode statusCodeOfResponse)
+ {
+ // Arrange
+ var testee = new HttpResponseMessage(statusCodeOfResponse);
+
+ // Act / Assert
+ testee.Should().BeServerError();
+ }
+
+ [Theory]
+ [MemberData(nameof(GetClientErrorStatusCodes))]
+ [MemberData(nameof(GetServerErrorStatusCodes))]
+ public void Should_succeed_when_asserting_statuscode_is_error(HttpStatusCode statusCodeOfResponse)
+ {
+ // Arrange
+ var testee = new HttpResponseMessage(statusCodeOfResponse);
+
+ // Act / Assert
+ testee.Should().BeError();
+ }
+
+ [Fact]
+ public void Should_fail_when_asserting_statuscode_error_is_successful()
+ {
+ // Arrange
+ var testee = new HttpResponseMessage(HttpStatusCode.Gone);
+
+ // Act
+ Action action = () => testee.Should().BeSuccessful();
+
+ // Assert
+ action.Should().Throw();
+ }
+
+ [Fact]
+ public void Should_fail_with_descriptive_message_when_asserting_statuscode_error_is_successful()
+ {
+ // Act
+ Action action = () =>
+ new HttpResponseMessage(HttpStatusCode.Gone).Should().BeSuccessful("because we want to test the failure {0}", "message");
+
+ // Assert
+ action
+ .Should().Throw()
+ .WithMessage("Expected HttpStatusCode to be successful (2xx) because we want to test the failure message, but found HttpStatusCode.Gone {value: 410}.");
+ }
+
+ [Fact]
+ public void Should_fail_when_asserting_statuscode_error_is_redirection()
+ {
+ // Arrange
+ var testee = new HttpResponseMessage(HttpStatusCode.Gone);
+
+ // Act
+ Action action = () => testee.Should().BeRedirection();
+
+ // Assert
+ action.Should().Throw();
+ }
+
+ [Fact]
+ public void Should_fail_with_descriptive_message_when_asserting_statuscode_error_is_redirection()
+ {
+ // Act
+ Action action = () =>
+ new HttpResponseMessage(HttpStatusCode.Gone).Should().BeRedirection("because we want to test the failure {0}", "message");
+
+ // Assert
+ action
+ .Should().Throw()
+ .WithMessage("Expected HttpStatusCode to be redirection (3xx) because we want to test the failure message, but found HttpStatusCode.Gone {value: 410}.");
+ }
+
+ [Fact]
+ public void Should_fail_when_asserting_statuscode_success_is_client_error()
+ {
+ // Arrange
+ var testee = new HttpResponseMessage(HttpStatusCode.OK);
+
+ // Act
+ Action action = () => testee.Should().BeClientError();
+
+ // Assert
+ action.Should().Throw();
+ }
+
+ [Fact]
+ public void Should_fail_with_descriptive_message_when_asserting_statuscode_success_is_client_error()
+ {
+ // Act
+ Action action = () =>
+ new HttpResponseMessage(HttpStatusCode.OK).Should().BeClientError("because we want to test the failure {0}", "message");
+
+ // Assert
+ action
+ .Should().Throw()
+ .WithMessage("Expected HttpStatusCode to be client error (4xx) because we want to test the failure message, but found HttpStatusCode.OK {value: 200}.");
+ }
+
+ [Fact]
+ public void Should_fail_when_asserting_statuscode_success_is_server_error()
+ {
+ // Arrange
+ var testee = new HttpResponseMessage(HttpStatusCode.OK);
+
+ // Act
+ Action action = () => testee.Should().BeServerError();
+
+ // Assert
+ action.Should().Throw();
+ }
+
+ [Fact]
+ public void Should_fail_with_descriptive_message_when_asserting_statuscode_success_is_server_error()
+ {
+ // Act
+ Action action = () =>
+ new HttpResponseMessage(HttpStatusCode.OK).Should().BeServerError("because we want to test the failure {0}", "message");
+
+ // Assert
+ action
+ .Should().Throw()
+ .WithMessage("Expected HttpStatusCode to be server error (5xx) because we want to test the failure message, but found HttpStatusCode.OK {value: 200}.");
+ }
+
+ [Fact]
+ public void Should_succeed_when_asserting_statuscode_to_be_equal_to_the_same_value()
+ {
+ // Act
+ Action action = () =>
+ new HttpResponseMessage(HttpStatusCode.OK).Should().HaveStatusCode(HttpStatusCode.OK);
+
+ // Assert
+ action.Should().NotThrow();
+ }
+
+ [Fact]
+ public void Should_fail_when_asserting_statuscode_to_be_equal_to_a_different_value()
+ {
+ // Act
+ Action action = () =>
+ new HttpResponseMessage(HttpStatusCode.OK).Should().HaveStatusCode(HttpStatusCode.Gone);
+
+ // Assert
+ action.Should().Throw();
+ }
+
+ [Fact]
+ public void Should_fail_with_descriptive_message_when_asserting_statuscode_value_to_be_equal_to_a_different_value()
+ {
+ // Act
+ Action action = () =>
+ new HttpResponseMessage(HttpStatusCode.OK).Should().HaveStatusCode(HttpStatusCode.Gone, "because we want to test the failure {0}", "message");
+
+ // Assert
+ action.Should().Throw()
+ .WithMessage("Expected HttpStatusCode to be HttpStatusCode.Gone {value: 410} because we want to test the failure message, but found HttpStatusCode.OK {value: 200}.*");
+ }
+
+ [Fact]
+ public void Should_succeed_when_asserting_statuscode_value_not_to_be_equal_to_the_same_value()
+ {
+ // Act
+ Action action = () =>
+ new HttpResponseMessage(HttpStatusCode.OK).Should().NotHaveStatusCode(HttpStatusCode.Gone);
+
+ // Assert
+ action.Should().NotThrow();
+ }
+
+ [Fact]
+ public void Should_fail_when_asserting_statuscode_value_not_to_be_equal_to_a_different_value()
+ {
+ // Act
+ Action action = () =>
+ new HttpResponseMessage(HttpStatusCode.OK).Should().NotHaveStatusCode(HttpStatusCode.OK);
+
+ // Assert
+ action.Should().Throw();
+ }
+
+ [Fact]
+ public void Should_fail_with_descriptive_message_when_asserting_statuscode_value_not_to_be_equal_to_a_different_value()
+ {
+ // Act
+ Action action = () =>
+ new HttpResponseMessage(HttpStatusCode.OK).Should().NotHaveStatusCode(HttpStatusCode.OK, "because we want to test the failure {0}", "message");
+
+ // Assert
+ action.Should().Throw()
+ .WithMessage("Expected HttpStatusCode not to be HttpStatusCode.OK {value: 200} because we want to test the failure message, but found HttpStatusCode.OK {value: 200}.*");
+ }
+
+ public static IEnumerable