Skip to content

Commit

Permalink
Created an Assertion for StatusCodeShouldBeSuccess (#161)
Browse files Browse the repository at this point in the history
* Created an Assertion for StatusCodeShouldBeSuccess

* Rebase

---------

Co-authored-by: JT <Hawxy@users.noreply.github.com>
  • Loading branch information
JeffryGonzalez and Hawxy authored Jul 26, 2024
1 parent fbdc3bc commit c99ee6d
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/Alba.Testing/Acceptance/asserting_against_status_code.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,53 @@ public async Task happily_blows_up_on_an_unexpected_500()
ex.Message.ShouldContain("Expected status code 200, but was 500");
ex.Message.ShouldContain("the error text");
}

[Fact]
public async Task using_scenario_with_StatusCodeShouldBeSuccess_happy_path()
{
router.Handlers["/one"] = c =>
{
c.Response.StatusCode = 204;
c.Response.ContentType("text/plain");
c.Response.Write("Some text");
return Task.CompletedTask;
};

var ex = await Exception<ScenarioAssertionException>.ShouldBeThrownBy(() =>
{
return host.Scenario(x =>
{
x.Get.Url("/one");
x.StatusCodeShouldBeSuccess();
});
});

}

[Fact]
public async Task using_scenario_with_StatusCodeShouldBeSuccess_sad_path()
{
router.Handlers["/one"] = c =>
{
c.Response.StatusCode = 500;
c.Response.ContentType("text/plain");
c.Response.Write("Some text");
return Task.CompletedTask;
};

var ex = await Exception<ScenarioAssertionException>.ShouldBeThrownBy(() =>
{
return host.Scenario(x =>
{
x.Get.Url("/one");
x.StatusCodeShouldBeSuccess();
});
});

ex.Message.ShouldContain("Expected status code 200, but was 500");
}

}
}
69 changes: 69 additions & 0 deletions src/Alba.Testing/Assertions/StatusCodeSuccessAssertionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Alba.Assertions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Alba.Testing.Assertions
{
public class StatusCodeSuccessAssertionTests
{
[Theory]
[ClassData(typeof(SuccessStatusCodes))]
public void HappyPath(int statusCode)
{
var assertion = new StatusCodeSuccessAssertion();

AssertionRunner.Run(assertion, _ => _.StatusCode(statusCode))
.AssertAll();
}

[Theory]
[ClassData(typeof(FailureStatusCodes))]
public void SadPath(int statusCode)
{
var assertion = new StatusCodeSuccessAssertion();

AssertionRunner.Run(assertion, _ => _.StatusCode(statusCode))
.SingleMessageShouldBe($"Expected a status code between 200 and 299, but was {statusCode}");
}
}



}

public class SuccessStatusCodes : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
foreach (var code in Enumerable.Range(200, 99))
{
yield return new object[] { code };
}
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

}

public class FailureStatusCodes : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
foreach (var code in Enumerable.Range(100, 99))
{
yield return new object[] { code };
}
foreach (var code in Enumerable.Range(300, 200))
{
yield return new object[] { code };
}
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

}
14 changes: 14 additions & 0 deletions src/Alba/Assertions/StatusCodeSuccessAssertion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Alba.Assertions;

public sealed class StatusCodeSuccessAssertion : IScenarioAssertion
{
public void Assert(Scenario scenario, AssertionContext context)
{
var statusCode = context.HttpContext.Response.StatusCode;
if(statusCode < 200 || statusCode >= 300)
{
context.AddFailure($"Expected a status code between 200 and 299, but was {statusCode}");
context.ReadBodyAsString();
}
}
}
14 changes: 13 additions & 1 deletion src/Alba/ScenarioExpectationsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Net;
using Alba.Assertions;

namespace Alba;

public static class ScenarioExpectationsExtensions
{
#region sample_ContentShouldContain

/// <summary>
/// Assert that the Http response contains the designated text
/// </summary>
Expand All @@ -16,6 +17,7 @@ public static Scenario ContentShouldContain(this Scenario scenario, string text)
{
return scenario.AssertThat(new BodyContainsAssertion(text));
}

#endregion

/// <summary>
Expand Down Expand Up @@ -50,6 +52,16 @@ public static Scenario StatusCodeShouldBeOk(this Scenario scenario)
return scenario.StatusCodeShouldBe(HttpStatusCode.OK);
}

/// <summary>
/// Assert that the Http Status Code is between 200 and 299
/// </summary>
/// <param name="scenario"></param>
/// <returns></returns>
public static Scenario StatusCodeShouldBeSuccess(this Scenario scenario)
{
return scenario.AssertThat(new StatusCodeSuccessAssertion());
}

/// <summary>
/// Assert that the content-type header value of the Http response
/// matches the expected value
Expand Down

0 comments on commit c99ee6d

Please sign in to comment.