public static SetCookieHeaderValue HasCookie(string name, HttpResponseMessage response, CookieComparison comparison) { var setCookieHeaderValue = new SetCookieHeaderValue(new StringSegment(name)); var foundCookie = HasCookieCore(setCookieHeaderValue, response, new SetCookieComparer(comparison)); if (foundCookie != null) { return(foundCookie); } var suffix = comparison.HasFlag(CookieComparison.NameStartsWith) ? "starting with" : ""; Assert.True(false, $"Couldn't find a cookie with a name {suffix} '{name}'"); return(null); }
public bool Equals(SetCookieHeaderValue expected, SetCookieHeaderValue candidate) { var matchesAllCriteria = true; if (_comparisonCriteria.HasFlag(CookieComparison.NameStartsWith)) { matchesAllCriteria = matchesAllCriteria && candidate.Name.StartsWith(expected.Name.ToString(), StringComparison.Ordinal); } if (_comparisonCriteria.HasFlag(CookieComparison.NameEquals)) { matchesAllCriteria = matchesAllCriteria && candidate.Name.Equals(expected.Name, StringComparison.Ordinal); } if (_comparisonCriteria.HasFlag(CookieComparison.PathEquals)) { matchesAllCriteria = matchesAllCriteria && candidate.Path.Equals(expected.Path, StringComparison.Ordinal); } if (_comparisonCriteria.HasFlag(CookieComparison.DomainEquals)) { matchesAllCriteria = matchesAllCriteria && candidate.Domain.Equals(expected.Domain, StringComparison.Ordinal); } if (_comparisonCriteria.HasFlag(CookieComparison.ExpiresEquals)) { matchesAllCriteria = matchesAllCriteria && ((expected.Expires.HasValue && candidate.Expires.HasValue && expected.Expires - _skewAllowance <= candidate.Expires && candidate.Expires <= expected.Expires + _skewAllowance) || expected.Expires == candidate.Expires); } if (_comparisonCriteria.HasFlag(CookieComparison.MaxAgeEquals)) { matchesAllCriteria = matchesAllCriteria && ((expected.MaxAge.HasValue && candidate.MaxAge.HasValue && expected.MaxAge - _skewAllowance <= candidate.MaxAge && candidate.MaxAge <= expected.MaxAge + _skewAllowance) || expected.MaxAge == candidate.MaxAge); } if (_comparisonCriteria.HasFlag(CookieComparison.HttpOnly)) { matchesAllCriteria = matchesAllCriteria && expected.HttpOnly == candidate.HttpOnly; } if (_comparisonCriteria.HasFlag(CookieComparison.SameSite)) { matchesAllCriteria = matchesAllCriteria && expected.SameSite == candidate.SameSite; } if (_comparisonCriteria.HasFlag(CookieComparison.Secure)) { matchesAllCriteria = matchesAllCriteria && expected.Secure == candidate.Secure; } if (_comparisonCriteria.HasFlag(CookieComparison.ValueEquals)) { matchesAllCriteria = matchesAllCriteria && expected.Value.Equals(candidate.Value, StringComparison.Ordinal); } if (_comparisonCriteria.HasFlag(CookieComparison.ValueStartsWith)) { matchesAllCriteria = matchesAllCriteria && candidate.Value.StartsWith(expected.Value.ToString(), StringComparison.Ordinal); } return(matchesAllCriteria); }