Skip to content

Commit

Permalink
Created an Assertion for StatusCodeShouldBeSuccess
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffryGonzalez committed May 27, 2024
1 parent f360e65 commit 27b90d6
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
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();

}
22 changes: 22 additions & 0 deletions src/Alba/Assertions/StatusCodeSuccessAssertion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Alba.Assertions
{
public class StatusCodeSuccessAssertion : IScenarioAssertion
{
public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionException ex)
{
var statusCode = context.Response.StatusCode;
if(statusCode < 200 || statusCode >= 300)
{
ex.Add($"Expected a status code between 200 and 299, but was {statusCode}");
ex.ReadBody(context);
}
}
}
}
10 changes: 10 additions & 0 deletions src/Alba/ScenarioExpectationsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public static Scenario StatusCodeShouldBeOk(this Scenario scenario)
return scenario.StatusCodeShouldBe(HttpStatusCode.OK);
}

/// <summary>
/// Assert tha 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 27b90d6

Please sign in to comment.