#121 - Make the query parsing API public.

This commit is contained in:
Chris Ross 2014-09-16 10:30:45 -07:00
parent d61b683549
commit c2934912af
5 changed files with 94 additions and 17 deletions

View File

@ -795,22 +795,6 @@ namespace Microsoft.AspNet.PipelineCore.Infrastructure
}
};
private static readonly char[] AmpersandAndSemicolon = new[] { '&', ';' };
internal static IDictionary<string, string[]> GetQuery(string queryString)
{
if (!string.IsNullOrEmpty(queryString) && queryString[0] == '?')
{
queryString = queryString.Substring(1);
}
var accumulator = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
ParseDelimited(queryString, AmpersandAndSemicolon, AppendItemCallback, accumulator);
return accumulator.ToDictionary(
item => item.Key,
item => item.Value.ToArray(),
StringComparer.OrdinalIgnoreCase);
}
internal static string GetJoinedValue(IDictionary<string, string[]> store, string key)
{
string[] values = GetUnmodifiedValues(store, key);

View File

@ -6,6 +6,7 @@ using Microsoft.AspNet.FeatureModel;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.HttpFeature;
using Microsoft.AspNet.PipelineCore.Infrastructure;
using Microsoft.AspNet.WebUtilities;
using Microsoft.AspNet.WebUtilities.Collections;
namespace Microsoft.AspNet.PipelineCore
@ -45,7 +46,7 @@ namespace Microsoft.AspNet.PipelineCore
if (_query == null || _queryString != queryString)
{
_queryString = queryString;
_query = new ReadableStringCollection(ParsingHelpers.GetQuery(queryString));
_query = QueryHelpers.ParseQuery(queryString);
}
return _query;
}

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.WebUtilities.Collections;
@ -90,5 +91,21 @@ namespace Microsoft.AspNet.WebUtilities
string[] values;
return store.TryGetValue(key, out values) ? values : null;
}
private static readonly char[] AmpersandAndSemicolon = new[] { '&', ';' };
internal static IReadableStringCollection GetQuery(string queryString)
{
if (!string.IsNullOrEmpty(queryString) && queryString[0] == '?')
{
queryString = queryString.Substring(1);
}
var accumulator = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
ParseDelimited(queryString, AmpersandAndSemicolon, AppendItemCallback, accumulator);
return new ReadableStringCollection(accumulator.ToDictionary(
item => item.Key,
item => item.Value.ToArray(),
StringComparer.OrdinalIgnoreCase));
}
}
}

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.WebUtilities
{
@ -43,5 +44,15 @@ namespace Microsoft.AspNet.WebUtilities
}
return sb.ToString();
}
/// <summary>
/// Parse a query string into its component key and value parts.
/// </summary>
/// <param name="text">The raw query string value, with or without the leading '?'.</param>
/// <returns>A collection of parsed keys and values.</returns>
public static IReadableStringCollection ParseQuery(string text)
{
return ParsingHelpers.GetQuery(text);
}
}
}

View File

@ -0,0 +1,64 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Xunit;
namespace Microsoft.AspNet.WebUtilities
{
public class QueryHelperTests
{
[Fact]
public void ParseQueryWithUniqueKeysWorks()
{
var collection = QueryHelpers.ParseQuery("?key1=value1&key2=value2");
Assert.Equal(2, collection.Count);
Assert.Equal("value1", collection["key1"]);
Assert.Equal("value2", collection["key2"]);
}
[Fact]
public void ParseQueryWithoutQuestionmarkWorks()
{
var collection = QueryHelpers.ParseQuery("key1=value1&key2=value2");
Assert.Equal(2, collection.Count);
Assert.Equal("value1", collection["key1"]);
Assert.Equal("value2", collection["key2"]);
}
[Fact]
public void ParseQueryWithDuplicateKeysGroups()
{
var collection = QueryHelpers.ParseQuery("?key1=valueA&key2=valueB&key1=valueC");
Assert.Equal(2, collection.Count);
Assert.Equal("valueA,valueC", collection["key1"]);
Assert.Equal("valueB", collection["key2"]);
}
[Fact]
public void ParseQueryWithSemicolonWorks()
{
var collection = QueryHelpers.ParseQuery("?key1=value1;key2=value2");
Assert.Equal(2, collection.Count);
Assert.Equal("value1", collection["key1"]);
Assert.Equal("value2", collection["key2"]);
}
[Fact]
public void ParseQueryWithEmptyValuesWorks()
{
var collection = QueryHelpers.ParseQuery("?key1=;key2=");
Assert.Equal(2, collection.Count);
Assert.Equal(string.Empty, collection["key1"]);
Assert.Equal(string.Empty, collection["key2"]);
}
[Fact]
public void ParseQueryWithEmptyKeyWorks()
{
var collection = QueryHelpers.ParseQuery("?=value1;=");
Assert.Equal(1, collection.Count);
Assert.Equal("value1,", collection[""]);
}
}
}