#91 Provide a default constructor for DefaultHttpContext.

This commit is contained in:
Chris Ross 2014-07-07 10:48:07 -07:00
parent 9028c6a1a5
commit 5bf4883cd9
10 changed files with 73 additions and 124 deletions

View File

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// 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;
@ -6,10 +6,22 @@ using System.Collections.Generic;
using System.IO;
using Microsoft.AspNet.HttpFeature;
namespace Microsoft.AspNet.Builder.Extensions
namespace Microsoft.AspNet.PipelineCore
{
public class FakeHttpRequestFeature : IHttpRequestFeature
public class DeafultHttpRequestFeature : IHttpRequestFeature
{
public DeafultHttpRequestFeature()
{
Headers = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
Body = Stream.Null;
Protocol = string.Empty;
Scheme = string.Empty;
Method = string.Empty;
PathBase = string.Empty;
Path = string.Empty;
QueryString = string.Empty;
}
public string Protocol { get; set; }
public string Scheme { get; set; }
public string Method { get; set; }
@ -19,16 +31,4 @@ namespace Microsoft.AspNet.Builder.Extensions
public IDictionary<string, string[]> Headers { get; set; }
public Stream Body { get; set; }
}
public class FakeHttpResponseFeature : IHttpResponseFeature
{
public int StatusCode { get; set; }
public string ReasonPhrase { get; set; }
public IDictionary<string, string[]> Headers { get; set; }
public Stream Body { get; set; }
public void OnSendingHeaders(Action<object> callback, object state)
{
throw new NotImplementedException();
}
}
}

View File

@ -33,6 +33,13 @@ namespace Microsoft.AspNet.PipelineCore
private FeatureReference<IHttpWebSocketFeature> _webSockets;
private IFeatureCollection _features;
public DefaultHttpContext()
: this(new FeatureCollection())
{
SetFeature<IHttpRequestFeature>(new DeafultHttpRequestFeature());
SetFeature<IHttpResponseFeature>(new DefaultHttpResponseFeature());
}
public DefaultHttpContext(IFeatureCollection features)
{
_features = features;

View File

@ -0,0 +1,33 @@
// 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 System.Collections.Generic;
using System.IO;
using Microsoft.AspNet.HttpFeature;
namespace Microsoft.AspNet.PipelineCore
{
public class DefaultHttpResponseFeature : IHttpResponseFeature
{
public DefaultHttpResponseFeature()
{
StatusCode = 200;
Headers = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
Body = Stream.Null;
}
public int StatusCode { get; set; }
public string ReasonPhrase { get; set; }
public IDictionary<string, string[]> Headers { get; set; }
public Stream Body { get; set; }
public void OnSendingHeaders(Action<object> callback, object state)
{
throw new NotSupportedException();
}
}
}

View File

@ -27,6 +27,8 @@
<Compile Include="Collections\ReadableStringCollection.cs" />
<Compile Include="Collections\RequestCookiesCollection.cs" />
<Compile Include="Collections\ResponseCookies.cs" />
<Compile Include="DeafultHttpRequestFeature.cs" />
<Compile Include="DefaultHttpResponseFeature.cs" />
<Compile Include="FormFeature.cs" />
<Compile Include="ItemsFeature.cs" />
<Compile Include="QueryFeature.cs" />

View File

@ -187,9 +187,7 @@ namespace Microsoft.AspNet.Builder.Extensions
private HttpContext CreateRequest(string basePath, string requestPath)
{
HttpContext context = new DefaultHttpContext(new FeatureModel.FeatureCollection());
context.SetFeature<IHttpRequestFeature>(new FakeHttpRequestFeature());
context.SetFeature<IHttpResponseFeature>(new FakeHttpResponseFeature());
HttpContext context = new DefaultHttpContext();
context.Request.PathBase = new PathString(basePath);
context.Request.Path = new PathString(requestPath);
return context;

View File

@ -176,9 +176,7 @@ namespace Microsoft.AspNet.Builder.Extensions
private HttpContext CreateRequest()
{
HttpContext context = new DefaultHttpContext(new FeatureModel.FeatureCollection());
context.SetFeature<IHttpRequestFeature>(new FakeHttpRequestFeature());
context.SetFeature<IHttpResponseFeature>(new FakeHttpResponseFeature());
HttpContext context = new DefaultHttpContext();
return context;
}
}

View File

@ -21,10 +21,9 @@
<Content Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="Fakes.cs" />
<Compile Include="MapPathMiddlewareTests.cs" />
<Compile Include="MapPredicateMiddlewareTests.cs" />
<Compile Include="PathStringTests.cs" />
</ItemGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
</Project>

View File

@ -1,16 +1,11 @@
// 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 System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using Microsoft.AspNet.FeatureModel;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.HttpFeature;
using Microsoft.AspNet.HttpFeature.Security;
using Microsoft.AspNet.PipelineCore;
using Xunit;
@ -101,66 +96,8 @@ namespace Microsoft.AspNet.Owin
private HttpContext CreateContext()
{
var features = new FeatureCollection();
features.Add(typeof(IHttpRequestFeature), new MoqHttpRequestFeature());
features.Add(typeof(IHttpResponseFeature), new MoqHttpResponseFeature());
features.Add(typeof(IHttpRequestLifetimeFeature), new MoqHttpRequestLifetimeFeature());
return new DefaultHttpContext(features);
}
private class MoqHttpRequestFeature : IHttpRequestFeature
{
public MoqHttpRequestFeature()
{
Headers = new Dictionary<string, string[]>();
}
public string Method { get; set; }
public string Scheme { get; set; }
public string Protocol { get; set; }
public Stream Body { get; set; }
public string PathBase { get; set; }
public string Path { get; set; }
public string QueryString { get; set; }
public IDictionary<string, string[]> Headers { get; set; }
}
private class MoqHttpResponseFeature : IHttpResponseFeature
{
public MoqHttpResponseFeature()
{
Headers = new Dictionary<string, string[]>();
}
public Stream Body { get; set; }
public int StatusCode { get; set; }
public string ReasonPhrase { get; set; }
public IDictionary<string, string[]> Headers { get; set; }
public void OnSendingHeaders(Action<object> callback, object state)
{
throw new NotImplementedException();
}
}
private class MoqHttpRequestLifetimeFeature : IHttpRequestLifetimeFeature
{
public CancellationToken RequestAborted { get; private set; }
public void Abort()
{
throw new NotImplementedException();
}
var context = new DefaultHttpContext();
return context;
}
}
}

