diff --git a/src/Cake.Http.Tests/Unit/HttpSettingsExtensionsTests.cs b/src/Cake.Http.Tests/Unit/HttpSettingsExtensionsTests.cs index ba2de95..5c0939d 100644 --- a/src/Cake.Http.Tests/Unit/HttpSettingsExtensionsTests.cs +++ b/src/Cake.Http.Tests/Unit/HttpSettingsExtensionsTests.cs @@ -246,6 +246,37 @@ public void Should_Add_Authorization_Header() } } + public sealed class TheEnsureSuccessCodeMethod + { + [Fact] + [Trait(Traits.TestCategory, TestCategories.Unit)] + public void Should_Throw_On_Null_Settings_Parameter() + { + //Given + HttpSettings settings = null; + + //When + var nullRecord = Record.Exception(() => HttpSettingsExtensions.EnsureSuccessStatusCode(settings)); + + //Then + CakeAssert.IsArgumentNullException(nullRecord, "settings"); + } + + [Fact] + [Trait(Traits.TestCategory, TestCategories.Unit)] + public void Should_Set_EnsureSuccesscCode_Property_To_True() + { + //Given + HttpSettings settings = new HttpSettings(); + + //When + settings.EnsureSuccessStatusCode(); + + //Then + Assert.True(settings.EnsureSuccessStatusCode); + } + } + public sealed class TheUseBasicAuthorizationMethod { [Fact] diff --git a/src/Cake.Http/HttpSettingsExtensions.cs b/src/Cake.Http/HttpSettingsExtensions.cs index 3fa4b1a..19a9a26 100644 --- a/src/Cake.Http/HttpSettingsExtensions.cs +++ b/src/Cake.Http/HttpSettingsExtensions.cs @@ -264,6 +264,22 @@ public static HttpSettings SetJsonRequestBody(this HttpSettings settings, T d return settings; } + + /// + /// Sets the EnsureSuccessStatusCode to true. This makes the httpclient throw an error if it does not return a 200 range status. + /// + /// The settings. + /// The same instance so that multiple calls can be chained. + public static HttpSettings EnsureSuccessStatusCode(this HttpSettings settings) + { + if (settings == null) + throw new ArgumentNullException(nameof(settings)); + + settings.EnsureSuccessStatusCode = true; + + return settings; + } + private static void VerifyParameters(HttpSettings settings, string name, string value) { if (settings == null)