From f931cb7c6d11786ffdc7c8eab35e2ed4a942b0db Mon Sep 17 00:00:00 2001 From: John Luo Date: Fri, 23 Oct 2015 18:49:25 -0700 Subject: [PATCH] Moving httpcontextfactory to AspNet.Http.Abstractions --- .../IHttpContextFactory.cs | 13 +++++++++ .../HttpContextFactory.cs | 29 +++++++++++++++++++ .../HttpContextFactoryTests.cs | 25 ++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/Microsoft.AspNet.Http.Abstractions/IHttpContextFactory.cs create mode 100644 src/Microsoft.AspNet.Http/HttpContextFactory.cs create mode 100644 test/Microsoft.AspNet.Http.Tests/HttpContextFactoryTests.cs diff --git a/src/Microsoft.AspNet.Http.Abstractions/IHttpContextFactory.cs b/src/Microsoft.AspNet.Http.Abstractions/IHttpContextFactory.cs new file mode 100644 index 0000000000..f0e28db8a7 --- /dev/null +++ b/src/Microsoft.AspNet.Http.Abstractions/IHttpContextFactory.cs @@ -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); + } +} diff --git a/src/Microsoft.AspNet.Http/HttpContextFactory.cs b/src/Microsoft.AspNet.Http/HttpContextFactory.cs new file mode 100644 index 0000000000..4557cd13fb --- /dev/null +++ b/src/Microsoft.AspNet.Http/HttpContextFactory.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Http.Tests/HttpContextFactoryTests.cs b/test/Microsoft.AspNet.Http.Tests/HttpContextFactoryTests.cs new file mode 100644 index 0000000000..8cb2cc6e9b --- /dev/null +++ b/test/Microsoft.AspNet.Http.Tests/HttpContextFactoryTests.cs @@ -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)); + } + } +} \ No newline at end of file