-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created an Assertion for StatusCodeShouldBeSuccess
- Loading branch information
1 parent
f360e65
commit 27b90d6
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Alba.Testing/Assertions/StatusCodeSuccessAssertionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters