Files
acceptance-tests
data
docs
eglib
external
Lucene.Net.Light
Newtonsoft.Json
android-libunwind
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
Parsers
FormUrlEncodedParserTests.cs
HttpRequestHeaderParserTests.cs
HttpRequestLineParserTests.cs
HttpResponseHeaderParserTests.cs
HttpStatusLineParserTests.cs
InternetMessageFormatHeaderParserTests.cs
MimeMultipartParserTests.cs
BufferedMediaTypeFormatterTests.cs
ContentNegotiationResultTest.cs
DataContractJsonMediaTypeFormatterTests.cs
DefaultContentNegotiatorTests.cs
FormDataCollectionTests.cs
FormUrlEncodedJsonTests.cs
FormUrlEncodedMediaTypeFormatterTests.cs
JsonMediaTypeFormatterTests.cs
MediaRangeMappingTests.cs
MediaTypeConstantsTests.cs
MediaTypeFormatterCollectionTests.cs
MediaTypeFormatterExtensionsTests.cs
MediaTypeFormatterTestBase.cs
MediaTypeFormatterTests.cs
MediaTypeHeaderValueExtensionsTests.cs
MediaTypeWithQualityHeaderValueComparerTests.cs
ParsedMediaTypeHeaderValueTests.cs
QueryStringMappingTests.cs
RequestHeaderMappingTests.cs
SerializerConsistencyTests.cs
StringWithQualityHeaderValueComparerTests.cs
XmlMediaTypeFormatterTests.cs
XmlSerializerMediaTypeFormatterTests.cs
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
buildtools
cecil
debian-snapshot
ikdasm
ikvm
nuget-buildtasks
nunit-lite
rx
ikvm-native
libgc
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

