forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnvalidatedRequestValuesTest.cs
More file actions
178 lines (149 loc) · 6.3 KB
/
Copy pathUnvalidatedRequestValuesTest.cs
File metadata and controls
178 lines (149 loc) · 6.3 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
// 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 System.Web;
using Microsoft.TestCommon;
using Moq;
namespace Microsoft.WebPages.Test.Helpers
{
public class UnvalidatedRequestValuesTest
{
[Fact]
public void Constructor_SetsPropertiesCorrectly()
{
// Arrange
NameValueCollection expectedForm = new NameValueCollection();
NameValueCollection expectedQueryString = new NameValueCollection();
Mock<System.Web.UnvalidatedRequestValuesBase> mockUnvalidatedRequestValue = new Mock<System.Web.UnvalidatedRequestValuesBase>();
mockUnvalidatedRequestValue.SetupGet(u => u.Form).Returns(expectedForm);
mockUnvalidatedRequestValue.SetupGet(u => u.QueryString).Returns(expectedQueryString);
Mock<HttpRequestBase> mockRequest = new Mock<HttpRequestBase>();
mockRequest.SetupGet(r => r.Unvalidated).Returns(mockUnvalidatedRequestValue.Object);
// Act
#pragma warning disable 0618 // Obsolete System.Web.Helpers.UnvalidatedRequestValues
System.Web.Helpers.UnvalidatedRequestValues unvalidatedValues = new System.Web.Helpers.UnvalidatedRequestValues(mockRequest.Object);
#pragma warning restore
// Assert
Assert.Same(expectedForm, unvalidatedValues.Form);
Assert.Same(expectedQueryString, unvalidatedValues.QueryString);
}
[Fact]
public void Indexer_LooksUpValuesInCorrectOrder()
{
// Order should be QueryString, Form, Cookies, ServerVariables
// Arrange
NameValueCollection queryString = new NameValueCollection()
{
{ "foo", "fooQueryString" }
};
NameValueCollection form = new NameValueCollection()
{
{ "foo", "fooInForm" },
{ "bar", "barInForm" },
};
HttpCookieCollection cookies = new HttpCookieCollection()
{
new HttpCookie("foo", "fooCookie"),
new HttpCookie("bar", "barCookie"),
new HttpCookie("baz", "bazCookie")
};
NameValueCollection serverVars = new NameValueCollection()
{
{ "foo", "fooServerVars" },
{ "bar", "barServerVars" },
{ "baz", "bazServerVars" },
{ "quux", "quuxServerVars" },
};
Mock<HttpRequestBase> mockRequest = new Mock<HttpRequestBase>();
mockRequest.SetupGet(r => r.ServerVariables).Returns(serverVars);
mockRequest.SetupGet(r => r.Form).Returns(form);
mockRequest.SetupGet(r => r.QueryString).Returns(queryString);
mockRequest.SetupGet(r => r.Cookies).Returns(cookies);
TestUnvalidatedRequestValues testUnvalidatedRequestValue = new TestUnvalidatedRequestValues(mockRequest.Object);
mockRequest.SetupGet(r => r.Unvalidated).Returns(testUnvalidatedRequestValue);
#pragma warning disable 0618 // Obsolete System.Web.Helpers.UnvalidatedRequestValues
System.Web.Helpers.UnvalidatedRequestValues unvalidatedValues = new System.Web.Helpers.UnvalidatedRequestValues(mockRequest.Object);
#pragma warning restore
// Act
string fooValue = unvalidatedValues["foo"];
string barValue = unvalidatedValues["bar"];
string bazValue = unvalidatedValues["baz"];
string quuxValue = unvalidatedValues["quux"];
string notFoundValue = unvalidatedValues["not-found"];
// Assert
Assert.Equal("fooQueryString", fooValue);
Assert.Equal("barInForm", barValue);
Assert.Equal("bazCookie", bazValue);
Assert.Equal("quuxServerVars", quuxValue);
Assert.Null(notFoundValue);
}
private sealed class TestUnvalidatedRequestValues : UnvalidatedRequestValuesBase
{
HttpRequestBase _request;
NameValueCollection _queryString;
NameValueCollection _form;
public TestUnvalidatedRequestValues(HttpRequestBase request)
{
_request = request;
}
public override HttpCookieCollection Cookies
{
get
{
// HttpCookieCollection copy constructor is not public, so just return it from the request.
return _request.Cookies;
}
}
public override NameValueCollection QueryString
{
get
{
if (_queryString == null)
{
_queryString = new NameValueCollection(_request.QueryString);
}
return _queryString;
}
}
public override NameValueCollection Form
{
get
{
if (_form == null)
{
_form = new NameValueCollection(_request.Form);
}
return _form;
}
}
public override string this[string key]
{
// this item getter follows the same logic as UnvalidatedRequestValues.get_Item
get
{
string queryStringValue = QueryString[key];
if (queryStringValue != null)
{
return queryStringValue;
}
string formValue = Form[key];
if (formValue != null)
{
return formValue;
}
HttpCookie cookie = Cookies[key];
if (cookie != null)
{
return cookie.Value;
}
string serverVarValue = _request.ServerVariables[key];
if (serverVarValue != null)
{
return serverVarValue;
}
return null;
}
}
}
}
}