Moving httpcontextfactory to AspNet.Http.Abstractions

This commit is contained in:
John Luo 2015-10-23 18:49:25 -07:00
parent bcb56bdd1a
commit f931cb7c6d
3 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Http.Features;
namespace Microsoft.AspNet.Http
{
public interface IHttpContextFactory
{
HttpContext CreateHttpContext(IFeatureCollection featureCollection);
void Dispose(HttpContext httpContext);
}
}

View File

@ -0,0 +1,29 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Http.Features;
namespace Microsoft.AspNet.Http.Internal
{
public class HttpContextFactory : IHttpContextFactory
{
private IHttpContextAccessor _httpContextAccessor;
public HttpContextFactory(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public HttpContext CreateHttpContext(IFeatureCollection featureCollection)
{
var httpContext = new DefaultHttpContext(featureCollection);
_httpContextAccessor.HttpContext = httpContext;
return httpContext;
}
public void Dispose(HttpContext httpContext)
{
_httpContextAccessor.HttpContext = null;
}
}
}

View File

@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Http.Features;
using Xunit;
namespace Microsoft.AspNet.Http.Internal
{
public class HttpContextFactoryTests
{
[Fact]
public void CreateHttpContextSetsHttpContextAccessor()
{
// Arrange
var accessor = new HttpContextAccessor();
var contextFactory = new HttpContextFactory(accessor);
// Act
var context = contextFactory.CreateHttpContext(new FeatureCollection());
// Assert
Assert.True(ReferenceEquals(context, accessor.HttpContext));
}
}
}