// 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 System; using System.IO; using Microsoft.AspNetCore.Http.Features; using Microsoft.Extensions.Options; using Xunit; namespace Microsoft.AspNetCore.Http { public class HttpContextFactoryTests { [Fact] public void CreateHttpContextSetsHttpContextAccessor() { // Arrange var accessor = new HttpContextAccessor(); var contextFactory = new HttpContextFactory(Options.Create(new FormOptions()), accessor); // Act var context = contextFactory.Create(new FeatureCollection()); // Assert Assert.Same(context, accessor.HttpContext); } [Fact] public void DisposeHttpContextSetsHttpContextAccessorToNull() { // Arrange var accessor = new HttpContextAccessor(); var contextFactory = new HttpContextFactory(Options.Create(new FormOptions()), accessor); // Act var context = contextFactory.Create(new FeatureCollection()); // Assert Assert.Same(context, accessor.HttpContext); contextFactory.Dispose(context); Assert.Null(accessor.HttpContext); } [Fact] public void AllowsCreatingContextWithoutSettingAccessor() { // Arrange var contextFactory = new HttpContextFactory(Options.Create(new FormOptions())); // Act & Assert var context = contextFactory.Create(new FeatureCollection()); contextFactory.Dispose(context); } } }