forked from haoduotnt/aspnetwebstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpRequestHeadersExtensionsTest.cs
162 lines (147 loc) · 7.15 KB
/
HttpRequestHeadersExtensionsTest.cs
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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using Microsoft.TestCommon;
namespace System.Net.Http
{
public class HttpRequestHeadersExtensionsTest
{
public static TheoryDataSet<string[], string, string[]> CookieMatches
{
get
{
// IEnumerable<string> inputCookies, string matchName, IEnumerable<string> expectedOuput
return new TheoryDataSet<string[], string, string[]>
{
{
new string[] {},
"empty",
new string[] {}
},
{
new string[]
{
"RMID=2dab5fc9747d4f8edaf410ff",
"adxcs=-",
"adxcl=l*2ba62=4fc449bf:1",
"adxcs=si=0:1",
},
"nomatch",
new string[] {}
},
{
new string[]
{
"RMID=2dab5fc9747d4f8edaf410ff",
"adxcs=-",
"adxcl=l*2ba62=4fc449bf:1",
"ADXCS=si=0:1",
},
"adxcs",
new string[]
{
"adxcs=-",
"ADXCS=si=0%3a1"
}
},
{
new string[]
{
"MC1=GUID=e87574286c55d547b5a0b19fb27d57a4&HASH=2874&LV=20124&V=3&LU=1334766376863; MS0=7bbaad2a8316483c89bbd2ca4e96fcea; A=I&I=AxUFAAAAAACSCAAAHFNnP3xE7Uth5BCZZSiqZQ!!",
"MC0=1334766377159; MC1=GUID=e87574286c55d547b5a0b19fb27d57a4&HASH=2874&LV=20124&V=3&LU=1334766376863; MS0=7bbaad2a8316483c89bbd2ca4e96fcea; A=I&I=AxUFAAAAAACSCAAAHFNnP3xE7Uth5BCZZSiqZQ!!; MUID=20EC57A324256BF3039D54E520256B7D&TUID=1",
},
"A",
new string[]
{
"MC1=GUID=e87574286c55d547b5a0b19fb27d57a4&HASH=2874&LV=20124&V=3&LU=1334766376863; MS0=7bbaad2a8316483c89bbd2ca4e96fcea; A=I&I=AxUFAAAAAACSCAAAHFNnP3xE7Uth5BCZZSiqZQ!!",
"MC0=1334766377159; MC1=GUID=e87574286c55d547b5a0b19fb27d57a4&HASH=2874&LV=20124&V=3&LU=1334766376863; MS0=7bbaad2a8316483c89bbd2ca4e96fcea; A=I&I=AxUFAAAAAACSCAAAHFNnP3xE7Uth5BCZZSiqZQ!!; MUID=20EC57A324256BF3039D54E520256B7D&TUID=1",
}
},
{
new string[]
{
"MC1=GUID=e87574286c55d547b5a0b19fb27d57a4&HASH=2874&LV=20124&V=3&LU=1334766376863; MS0=7bbaad2a8316483c89bbd2ca4e96fcea; A=I&I=AxUFAAAAAACSCAAAHFNnP3xE7Uth5BCZZSiqZQ!!",
"MC0=1334766377159; MC1=GUID=e87574286c55d547b5a0b19fb27d57a4&HASH=2874&LV=20124&V=3&LU=1334766376863; MS0=7bbaad2a8316483c89bbd2ca4e96fcea; A=I&I=AxUFAAAAAACSCAAAHFNnP3xE7Uth5BCZZSiqZQ!!; MUID=20EC57A324256BF3039D54E520256B7D&TUID=1",
},
"MC0",
new string[]
{
"MC0=1334766377159; MC1=GUID=e87574286c55d547b5a0b19fb27d57a4&HASH=2874&LV=20124&V=3&LU=1334766376863; MS0=7bbaad2a8316483c89bbd2ca4e96fcea; A=I&I=AxUFAAAAAACSCAAAHFNnP3xE7Uth5BCZZSiqZQ!!; MUID=20EC57A324256BF3039D54E520256B7D&TUID=1",
}
},
{
new string[]
{
"MC1=GUID=e87574286c55d547b5a0b19fb27d57a4&HASH=2874&LV=20124&V=3&LU=1334766376863; MS0=7bbaad2a8316483c89bbd2ca4e96fcea; A=I&I=AxUFAAAAAACSCAAAHFNnP3xE7Uth5BCZZSiqZQ!!",
"MC0=1334766377159; MC1=GUID=e87574286c55d547b5a0b19fb27d57a4&HASH=2874&LV=20124&V=3&LU=1334766376863; MS0=7bbaad2a8316483c89bbd2ca4e96fcea; A=I&I=AxUFAAAAAACSCAAAHFNnP3xE7Uth5BCZZSiqZQ!!; MUID=20EC57A324256BF3039D54E520256B7D&TUID=1",
},
"MC",
new string[] { }
},
};
}
}
[Fact]
public void GetCookies_ThrowsOnNull()
{
Assert.ThrowsArgumentNull(() => HttpRequestHeadersExtensions.GetCookies(null), "headers");
}
[Fact]
public void GetCookies_GetsCookiesReturnsEmptyCollection()
{
// Arrange
HttpRequestHeaders headers = CreateHttpRequestHeaders();
// Act
IEnumerable<CookieHeaderValue> cookies = headers.GetCookies();
// Assert
Assert.Equal(0, cookies.Count());
}
[Theory]
[InlineData("name1=n1=v1&n2=v2&n3=v3; expires=Sun, 06 Nov 1994 08:49:37 GMT; max-age=86400; domain=domain1; path=path1; secure; httponly")]
public void GetCookies_GetsCookies(string expectedCookie)
{
// Arrange
HttpRequestHeaders headers = CreateHttpRequestHeaders();
headers.TryAddWithoutValidation("Cookie", expectedCookie);
// Act
IEnumerable<CookieHeaderValue> cookies = headers.GetCookies();
// Assert
Assert.Equal(1, cookies.Count());
string actualCookie = cookies.ElementAt(0).ToString();
Assert.Equal(expectedCookie, actualCookie);
}
[Fact]
public void GetCookiesByName_ThrowsOnNullHeaders()
{
Assert.ThrowsArgumentNull(() => HttpRequestHeadersExtensions.GetCookies(null, "empty"), "headers");
}
[Fact]
public void GetCookiesByName_ThrowsOnNullName()
{
HttpRequestHeaders headers = CreateHttpRequestHeaders();
Assert.ThrowsArgumentNull(() => HttpRequestHeadersExtensions.GetCookies(headers, null), "name");
}
[Theory]
[PropertyData("CookieMatches")]
public void GetCookiesByName_GetsCookies(IEnumerable<string> cookies, string name, IEnumerable<string> expectedCookies)
{
// Arrange
HttpRequestHeaders headers = CreateHttpRequestHeaders();
foreach (string cookie in cookies)
{
headers.TryAddWithoutValidation("Cookie", cookie);
}
// Act
IEnumerable<CookieHeaderValue> actualCookieHeaderValues = headers.GetCookies(name);
// Assert
IEnumerable<string> actualCookies = actualCookieHeaderValues.Select(c => c.ToString());
Assert.True(actualCookies.SequenceEqual(expectedCookies));
}
private static HttpRequestHeaders CreateHttpRequestHeaders()
{
HttpRequestMessage request = new HttpRequestMessage();
return request.Headers;
}
}
}