Skip to content

Commit

Permalink
Merge pull request #21 from cake-contrib/feature/18/ensure-success-code
Browse files Browse the repository at this point in the history
EnsureStatusCode Extension Method Changes #18
  • Loading branch information
louisfischer committed Apr 25, 2017
2 parents adc650a + 68f4623 commit e5145ca
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Cake.Http.Tests/Unit/HttpSettingsExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 16 additions & 0 deletions src/Cake.Http/HttpSettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@ public static HttpSettings SetJsonRequestBody<T>(this HttpSettings settings, T d
return settings;
}


/// <summary>
/// Sets the EnsureSuccessStatusCode to true. This makes the httpclient throw an error if it does not return a 200 range status.
/// </summary>
/// <param name="settings">The settings.</param>
/// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
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)
Expand Down

0 comments on commit e5145ca

Please sign in to comment.