Skip to content

Commit

Permalink
✨ add matcher to assert that the cookie on an http response or http r…
Browse files Browse the repository at this point in the history
…esponse message has expired
  • Loading branch information
fluffynuts committed May 17, 2024
1 parent 18e1a5b commit 2f2a38a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 9 deletions.
62 changes: 53 additions & 9 deletions src/NExpect.Matchers.AspNetCore.Tests/TestHttpResponseMatchers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,50 @@ public void ShouldTestCookieNameAndValue()
// Assert
}

[Test]
public void ShouldBeAbleToAssertCookieIsExpired()
{
// Arrange
var key = GetRandomString();
var value = GetRandomString();
var hasExpiredCookie = HttpResponseBuilder.Create()
.WithCookie(
key,
value,
new CookieOptions()
{
MaxAge = TimeSpan.FromSeconds(-1)
}
).Build();

var hasValidCookie = HttpResponseBuilder.Create()
.WithCookie(
key,
value,
new CookieOptions()
{
MaxAge = TimeSpan.FromSeconds(120)
}
).Build();

// Act
Assert.That(() =>
{
Expect(hasExpiredCookie)
.To.Have.Cookie(key)
.With.Value(value)
.Which.Is.Expired();
}, Throws.Nothing);
Assert.That(() =>
{
Expect(hasValidCookie)
.To.Have.Cookie(key)
.With.Value(value)
.Which.Is.Expired();
}, Throws.Exception.InstanceOf<UnmetExpectationException>());
// Assert
}

[Test]
public void ShouldTestCookieSecureHttpOnlyDomain()
{
Expand Down Expand Up @@ -213,15 +257,15 @@ public void ShouldBeAbleToAssertCookieExpiration()
Throws.Nothing
);
Assert.That(
() =>
{
Expect(res)
.To.Have.Cookie(sessionKey)
.With.Value(sessionValue)
.With.Expiration(expires);
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
);
() =>
{
Expect(res)
.To.Have.Cookie(sessionKey)
.With.Value(sessionValue)
.With.Expiration(expires);
},
Throws.Exception.InstanceOf<UnmetExpectationException>()
);
Assert.That(
() =>
{
Expand Down
50 changes: 50 additions & 0 deletions src/NExpect.Matchers.AspNetCore/HttpResponseMessageMatchers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,56 @@ Func<string> customMessageGenerator
);
}

/// <summary>
/// Assert that the cookie has expired
/// </summary>
/// <param name="iis"></param>
/// <returns></returns>
public static IMore<Cookie> Expired(
this IIs<Cookie> iis
)
{
return iis.Expired(NULL_STRING);
}

/// <summary>
/// Assert that the cookie has expired
/// </summary>
/// <param name="iis"></param>
/// <param name="customMessage"></param>
/// <returns></returns>
public static IMore<Cookie> Expired(
this IIs<Cookie> iis,
string customMessage
)
{
return iis.Expired(() => customMessage);
}

/// <summary>
/// Assert that the cookie has expired
/// </summary>
/// <param name="iis"></param>
/// <param name="customMessageGenerator"></param>
/// <returns></returns>
public static IMore<Cookie> Expired(
this IIs<Cookie> iis,
Func<string> customMessageGenerator
)
{
return iis.AddMatcher(
actual =>
{
var passed = actual.Expires <= DateTime.Now;
return new MatcherResult(
passed,
() => $"Expected cookie '{actual.Name}' {passed.AsNot()} to be expired",
customMessageGenerator
);
}
);
}

/// <summary>
/// Asserts that the cookie has the desired Path
/// </summary>
Expand Down

0 comments on commit 2f2a38a

Please sign in to comment.