Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Provide a default constructor for DefaultHttpContext. #98

Merged
merged 1 commit into from
Jul 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
// 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;
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; }
Expand All @@ -19,16 +31,4 @@ public class FakeHttpRequestFeature : IHttpRequestFeature
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();
}
}
}
7 changes: 7 additions & 0 deletions src/Microsoft.AspNet.PipelineCore/DefaultHttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public class DefaultHttpContext : HttpContext
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;
Expand Down
33 changes: 33 additions & 0 deletions src/Microsoft.AspNet.PipelineCore/DefaultHttpResponseFeature.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
4 changes: 1 addition & 3 deletions test/Microsoft.AspNet.Http.Tests/MapPathMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ public void ChainedRoutes_Success()

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ public void ChainedPredicatesAsync_Success()

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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
67 changes: 2 additions & 65 deletions test/Microsoft.AspNet.Owin.Tests/OwinEnvironmentTests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -101,66 +96,8 @@ public void OwinEnvironmentCanBeModified()

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,8 @@ public void SignOutWithNoAuthMiddlewareMayThrow()

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();
}
}
}
}
28 changes: 10 additions & 18 deletions test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
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
Expand Down Expand Up @@ -57,9 +55,9 @@ public void Host_GetsHostFromHeaders()
// 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);
Expand All @@ -77,7 +75,7 @@ public void Host_DecodesPunyCode()
// 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" } },
};
Expand All @@ -97,7 +95,7 @@ public void Host_EncodesPunyCode()
// 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);

Expand All @@ -108,25 +106,19 @@ public void Host_EncodesPunyCode()
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);
Expand Down