|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Linq; |
5 | 6 | using Microsoft.AspNetCore.Http.Features; |
6 | 7 | using Microsoft.Extensions.DependencyInjection; |
7 | 8 | using Microsoft.Extensions.Logging; |
@@ -74,6 +75,64 @@ public void DeleteCookieShouldSetDefaultPath() |
74 | 75 | Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookieHeaderValues[0]); |
75 | 76 | } |
76 | 77 |
|
| 78 | + [Fact] |
| 79 | + public void DeleteCookieWithDomainAndPathDeletesPriorMatchingCookies() |
| 80 | + { |
| 81 | + var headers = (IHeaderDictionary)new HeaderDictionary(); |
| 82 | + var features = MakeFeatures(headers); |
| 83 | + var responseCookies = new ResponseCookies(features); |
| 84 | + |
| 85 | + var testCookies = new (string Key, string Path, string Domain)[] |
| 86 | + { |
| 87 | + new ("key1", "/path1/", null), |
| 88 | + new ("key1", "/path2/", null), |
| 89 | + new ("key2", "/path1/", "localhost"), |
| 90 | + new ("key2", "/path2/", "localhost"), |
| 91 | + }; |
| 92 | + |
| 93 | + foreach (var cookie in testCookies) |
| 94 | + { |
| 95 | + responseCookies.Delete(cookie.Key, new CookieOptions() { Domain = cookie.Domain, Path = cookie.Path }); |
| 96 | + } |
| 97 | + |
| 98 | + var deletedCookies = headers.SetCookie.ToArray(); |
| 99 | + Assert.Equal(testCookies.Length, deletedCookies.Length); |
| 100 | + |
| 101 | + Assert.Single(deletedCookies, cookie => cookie.StartsWith("key1", StringComparison.InvariantCulture) && cookie.Contains("path=/path1/")); |
| 102 | + Assert.Single(deletedCookies, cookie => cookie.StartsWith("key1", StringComparison.InvariantCulture) && cookie.Contains("path=/path2/")); |
| 103 | + Assert.Single(deletedCookies, cookie => cookie.StartsWith("key2", StringComparison.InvariantCulture) && cookie.Contains("path=/path1/") && cookie.Contains("domain=localhost")); |
| 104 | + Assert.Single(deletedCookies, cookie => cookie.StartsWith("key2", StringComparison.InvariantCulture) && cookie.Contains("path=/path2/") && cookie.Contains("domain=localhost")); |
| 105 | + Assert.All(deletedCookies, cookie => Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookie)); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void DeleteRemovesCookieWithDomainAndPathCreatedByAdd() |
| 110 | + { |
| 111 | + var headers = (IHeaderDictionary)new HeaderDictionary(); |
| 112 | + var features = MakeFeatures(headers); |
| 113 | + var responseCookies = new ResponseCookies(features); |
| 114 | + |
| 115 | + var testCookies = new (string Key, string Path, string Domain)[] |
| 116 | + { |
| 117 | + new ("key1", "/path1/", null), |
| 118 | + new ("key1", "/path1/", null), |
| 119 | + new ("key2", "/path1/", "localhost"), |
| 120 | + new ("key2", "/path1/", "localhost"), |
| 121 | + }; |
| 122 | + |
| 123 | + foreach (var cookie in testCookies) |
| 124 | + { |
| 125 | + responseCookies.Append(cookie.Key, cookie.Key, new CookieOptions() { Domain = cookie.Domain, Path = cookie.Path }); |
| 126 | + responseCookies.Delete(cookie.Key, new CookieOptions() { Domain = cookie.Domain, Path = cookie.Path }); |
| 127 | + } |
| 128 | + |
| 129 | + var deletedCookies = headers.SetCookie.ToArray(); |
| 130 | + Assert.Equal(2, deletedCookies.Length); |
| 131 | + Assert.Single(deletedCookies, cookie => cookie.StartsWith("key1", StringComparison.InvariantCulture) && cookie.Contains("path=/path1/")); |
| 132 | + Assert.Single(deletedCookies, cookie => cookie.StartsWith("key2", StringComparison.InvariantCulture) && cookie.Contains("path=/path1/") && cookie.Contains("domain=localhost")); |
| 133 | + Assert.All(deletedCookies, cookie => Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookie)); |
| 134 | + } |
| 135 | + |
77 | 136 | [Fact] |
78 | 137 | public void DeleteCookieWithCookieOptionsShouldKeepPropertiesOfCookieOptions() |
79 | 138 | { |
|
0 commit comments