forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookieStateTest.cs
More file actions
205 lines (174 loc) · 6.41 KB
/
Copy pathCookieStateTest.cs
File metadata and controls
205 lines (174 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Specialized;
using Microsoft.TestCommon;
namespace System.Net.Http.Headers
{
public class CookieStateTest
{
public static TheoryDataSet<string> InvalidCookieNames
{
get
{
return new TheoryDataSet<string>
{
"<acb>",
"{acb}",
"[acb]",
"\"acb\"",
"a,b",
"a;b",
"a\\b",
};
}
}
public static TheoryDataSet<string, string> EncodedCookieStateStrings
{
get
{
return new TheoryDataSet<string, string>
{
{ "?", "%3f" },
{ "=", "%3d" },
{ "<acb>", "%3cacb%3e" },
{ "{acb}", "%7bacb%7d" },
{ "[acb]", "%5bacb%5d" },
{ "\"acb\"", "%22acb%22" },
{ "a,b", "a%2cb" },
{ "a;b", "a%3bb" },
{ "a\\b", "a%5cb" },
{ "[]{}\\|!@#$%^&*()_-+=", "%5b%5d%7b%7d%5c%7c!%40%23%24%25%5e%26*()_-%2b%3d" },
};
}
}
[Fact]
public void CookieState_CtorThrowsOnNullName()
{
Assert.ThrowsArgumentNull(() => new CookieState(null, "value"), "name");
}
[Theory]
[PropertyData("InvalidCookieNames")]
public void CookieState_CtorThrowsOnInvalidName(string name)
{
Assert.ThrowsArgument(() => new CookieState(name, "value"), "name");
}
[Fact]
public void CookieState_CtorThrowsOnNullNameValueCollection()
{
Assert.ThrowsArgumentNull(() => new CookieState("name", (NameValueCollection)null), "values");
}
[Theory]
[InlineData("name", "")]
[InlineData("name", "value")]
[InlineData("name", "+=\\[]{}!@#$%^&*()_")]
public void CookieState_Ctor1InitializesCorrectly(string name, string value)
{
CookieState cookie = new CookieState(name, value);
Assert.Equal(name, cookie.Name);
Assert.Equal(value, cookie.Values.AllKeys[0]);
Assert.Equal(value, cookie.Value);
}
[Fact]
public void CookieState_Ctor2InitializesCorrectly()
{
// Arrange
NameValueCollection nvc = new NameValueCollection();
nvc.Add("n1", "v1");
// Act
CookieState cookie = new CookieState("name", nvc);
// Assert
Assert.Equal("name", cookie.Name);
Assert.Single(cookie.Values);
Assert.Equal("n1", cookie.Values.AllKeys[0]);
Assert.Equal("v1", cookie.Values["n1"]);
Assert.Equal("n1", cookie.Value);
}
[Fact]
public void CookieState_Value()
{
CookieState cookie = new CookieState("name");
Assert.Equal(String.Empty, cookie.Value);
cookie.Value = "value1";
Assert.Equal("value1", cookie.Value);
cookie.Values.AllKeys[0] = "value2";
Assert.Equal("value2", cookie.Value);
}
[Fact]
public void CookieState_ItemTreatsNullNameAsEmpty()
{
// Arrange
CookieState state = new CookieState("name", "value");
// Act
state[null] = "v1";
// Assert
Assert.Equal("name=value&=v1", state.ToString());
}
[Theory]
[PropertyData("EncodedCookieStateStrings")]
public void CookieState_ItemEncodesName(string subname, string encodedSubname)
{
// Arrange
CookieState state = new CookieState("name", "value");
// Act
state[subname] = "v1";
// Assert
string value = String.Format("name=value&{0}=v1", encodedSubname);
Assert.Equal(value, state.ToString());
}
[Theory]
[PropertyData("EncodedCookieStateStrings")]
public void CookieState_ItemEncodesValue(string subvalue, string encodedSubvalue)
{
// Arrange
CookieState state = new CookieState("name", "value");
// Act
state["n1"] = subvalue;
// Assert
string value = String.Format("name=value&n1={0}", encodedSubvalue);
Assert.Equal(value, state.ToString());
}
[Fact]
public void CookieState_Clone()
{
// Arrange
NameValueCollection nvc = new NameValueCollection();
nvc.Add("n1", "v1");
CookieState expectedValue = new CookieState("name", nvc);
// Act
CookieState actualValue = expectedValue.Clone() as CookieState;
// Assert
Assert.Equal("name", actualValue.Name);
Assert.Single(actualValue.Values);
Assert.Equal("n1", actualValue.Values.AllKeys[0]);
Assert.Equal("v1", actualValue.Values["n1"]);
}
[Theory]
[PropertyData("EncodedCookieStateStrings")]
public void CookieState_ToStringWithSingleValue(string subValue, string encodedSubvalue)
{
// Arrange
CookieState cookie = new CookieState("name", subValue);
// Act
string actualValue = cookie.ToString();
// Assert
string expectedValue = String.Format("name={0}", encodedSubvalue);
Assert.Equal(expectedValue, actualValue);
}
[Theory]
[PropertyData("EncodedCookieStateStrings")]
public void CookieState_ToStringWithNameValueCollection(string subValue, string encodedSubvalue)
{
// Arrange
NameValueCollection nvc = new NameValueCollection();
nvc.Add("n1", subValue);
nvc.Add("n2", subValue);
nvc.Add("n3", subValue);
CookieState cookie = new CookieState("name", nvc);
// Act
string actualValue = cookie.ToString();
// Assert
string expectedValue = String.Format("name=n1={0}&n2={0}&n3={0}", encodedSubvalue);
Assert.Equal(expectedValue, actualValue);
}
}
}