276 lines
11 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Net.Http.Formatting.DataSets;
using System.Text;
using Microsoft.TestCommon;
using Xunit;
using Xunit.Extensions;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Net.Http.Formatting.Parsers
{
public class HttpRequestLineParserTests
{
[Fact]
[Trait("Description", "HttpRequestLineParser is internal class")]
public void TypeIsCorrect()
{
Assert.Type.HasProperties<HttpRequestLineParser>(TypeAssert.TypeProperties.IsClass);
}
internal static byte[] CreateBuffer(string method, string address, string version)
{
return CreateBuffer(method, address, version, false);
}
private static byte[] CreateBuffer(string method, string address, string version, bool withLws)
{
const string SP = " ";
const string HTAB = "\t";
const string CRLF = "\r\n";
string lws = SP;
if (withLws)
{
lws = SP + SP + HTAB + SP;
}
string requestLine = String.Format("{0}{1}{2}{3}{4}{5}", method, lws, address, lws, version, CRLF);
return Encoding.UTF8.GetBytes(requestLine);
}
private static ParserState ParseBufferInSteps(HttpRequestLineParser parser, byte[] buffer, int readsize, out int totalBytesConsumed)
{
ParserState state = ParserState.Invalid;
totalBytesConsumed = 0;
while (totalBytesConsumed <= buffer.Length)
{
int size = Math.Min(buffer.Length - totalBytesConsumed, readsize);
byte[] parseBuffer = new byte[size];
Buffer.BlockCopy(buffer, totalBytesConsumed, parseBuffer, 0, size);
int bytesConsumed = 0;
state = parser.ParseBuffer(parseBuffer, parseBuffer.Length, ref bytesConsumed);
totalBytesConsumed += bytesConsumed;
if (state != ParserState.NeedMoreData)
{
return state;
}
}
return state;
}
private static void ValidateResult(HttpUnsortedRequest requestLine, string method, string requestUri, Version version)
{
Assert.Equal(new HttpMethod(method), requestLine.Method);
Assert.Equal(requestUri, requestLine.RequestUri);
Assert.Equal(version, requestLine.Version);
}
[Fact]
[Trait("Description", "HttpRequestLineParser constructor throws on invalid arguments")]
public void HttpRequestLineParserConstructorTest()
{
HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
Assert.NotNull(requestLine);
Assert.ThrowsArgumentGreaterThanOrEqualTo(() => new HttpRequestLineParser(requestLine, ParserData.MinRequestLineSize - 1),
"maxRequestLineSize", ParserData.MinRequestLineSize.ToString(), ParserData.MinRequestLineSize - 1);
HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, ParserData.MinRequestLineSize);
Assert.NotNull(parser);
Assert.ThrowsArgumentNull(() => { new HttpRequestLineParser(null, ParserData.MinRequestLineSize); }, "httpRequest");
}
[Fact]
[Trait("Description", "HttpRequestLineParser.ParseBuffer throws on null buffer.")]
public void RequestLineParserNullBuffer()
{
HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, ParserData.MinRequestLineSize);
Assert.NotNull(parser);
int bytesConsumed = 0;
Assert.ThrowsArgumentNull(() => { parser.ParseBuffer(null, 0, ref bytesConsumed); }, "buffer");
}
[Fact]
[Trait("Description", "HttpRequestLineParser.ParseBuffer parses minimum requestline.")]
public void RequestLineParserMinimumBuffer()
{
byte[] data = CreateBuffer("G", "/", "HTTP/1.1");
HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, ParserData.MinRequestLineSize);
Assert.NotNull(parser);
int bytesConsumed = 0;
ParserState state = parser.ParseBuffer(data, data.Length, ref bytesConsumed);
Assert.Equal(ParserState.Done, state);
Assert.Equal(data.Length, bytesConsumed);
ValidateResult(requestLine, "G", "/", new Version("1.1"));
}
[Fact]
[Trait("Description", "HttpRequestLineParser.ParseBuffer rejects LWS requestline.")]
public void RequestLineParserRejectsLws()
{
byte[] data = CreateBuffer("GET", "/", "HTTP/1.1", true);
for (var cnt = 1; cnt <= data.Length; cnt++)
{
HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, data.Length);
Assert.NotNull(parser);
int totalBytesConsumed = 0;
ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed);
Assert.Equal(ParserState.Invalid, state);
}
}
[Fact]
[Trait("Description", "HttpRequestLineParser.ParseBuffer parses standard methods.")]
public void RequestLineParserAcceptsStandardMethods()
{
foreach (HttpMethod method in HttpUnitTestDataSets.AllHttpMethods)
{
byte[] data = CreateBuffer(method.ToString(), "/", "HTTP/1.1");
for (var cnt = 1; cnt <= data.Length; cnt++)
{
HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, data.Length);
Assert.NotNull(parser);
int totalBytesConsumed = 0;
ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed);
Assert.Equal(ParserState.Done, state);
Assert.Equal(data.Length, totalBytesConsumed);
ValidateResult(requestLine, method.ToString(), "/", new Version("1.1"));
}
}
}
[Fact]
[Trait("Description", "HttpRequestLineParser.ParseBuffer parses custom methods.")]
public void RequestLineParserAcceptsCustomMethods()
{
foreach (HttpMethod method in HttpUnitTestDataSets.CustomHttpMethods)
{
byte[] data = CreateBuffer(method.ToString(), "/", "HTTP/1.1");
for (var cnt = 1; cnt <= data.Length; cnt++)
{
HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, data.Length);
Assert.NotNull(parser);
int totalBytesConsumed = 0;
ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed);
Assert.Equal(ParserState.Done, state);
Assert.Equal(data.Length, totalBytesConsumed);
ValidateResult(requestLine, method.ToString(), "/", new Version("1.1"));
}
}
}
[Fact]
[Trait("Description", "HttpRequestLineParser.ParseBuffer rejects invalid method")]
public void RequestLineParserRejectsInvalidMethod()
{
foreach (string invalidMethod in ParserData.InvalidMethods)
{
byte[] data = CreateBuffer(invalidMethod, "/", "HTTP/1.1");
for (var cnt = 1; cnt <= data.Length; cnt++)
{
HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, 256);
Assert.NotNull(parser);
int totalBytesConsumed = 0;
ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed);
Assert.Equal(ParserState.Invalid, state);
}
}
}
[Fact]
[Trait("Description", "HttpRequestLineParser.ParseBuffer rejects invalid URI.")]
public void RequestLineParserRejectsInvalidUri()
{
foreach (string invalidRequestUri in ParserData.InvalidRequestUris)
{
byte[] data = CreateBuffer("GET", invalidRequestUri, "HTTP/1.1");
for (var cnt = 1; cnt <= data.Length; cnt++)
{
HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, 256);
Assert.NotNull(parser);
int totalBytesConsumed = 0;
ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed);
Assert.Equal(ParserState.Invalid, state);
}
}
}
public static IEnumerable<object[]> Versions
{
get { return ParserData.Versions; }
}
[Theory]
[PropertyData("Versions")]
[Trait("Description", "HttpRequestLineParser.ParseBuffer accepts valid versions.")]
public void RequestLineParserAcceptsValidVersion(Version version)
{
byte[] data = CreateBuffer("GET", "/", String.Format("HTTP/{0}", version.ToString(2)));
for (var cnt = 1; cnt <= data.Length; cnt++)
{
HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, 256);
Assert.NotNull(parser);
int totalBytesConsumed = 0;
ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed);
Assert.Equal(data.Length, totalBytesConsumed);
ValidateResult(requestLine, "GET", "/", version);
}
}
public static IEnumerable<object[]> InvalidVersions
{
get { return ParserData.InvalidVersions; }
}
[Theory]
[PropertyData("InvalidVersions")]
[Trait("Description", "HttpRequestLineParser.ParseBuffer rejects invalid protocol version.")]
public void RequestLineParserRejectsInvalidVersion(string invalidVersion)
{
byte[] data = CreateBuffer("GET", "/", invalidVersion);
for (var cnt = 1; cnt <= data.Length; cnt++)
{
HttpUnsortedRequest requestLine = new HttpUnsortedRequest();
HttpRequestLineParser parser = new HttpRequestLineParser(requestLine, 256);
Assert.NotNull(parser);
int totalBytesConsumed = 0;
ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed);
Assert.Equal(ParserState.Invalid, state);
}
}
}
}