|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace System.Net.Tests |
| 11 | +{ |
| 12 | + public class HttpListenerResponseCookiesTests : HttpListenerResponseTestBase |
| 13 | + { |
| 14 | + [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))] |
| 15 | + public async Task Cookies_GetSet_ReturnsExpected() |
| 16 | + { |
| 17 | + HttpListenerResponse response = await GetResponse(); |
| 18 | + Assert.Same(response.Cookies, response.Cookies); |
| 19 | + Assert.Empty(response.Cookies); |
| 20 | + |
| 21 | + var cookies = new CookieCollection() { new Cookie("name", "value") }; |
| 22 | + response.Cookies = cookies; |
| 23 | + Assert.Equal(cookies, response.Cookies); |
| 24 | + |
| 25 | + response.Cookies = null; |
| 26 | + Assert.Empty(response.Cookies); |
| 27 | + } |
| 28 | + |
| 29 | + public static IEnumerable<object[]> Cookies_TestData() |
| 30 | + { |
| 31 | + yield return new object[] |
| 32 | + { |
| 33 | + new CookieCollection() |
| 34 | + { |
| 35 | + new Cookie() |
| 36 | + }, |
| 37 | + 120, null, null |
| 38 | + }; |
| 39 | + |
| 40 | + yield return new object[] |
| 41 | + { |
| 42 | + new CookieCollection() |
| 43 | + { |
| 44 | + new Cookie(), |
| 45 | + new Cookie("name", "value") |
| 46 | + }, |
| 47 | + 144, "Set-Cookie: name=value", null |
| 48 | + }; |
| 49 | + |
| 50 | + yield return new object[] |
| 51 | + { |
| 52 | + new CookieCollection() |
| 53 | + { |
| 54 | + new Cookie("name", "value") |
| 55 | + }, |
| 56 | + 144, "Set-Cookie: name=value", null |
| 57 | + }; |
| 58 | + |
| 59 | + yield return new object[] |
| 60 | + { |
| 61 | + new CookieCollection() |
| 62 | + { |
| 63 | + new Cookie("name1", "value1"), |
| 64 | + new Cookie("name2", "value2") |
| 65 | + }, |
| 66 | + 160, "Set-Cookie: name1=value1, name2=value2", null |
| 67 | + }; |
| 68 | + |
| 69 | + yield return new object[] |
| 70 | + { |
| 71 | + new CookieCollection() |
| 72 | + { |
| 73 | + new Cookie("name1", "value1") { Port = "\"200\"" }, |
| 74 | + new Cookie("name2", "value2") { Port = "\"300\"" }, |
| 75 | + }, |
| 76 | + 207, null, "Set-Cookie2: name1=value1; Port=\"200\"; Version=1, name2=value2; Port=\"300\"; Version=1" |
| 77 | + }; |
| 78 | + |
| 79 | + yield return new object[] |
| 80 | + { |
| 81 | + new CookieCollection() |
| 82 | + { |
| 83 | + new Cookie("name1", "value1"), |
| 84 | + new Cookie("name2", "value2") { Port = "\"300\"" }, |
| 85 | + }, |
| 86 | + 196, "Set-Cookie: name1=value1", "Set-Cookie2: name2=value2; Port=\"300\"; Version=1" |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + [ConditionalTheory(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))] |
| 91 | + [MemberData(nameof(Cookies_TestData))] |
| 92 | + [ActiveIssue(20162)] |
| 93 | + public async Task Cookies_SetAndSend_ClientReceivesExpectedHeaders(CookieCollection cookies, int expectedBytes, string expectedSetCookie, string expectedSetCookie2) |
| 94 | + { |
| 95 | + HttpListenerResponse response = await GetResponse(); |
| 96 | + response.Cookies = cookies; |
| 97 | + |
| 98 | + response.Close(); |
| 99 | + |
| 100 | + Assert.Equal(expectedSetCookie?.Replace("Set-Cookie: ", ""), response.Headers["Set-Cookie"]); |
| 101 | + Assert.Equal(expectedSetCookie2?.Replace("Set-Cookie2: ", ""), response.Headers["Set-Cookie2"]); |
| 102 | + |
| 103 | + string clientResponse = GetClientResponse(expectedBytes); |
| 104 | + if (expectedSetCookie != null) |
| 105 | + { |
| 106 | + Assert.Contains($"{Environment.NewLine}{expectedSetCookie}{Environment.NewLine}", clientResponse); |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + Assert.DoesNotContain("Set-Cookie:", clientResponse); |
| 111 | + } |
| 112 | + |
| 113 | + if (expectedSetCookie2 != null) |
| 114 | + { |
| 115 | + Assert.Contains($"{Environment.NewLine}{expectedSetCookie2}{Environment.NewLine}", clientResponse); |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + Assert.DoesNotContain("Set-Cookie2:", clientResponse); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))] |
| 124 | + public async Task Cookies_SetInHeader_ClientReceivesExpectedHeaders() |
| 125 | + { |
| 126 | + HttpListenerResponse response = await GetResponse(); |
| 127 | + response.Headers["Set-Cookie"] = "name1=value1"; |
| 128 | + response.Headers["Set-Cookie2"] = "name2=value2"; |
| 129 | + |
| 130 | + response.Close(); |
| 131 | + |
| 132 | + string clientResponse = GetClientResponse(173); |
| 133 | + Assert.Contains($"{Environment.NewLine}Set-Cookie: name1=value1{Environment.NewLine}", clientResponse); |
| 134 | + Assert.Contains($"{Environment.NewLine}Set-Cookie2: name2=value2{Environment.NewLine}", clientResponse); |
| 135 | + } |
| 136 | + |
| 137 | + [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))] |
| 138 | + public async Task Cookies_SetCookie2InHeadersButNotInCookies_RemovesFromHeaders() |
| 139 | + { |
| 140 | + HttpListenerResponse response = await GetResponse(); |
| 141 | + response.Headers["Set-Cookie"] = "name1=value2"; |
| 142 | + response.Headers["Set-Cookie2"] = "name2=value2"; |
| 143 | + |
| 144 | + response.Cookies.Add(new Cookie("name3", "value3") { Port = "\"200\"" }); |
| 145 | + |
| 146 | + response.Close(); |
| 147 | + |
| 148 | + Assert.Null(response.Headers["Set-Cookie"]); |
| 149 | + Assert.Equal("name3=value3; Port=\"200\"; Version=1", response.Headers["Set-Cookie2"]); |
| 150 | + |
| 151 | + string clientResponse = GetClientResponse(170); |
| 152 | + Assert.DoesNotContain("Set-Cookie:", clientResponse); |
| 153 | + Assert.Contains($"{Environment.NewLine}Set-Cookie2: name3=value3; Port=\"200\"; Version=1{Environment.NewLine}", clientResponse); |
| 154 | + } |
| 155 | + |
| 156 | + [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))] |
| 157 | + public async Task Cookies_SetCookieInHeadersButNotInCookies_RemovesFromHeaders() |
| 158 | + { |
| 159 | + HttpListenerResponse response = await GetResponse(); |
| 160 | + response.Headers["Set-Cookie"] = "name1=value2"; |
| 161 | + response.Headers["Set-Cookie2"] = "name2=value2"; |
| 162 | + |
| 163 | + response.Cookies.Add(new Cookie("name3", "value3")); |
| 164 | + |
| 165 | + response.Close(); |
| 166 | + |
| 167 | + Assert.Equal("name3=value3", response.Headers["Set-Cookie"]); |
| 168 | + Assert.Null(response.Headers["Set-Cookie2"]); |
| 169 | + |
| 170 | + string clientResponse = GetClientResponse(146); |
| 171 | + Assert.Contains($"{Environment.NewLine}Set-Cookie: name3=value3{Environment.NewLine}", clientResponse); |
| 172 | + Assert.DoesNotContain("Set-Cookie2", clientResponse); |
| 173 | + } |
| 174 | + |
| 175 | + [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))] |
| 176 | + public async Task AppendCookie_ValidCookie_AddsCookieToCollection() |
| 177 | + { |
| 178 | + HttpListenerResponse response = await GetResponse(); |
| 179 | + |
| 180 | + var cookie1 = new Cookie("name1", "value"); |
| 181 | + var cookie2 = new Cookie("name2", "value2"); |
| 182 | + |
| 183 | + response.AppendCookie(cookie1); |
| 184 | + response.AppendCookie(cookie2); |
| 185 | + Assert.Equal(new Cookie[] { cookie1, cookie2 }, response.Cookies.Cast<Cookie>()); |
| 186 | + |
| 187 | + var cookie3 = new Cookie("name1", "value2"); |
| 188 | + response.AppendCookie(cookie3); |
| 189 | + Assert.Equal(new Cookie[] { cookie3, cookie2 }, response.Cookies.Cast<Cookie>()); |
| 190 | + |
| 191 | + // Cookies are not cloned. |
| 192 | + cookie3.Value = "value3"; |
| 193 | + Assert.Equal("value3", response.Cookies[0].Value); |
| 194 | + } |
| 195 | + |
| 196 | + [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))] |
| 197 | + public async Task AppendCookie_NullCookie_ThrowsArgumentNullException() |
| 198 | + { |
| 199 | + HttpListenerResponse response = await GetResponse(); |
| 200 | + Assert.Throws<ArgumentNullException>("cookie", () => response.AppendCookie(null)); |
| 201 | + } |
| 202 | + |
| 203 | + [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))] |
| 204 | + public async Task SetCookie_ValidCookie_AddsCookieToCollection() |
| 205 | + { |
| 206 | + HttpListenerResponse response = await GetResponse(); |
| 207 | + |
| 208 | + var cookie1 = new Cookie("name1", "value1"); |
| 209 | + var cookie2 = new Cookie("name2", "value2"); |
| 210 | + |
| 211 | + response.SetCookie(cookie1); |
| 212 | + response.SetCookie(cookie2); |
| 213 | + Assert.Equal(new Cookie[] { cookie1, cookie2 }, response.Cookies.Cast<Cookie>()); |
| 214 | + } |
| 215 | + |
| 216 | + [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))] |
| 217 | + [ActiveIssue(20160)] |
| 218 | + public async Task SetCookie_ValidCookie_ClonesCookie() |
| 219 | + { |
| 220 | + HttpListenerResponse response = await GetResponse(); |
| 221 | + var cookie = new Cookie("name", "value"); |
| 222 | + response.SetCookie(cookie); |
| 223 | + |
| 224 | + // Cookies are cloned. |
| 225 | + cookie.Value = "value3"; |
| 226 | + Assert.Equal("value", response.Cookies[0].Value); |
| 227 | + } |
| 228 | + |
| 229 | + [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))] |
| 230 | + public async Task SetCookie_NullCookie_ThrowsArgumentNullException() |
| 231 | + { |
| 232 | + HttpListenerResponse response = await GetResponse(); |
| 233 | + Assert.Throws<ArgumentNullException>("cookie", () => response.SetCookie(null)); |
| 234 | + } |
| 235 | + |
| 236 | + [ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))] |
| 237 | + [ActiveIssue(20159)] |
| 238 | + public async Task SetCookie_CookieDoesntExist_ThrowsArgumentException() |
| 239 | + { |
| 240 | + HttpListenerResponse response = await GetResponse(); |
| 241 | + var cookie1 = new Cookie("name", "value"); |
| 242 | + |
| 243 | + response.SetCookie(cookie1); |
| 244 | + Assert.Throws<ArgumentException>("cookie", () => response.SetCookie(cookie1)); |
| 245 | + |
| 246 | + var cookie2 = new Cookie("name", "value2"); |
| 247 | + Assert.Throws<ArgumentException>("cookie", () => response.SetCookie(cookie2)); |
| 248 | + Assert.Equal(new Cookie[] { cookie2 }, response.Cookies.Cast<Cookie>()); |
| 249 | + } |
| 250 | + } |
| 251 | +} |
0 commit comments