Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
DataSets
Formatting
Headers
Internal
Mocks
Properties
FormattingUtilitiesTests.cs
HttpClientExtensionsTest.cs
HttpContentCollectionExtensionsTests.cs
HttpContentExtensionsTest.cs
HttpContentMessageExtensionsTests.cs
HttpContentMultipartExtensionsTests.cs
HttpMessageContentTests.cs
HttpRequestHeadersExtensionsTest.cs
HttpRequestMessageCommonExtensionsTest.cs
HttpResponseHeadersExtensionsTest.cs
MultipartFileStreamProviderTests.cs
MultipartFormDataStreamProviderTests.cs
MultipartMemoryStreamProviderTests.cs
ObjectContentOfTTests.cs
ObjectContentTests.cs
ParserData.cs
System.Net.Http.Formatting.Test.Unit.csproj
UriExtensionsTests.cs
UriQueryDataSet.cs
UriQueryUtilityTests.cs
packages.config
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
ikdasm
ikvm
linker
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
ikvm-native
libgc
llvm
m4
man
mcs
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

159 lines
5.4 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net.Http.Formatting;
using System.Net.Http.Internal;
using System.Text;
using Microsoft.TestCommon;
using Xunit;
using Xunit.Extensions;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Net.Http
{
public class UriQueryUtilityTests
{
[Fact]
public void TypeIsCorrect()
{
Assert.Type.HasProperties(typeof(UriQueryUtility), TypeAssert.TypeProperties.IsClass | TypeAssert.TypeProperties.IsStatic);
}
#region UrlEncode
[Fact]
public void UrlEncodeReturnsNull()
{
Assert.Null(UriQueryUtility.UrlEncode(null));
}
public void UrlEncodeToBytesThrowsOnInvalidArgs()
{
Assert.Null(UriQueryUtility.UrlEncodeToBytes(null, 0, 0));
Assert.ThrowsArgumentNull(() => UriQueryUtility.UrlEncodeToBytes(null, 0, 2), "bytes");
Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlEncodeToBytes(new byte[0], -1, 0), "offset", null);
Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlEncodeToBytes(new byte[0], 2, 0), "offset", null);
Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlEncodeToBytes(new byte[0], 0, -1), "count", null);
Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlEncodeToBytes(new byte[0], 0, 2), "count", null);
}
#endregion
#region UrlDecode
[Fact]
public void UrlDecodeReturnsNull()
{
Assert.Null(UriQueryUtility.UrlDecode(null));
}
[Fact]
public void UrlDecodeParsesEmptySegmentsCorrectly()
{
int iterations = 16;
List<string> segments = new List<string>();
for (int index = 1; index < iterations; index++)
{
segments.Add("&");
string query = string.Join("", segments);
NameValueCollection result = ParseQueryString(query);
Assert.NotNull(result);
// Because this is a NameValueCollection, the same name appears only once
Assert.Equal(1, result.Count);
// Values should be a comma separated list of empty strings
string[] values = result[""].Split(new char[] { ',' });
// We expect length+1 segment as the final '&' counts as a segment
Assert.Equal(index + 1, values.Length);
foreach (var value in values)
{
Assert.Equal("", value);
}
}
}
public static TheoryDataSet<string, string, string> UriQueryData
{
get
{
return UriQueryTestData.UriQueryData;
}
}
private static string CreateQuery(params string[] segments)
{
StringBuilder buffer = new StringBuilder();
bool first = true;
foreach (string segment in segments)
{
if (first)
{
first = false;
}
else
{
buffer.Append('&');
}
buffer.Append(segment);
}
return buffer.ToString();
}
[Theory]
[InlineData("N", "N", "")]
[InlineData("%26", "&", "")]
[PropertyData("UriQueryData")]
public void UrlDecodeParsesCorrectly(string segment, string resultName, string resultValue)
{
int iterations = 16;
List<string> segments = new List<string>();
for (int index = 1; index < iterations; index++)
{
segments.Add(segment);
string query = CreateQuery(segments.ToArray());
NameValueCollection result = ParseQueryString(query);
Assert.NotNull(result);
// Because this is a NameValueCollection, the same name appears only once
Assert.Equal(1, result.Count);
// Values should be a comma separated list of resultValue
string[] values = result[resultName].Split(new char[] { ',' });
Assert.Equal(index, values.Length);
foreach (var value in values)
{
Assert.Equal(resultValue, value);
}
}
}
public void UrlDecodeToBytesThrowsOnInvalidArgs()
{
Assert.Null(UriQueryUtility.UrlDecodeToBytes(null, 0, 0));
Assert.ThrowsArgumentNull(() => UriQueryUtility.UrlDecodeToBytes(null, 0, 2), "bytes");
Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlDecodeToBytes(new byte[0], -1, 0), "offset", null);
Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlDecodeToBytes(new byte[0], 2, 0), "offset", null);
Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlDecodeToBytes(new byte[0], 0, -1), "count", null);
Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlDecodeToBytes(new byte[0], 0, 2), "count", null);
}
#endregion
private static NameValueCollection ParseQueryString(string query)
{
return new FormDataCollection(query).ReadAsNameValueCollection();
}
}
}