View File

@ -80,25 +80,8 @@ namespace Microsoft.AspNet.PipelineCore.Tests
private HttpContext CreateContext()
{
var context = new DefaultHttpContext(new FeatureCollection());
context.SetFeature<IHttpResponseFeature>(new FakeHttpResponse());
var context = new DefaultHttpContext();
return context;
}
private class FakeHttpResponse : IHttpResponseFeature
{
public int StatusCode { get; set; }
public string ReasonPhrase { get; set; }
public IDictionary<string, string[]> Headers { get; set; }
public Stream Body { get; set; }
public void OnSendingHeaders(Action<object> callback, object state)
{
throw new NotImplementedException();
}
}
}
}

View File

@ -5,9 +5,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.FeatureModel;
using Microsoft.AspNet.HttpFeature;
using Moq;
using Xunit;
namespace Microsoft.AspNet.PipelineCore.Tests
@ -57,9 +55,9 @@ namespace Microsoft.AspNet.PipelineCore.Tests
// Arrange
const string expected = "localhost:9001";
var headers = new Dictionary<string, string[]>(StringComparer.Ordinal)
var headers = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase)
{
{ "Host", new string[]{ expected } },
{ "Host", new string[] { expected } },
};
var request = CreateRequest(headers);
@ -77,7 +75,7 @@ namespace Microsoft.AspNet.PipelineCore.Tests
// Arrange
const string expected = "löcalhöst";
var headers = new Dictionary<string, string[]>(StringComparer.Ordinal)
var headers = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase)
{
{ "Host", new string[]{ "xn--lcalhst-90ae" } },
};
@ -97,7 +95,7 @@ namespace Microsoft.AspNet.PipelineCore.Tests
// Arrange
const string expected = "xn--lcalhst-90ae";
var headers = new Dictionary<string, string[]>(StringComparer.Ordinal);
var headers = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
var request = CreateRequest(headers);
@ -108,25 +106,19 @@ namespace Microsoft.AspNet.PipelineCore.Tests
Assert.Equal(expected, headers["Host"][0]);
}
private static DefaultHttpRequest CreateRequest(IDictionary<string, string[]> headers)
private static HttpRequest CreateRequest(IDictionary<string, string[]> headers)
{
var requestInfo = new Mock<IHttpRequestFeature>();
requestInfo.SetupGet(r => r.Headers).Returns(headers);
var features = new FeatureCollection();
features.Add(typeof(IHttpRequestFeature), requestInfo.Object);
var context = new DefaultHttpContext(features);
return new DefaultHttpRequest(context, features);
var context = new DefaultHttpContext();
context.GetFeature<IHttpRequestFeature>().Headers = headers;
return context.Request;
}
private static DefaultHttpRequest GetRequestWithContentLength(string contentLength = null)
private static HttpRequest GetRequestWithContentLength(string contentLength = null)
{
var headers = new Dictionary<string, string[]>(StringComparer.Ordinal);
var headers = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
if (contentLength != null)
{
headers.Add("Content-Length", new[] { contentLength });
}
return CreateRequest(headers);