1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
2 
3 using System.Collections.Specialized;
4 using Xunit;
5 using Xunit.Extensions;
6 using Assert = Microsoft.TestCommon.AssertEx;
7 using Microsoft.TestCommon;
8 
9 namespace System.Net.Http.Headers
10 {
11     class CookieStateTest
12     {
13         [Fact]
CookieState_CtorThrowsOnNullName()14         public void CookieState_CtorThrowsOnNullName()
15         {
16             Assert.ThrowsArgumentNull(() => new CookieState(null, "value"), "name");
17         }
18 
19         [Theory]
20         [InlineData("<acb>")]
21         [InlineData("{acb}")]
22         [InlineData("[acb]")]
CookieState_CtorThrowsOnInvalidName(string name)23         public void CookieState_CtorThrowsOnInvalidName(string name)
24         {
25             Assert.ThrowsArgument(() => new CookieState(name, "value"), "name");
26         }
27 
28         [Theory]
29         [InlineData("\"acb\"")]
30         [InlineData("a,b")]
31         [InlineData("a;b")]
32         [InlineData("a\\b")]
CookieState_CtorThrowsOnInvalidValue(string value)33         public void CookieState_CtorThrowsOnInvalidValue(string value)
34         {
35             Assert.ThrowsArgument(() => new CookieState("name", value), "value");
36         }
37 
38         [Fact]
CookieState_CtorThrowsOnNullNameValueCollection()39         public void CookieState_CtorThrowsOnNullNameValueCollection()
40         {
41             Assert.ThrowsArgumentNull(() => new CookieState("name", (NameValueCollection)null), "values");
42         }
43 
44         [Theory]
45         [InlineData("name", "")]
46         [InlineData("name", "value")]
CookieState_Ctor1InitializesCorrectly(string name, string value)47         public void CookieState_Ctor1InitializesCorrectly(string name, string value)
48         {
49             CookieState cookie = new CookieState(name, value);
50             Assert.Equal(name, cookie.Name);
51             Assert.Equal(value, cookie.Values.AllKeys[0]);
52             Assert.Equal(value, cookie.Value);
53         }
54 
55         [Fact]
CookieState_Ctor2InitializesCorrectly()56         public void CookieState_Ctor2InitializesCorrectly()
57         {
58             // Arrange
59             NameValueCollection nvc = new NameValueCollection();
60             nvc.Add("n1", "v1");
61 
62             // Act
63             CookieState cookie = new CookieState("name", nvc);
64 
65             // Assert
66             Assert.Equal("name", cookie.Name);
67             Assert.Equal(1, cookie.Values.Count);
68             Assert.Equal("n1", cookie.Values.AllKeys[0]);
69             Assert.Equal("v1", cookie.Values["n1"]);
70             Assert.Equal("n1=v1", cookie.Value);
71         }
72 
73         [Fact]
CookieState_Value()74         public void CookieState_Value()
75         {
76             CookieState cookie = new CookieState("name");
77             Assert.Equal(String.Empty, cookie.Value);
78 
79             cookie.Value = "value1";
80             Assert.Equal("value1", cookie.Value);
81 
82             cookie.Values.AllKeys[0] = "value2";
83             Assert.Equal("value2", cookie.Value);
84         }
85 
86         [Fact]
CookieState_Clone()87         public void CookieState_Clone()
88         {
89             // Arrange
90             NameValueCollection nvc = new NameValueCollection();
91             nvc.Add("n1", "v1");
92             CookieState expectedValue = new CookieState("name", nvc);
93 
94             // Act
95             CookieState actualValue = expectedValue.Clone() as CookieState;
96 
97             // Assert
98             Assert.Equal("name", actualValue.Name);
99             Assert.Equal(1, actualValue.Values.Count);
100             Assert.Equal("n1", actualValue.Values.AllKeys[0]);
101             Assert.Equal("v1", actualValue.Values["n1"]);
102         }
103 
104         [Fact]
CookieState_ToStringWithSingleValue()105         public void CookieState_ToStringWithSingleValue()
106         {
107             // Arrange
108             CookieState cookie = new CookieState("name", "value");
109 
110             // Act
111             string actualValue = cookie.ToString();
112 
113             // Assert
114             Assert.Equal("name=value", actualValue);
115         }
116 
117         [Fact]
CookieState_ToStringWithNameValueCollection()118         public void CookieState_ToStringWithNameValueCollection()
119         {
120             // Arrange
121             NameValueCollection nvc = new NameValueCollection();
122             nvc.Add("n1", "v1");
123             nvc.Add("n2", "v2");
124             nvc.Add("n3", "v3");
125             CookieState cookie = new CookieState("name", nvc);
126 
127             // Act
128             string actualValue = cookie.ToString();
129 
130             // Assert
131             Assert.Equal("name=n1=v1&n2=v2&n3=v3", actualValue);
132         }
133     }
134 }
135