Skip to content

Commit 36d0f7b

Browse files
committed
Cleanup
1 parent 7a8237e commit 36d0f7b

File tree

2 files changed

+105
-100
lines changed

2 files changed

+105
-100
lines changed

tests/Microsoft.Identity.Web.Test.Common/Mocks/MockHttpMessageHandler.cs

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Globalization;
76
using System.Net;
87
using System.Net.Http;
98
using System.Threading;
109
using System.Threading.Tasks;
11-
using System.Web;
12-
using Azure;
1310
using Xunit;
1411

1512
namespace Microsoft.Identity.Web.Test.Common.Mocks
@@ -94,101 +91,4 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
9491
return ResponseMessage;
9592
}
9693
}
97-
98-
public static class QueryStringParser
99-
{
100-
public static Dictionary<string, string> ParseKeyValueList(string input, char delimiter, bool urlDecode,
101-
bool lowercaseKeys)
102-
{
103-
var response = new Dictionary<string, string>();
104-
105-
var queryPairs = SplitWithQuotes(input, delimiter);
106-
107-
foreach (string queryPair in queryPairs)
108-
{
109-
var pair = SplitWithQuotes(queryPair, '=');
110-
111-
if (pair.Count == 2 && !string.IsNullOrWhiteSpace(pair[0]) && !string.IsNullOrWhiteSpace(pair[1]))
112-
{
113-
string key = pair[0];
114-
string value = pair[1];
115-
116-
// Url decoding is needed for parsing OAuth response, but not for parsing WWW-Authenticate header in 401 challenge
117-
if (urlDecode)
118-
{
119-
key = UrlDecode(key);
120-
value = UrlDecode(value);
121-
}
122-
123-
if (lowercaseKeys)
124-
{
125-
key = key.Trim().ToLowerInvariant();
126-
}
127-
128-
value = value.Trim().Trim('\"').Trim();
129-
130-
response[key] = value;
131-
}
132-
}
133-
134-
return response;
135-
}
136-
137-
public static Dictionary<string, string> ParseKeyValueList(string input, char delimiter, bool urlDecode)
138-
{
139-
return ParseKeyValueList(input, delimiter, urlDecode, true);
140-
}
141-
142-
private static string UrlDecode(string message)
143-
{
144-
if (string.IsNullOrEmpty(message))
145-
{
146-
return message;
147-
}
148-
149-
message = message.Replace("+", "%20");
150-
message = Uri.UnescapeDataString(message);
151-
152-
return message;
153-
}
154-
155-
internal static IReadOnlyList<string> SplitWithQuotes(string input, char delimiter)
156-
{
157-
if (string.IsNullOrWhiteSpace(input))
158-
{
159-
return Array.Empty<string>();
160-
}
161-
162-
var items = new List<string>();
163-
164-
int startIndex = 0;
165-
bool insideString = false;
166-
string item;
167-
for (int i = 0; i < input.Length; i++)
168-
{
169-
if (input[i] == delimiter && !insideString)
170-
{
171-
item = input.Substring(startIndex, i - startIndex);
172-
if (!string.IsNullOrWhiteSpace(item.Trim()))
173-
{
174-
items.Add(item);
175-
}
176-
177-
startIndex = i + 1;
178-
}
179-
else if (input[i] == '"')
180-
{
181-
insideString = !insideString;
182-
}
183-
}
184-
185-
item = input.Substring(startIndex);
186-
if (!string.IsNullOrWhiteSpace(item.Trim()))
187-
{
188-
items.Add(item);
189-
}
190-
191-
return items;
192-
}
193-
}
19494
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
7+
namespace Microsoft.Identity.Web.Test.Common.Mocks
8+
{
9+
public static class QueryStringParser
10+
{
11+
public static Dictionary<string, string> ParseKeyValueList(string input, char delimiter, bool urlDecode,
12+
bool lowercaseKeys)
13+
{
14+
var response = new Dictionary<string, string>();
15+
16+
var queryPairs = SplitWithQuotes(input, delimiter);
17+
18+
foreach (string queryPair in queryPairs)
19+
{
20+
var pair = SplitWithQuotes(queryPair, '=');
21+
22+
if (pair.Count == 2 && !string.IsNullOrWhiteSpace(pair[0]) && !string.IsNullOrWhiteSpace(pair[1]))
23+
{
24+
string key = pair[0];
25+
string value = pair[1];
26+
27+
// Url decoding is needed for parsing OAuth response, but not for parsing WWW-Authenticate header in 401 challenge
28+
if (urlDecode)
29+
{
30+
key = UrlDecode(key);
31+
value = UrlDecode(value);
32+
}
33+
34+
if (lowercaseKeys)
35+
{
36+
key = key.Trim().ToLowerInvariant();
37+
}
38+
39+
value = value.Trim().Trim('\"').Trim();
40+
41+
response[key] = value;
42+
}
43+
}
44+
45+
return response;
46+
}
47+
48+
public static Dictionary<string, string> ParseKeyValueList(string input, char delimiter, bool urlDecode)
49+
{
50+
return ParseKeyValueList(input, delimiter, urlDecode, true);
51+
}
52+
53+
private static string UrlDecode(string message)
54+
{
55+
if (string.IsNullOrEmpty(message))
56+
{
57+
return message;
58+
}
59+
60+
message = message.Replace("+", "%20");
61+
message = Uri.UnescapeDataString(message);
62+
63+
return message;
64+
}
65+
66+
internal static IReadOnlyList<string> SplitWithQuotes(string input, char delimiter)
67+
{
68+
if (string.IsNullOrWhiteSpace(input))
69+
{
70+
return Array.Empty<string>();
71+
}
72+
73+
var items = new List<string>();
74+
75+
int startIndex = 0;
76+
bool insideString = false;
77+
string item;
78+
for (int i = 0; i < input.Length; i++)
79+
{
80+
if (input[i] == delimiter && !insideString)
81+
{
82+
item = input.Substring(startIndex, i - startIndex);
83+
if (!string.IsNullOrWhiteSpace(item.Trim()))
84+
{
85+
items.Add(item);
86+
}
87+
88+
startIndex = i + 1;
89+
}
90+
else if (input[i] == '"')
91+
{
92+
insideString = !insideString;
93+
}
94+
}
95+
96+
item = input.Substring(startIndex);
97+
if (!string.IsNullOrWhiteSpace(item.Trim()))
98+
{
99+
items.Add(item);
100+
}
101+
102+
return items;
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)