diff --git a/samples/MvcSandbox/Startup.cs b/samples/MvcSandbox/Startup.cs index 774d6bd710..2edcde97b4 100644 --- a/samples/MvcSandbox/Startup.cs +++ b/samples/MvcSandbox/Startup.cs @@ -32,7 +32,14 @@ namespace MvcSandbox public static void Main(string[] args) { - var host = new WebHostBuilder() + var host = CreateWebHostBuilder(args) + .Build(); + + host.Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureLogging(factory => { @@ -42,11 +49,7 @@ namespace MvcSandbox }) .UseIISIntegration() .UseKestrel() - .UseStartup() - .Build(); - - host.Run(); - } + .UseStartup(); } } diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Handlers/CookieContainerHandler.cs b/src/Microsoft.AspNetCore.Mvc.Testing/Handlers/CookieContainerHandler.cs new file mode 100644 index 0000000000..bde4250b50 --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Testing/Handlers/CookieContainerHandler.cs @@ -0,0 +1,61 @@ +// 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.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Net.Http.Headers; + +namespace Microsoft.AspNetCore.Mvc.Testing.Handlers +{ + /// + /// A that manages cookies associated with one or + /// more pairs of and . + /// + public class CookieContainerHandler : DelegatingHandler + { + /// + /// Creates a new instance of . + /// + public CookieContainerHandler() + : this(new CookieContainer()) + { + } + + /// + /// Creates a new instance of . + /// + /// The to use for + /// storing and retrieving cookies. + /// + public CookieContainerHandler(CookieContainer cookieContainer) + { + Container = cookieContainer; + } + + /// + /// Gets the used to store and retrieve cookies. + /// + public CookieContainer Container { get; } + + /// + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var cookieHeader = Container.GetCookieHeader(request.RequestUri); + request.Headers.Add(HeaderNames.Cookie, cookieHeader); + + var response = await base.SendAsync(request, cancellationToken); + + if (response.Headers.TryGetValues(HeaderNames.SetCookie, out var setCookieHeaders)) + { + foreach (var header in setCookieHeaders) + { + Container.SetCookies(response.RequestMessage.RequestUri, header); + } + } + + return response; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Handlers/RedirectHandler.cs b/src/Microsoft.AspNetCore.Mvc.Testing/Handlers/RedirectHandler.cs new file mode 100644 index 0000000000..63158713d3 --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Testing/Handlers/RedirectHandler.cs @@ -0,0 +1,161 @@ +// 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 System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.AspNetCore.Mvc.Testing.Handlers +{ + /// + /// A that follows redirect responses. + /// + public class RedirectHandler : DelegatingHandler + { + internal const int DefaultMaxRedirects = 7; + + /// + /// Creates a new instance of . + /// + public RedirectHandler() + : this(maxRedirects: DefaultMaxRedirects) + { + } + + /// + /// Creates a new instance of . + /// + /// The maximun number of redirect responses to follow. It must be + /// equal or greater than 0. + public RedirectHandler(int maxRedirects) + { + if (maxRedirects <= 0) + { + throw new ArgumentOutOfRangeException(nameof(maxRedirects)); + } + + MaxRedirects = maxRedirects; + } + + /// + /// Gets the maximum number of redirects this handler will follow. + /// + public int MaxRedirects { get; } + + /// + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var remainingRedirects = MaxRedirects; + + var originalRequestContent = HasBody(request) ? await DuplicateRequestContent(request) : null; + var response = await base.SendAsync(request, cancellationToken); + while (IsRedirect(response) && remainingRedirects >= 0) + { + remainingRedirects--; + var redirectRequest = GetRedirectRequest(response, originalRequestContent); + originalRequestContent = HasBody(redirectRequest) ? await DuplicateRequestContent(redirectRequest) : null; + response = await base.SendAsync(redirectRequest, cancellationToken); + } + + return response; + } + + private static bool HasBody(HttpRequestMessage request) => + request.Method == HttpMethod.Post || request.Method == HttpMethod.Put; + + private static async Task DuplicateRequestContent(HttpRequestMessage request) + { + if (request.Content == null) + { + return null; + } + var originalRequestContent = request.Content; + var (originalBody, copy) = await CopyBody(request); + + var contentCopy = new StreamContent(copy); + request.Content = new StreamContent(originalBody); + + CopyContentHeaders(originalRequestContent, request.Content, contentCopy); + + return contentCopy; + } + + private static void CopyContentHeaders( + HttpContent originalRequestContent, + HttpContent newRequestContent, + HttpContent contentCopy) + { + foreach (var header in originalRequestContent.Headers) + { + contentCopy.Headers.Add(header.Key, header.Value); + newRequestContent.Headers.Add(header.Key, header.Value); + } + } + + private static async Task<(Stream originalBody, Stream copy)> CopyBody(HttpRequestMessage request) + { + var originalBody = await request.Content.ReadAsStreamAsync(); + var bodyCopy = new MemoryStream(); + await originalBody.CopyToAsync(bodyCopy); + bodyCopy.Seek(0, SeekOrigin.Begin); + if (originalBody.CanSeek) + { + originalBody.Seek(0, SeekOrigin.Begin); + } + else + { + originalBody = new MemoryStream(); + await bodyCopy.CopyToAsync(originalBody); + originalBody.Seek(0, SeekOrigin.Begin); + bodyCopy.Seek(0, SeekOrigin.Begin); + } + + return (originalBody, bodyCopy); + } + + private static HttpRequestMessage GetRedirectRequest( + HttpResponseMessage response, + HttpContent originalContent) + { + var location = response.Headers.Location; + if (!location.IsAbsoluteUri) + { + location = new Uri( + new Uri(response.RequestMessage.RequestUri.GetLeftPart(UriPartial.Authority)), + location); + } + + var redirect = !ShouldKeepVerb(response) ? + new HttpRequestMessage(HttpMethod.Get, location) : + new HttpRequestMessage(response.RequestMessage.Method, location) + { + Content = originalContent + }; + + foreach (var header in response.RequestMessage.Headers) + { + redirect.Headers.Add(header.Key, header.Value); + } + + foreach (var property in response.RequestMessage.Properties) + { + redirect.Properties.Add(property.Key, property.Value); + } + + return redirect; + } + + private static bool ShouldKeepVerb(HttpResponseMessage response) => + response.StatusCode == HttpStatusCode.RedirectKeepVerb || + (int)response.StatusCode == 308; + + private bool IsRedirect(HttpResponseMessage response) => + response.StatusCode == HttpStatusCode.MovedPermanently || + response.StatusCode == HttpStatusCode.Redirect || + response.StatusCode == HttpStatusCode.RedirectKeepVerb || + (int)response.StatusCode == 308; + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Properties/Resources.Designer.cs b/src/Microsoft.AspNetCore.Mvc.Testing/Properties/Resources.Designer.cs index 41de4ef679..cd0ce4b518 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNetCore.Mvc.Testing/Properties/Resources.Designer.cs @@ -10,6 +10,20 @@ namespace Microsoft.AspNetCore.Mvc.Testing private static readonly ResourceManager _resourceManager = new ResourceManager("Microsoft.AspNetCore.Mvc.Testing.Resources", typeof(Resources).GetTypeInfo().Assembly); + /// + /// No method 'public static {0} CreateWebHostBuilder(string[] args)' found on '{1}'. Alternatively, {2} can be extended and 'protected virtual {0} {3}()' can be overridden to provide your own {0} instance. + /// + internal static string MissingCreateWebHostBuilderMethod + { + get => GetString("MissingCreateWebHostBuilderMethod"); + } + + /// + /// No method 'public static {0} CreateWebHostBuilder(string[] args)' found on '{1}'. Alternatively, {2} can be extended and 'protected virtual {0} {3}()' can be overridden to provide your own {0} instance. + /// + internal static string FormatMissingCreateWebHostBuilderMethod(object p0, object p1, object p2, object p3) + => string.Format(CultureInfo.CurrentCulture, GetString("MissingCreateWebHostBuilderMethod"), p0, p1, p2, p3); + /// /// Can't find'{0}'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g '<PreserveCompilationContext>true</PreserveCompilationContext>'. For functional tests to work they need to either run from the build output folder or the {1} file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run. /// diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Resources.resx b/src/Microsoft.AspNetCore.Mvc.Testing/Resources.resx index 3ba53d0317..8174cf1f2d 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/Resources.resx +++ b/src/Microsoft.AspNetCore.Mvc.Testing/Resources.resx @@ -1,17 +1,17 @@  - @@ -117,6 +117,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + No method 'public static {0} CreateWebHostBuilder(string[] args)' found on '{1}'. Alternatively, {2} can be extended and 'protected virtual {0} {3}()' can be overridden to provide your own {0} instance. + Can't find'{0}'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g '<PreserveCompilationContext>true</PreserveCompilationContext>'. For functional tests to work they need to either run from the build output folder or the {1} file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run. diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs new file mode 100644 index 0000000000..3c9d153e8f --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs @@ -0,0 +1,345 @@ +// 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.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Reflection; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyModel; + +namespace Microsoft.AspNetCore.Mvc.Testing +{ + /// + /// Factory for bootstrapping an application in memory for functional end to end tests. + /// + /// A type in the entry point assembly of the application. + /// Typically the Startup or Program classes can be used. + public class WebApplicationFactory : IDisposable where TEntryPoint : class + { + private TestServer _server; + private Action _configuration; + private IList _clients = new List(); + private List> _derivedFactories = + new List>(); + + /// + /// + /// Creates an instance of . This factory can be used to + /// create a instance using the MVC application defined by + /// and one or more instances used to send to the . + /// The will find the entry point class of + /// assembly and initialize the application by calling IWebHostBuilder CreateWebHostBuilder(string [] args) + /// on . + /// + /// + /// This constructor will infer the application content root path by searching for a + /// on the assembly containing the functional tests with + /// a key equal to the assembly . + /// In case an attribute with the right key can't be found, + /// will fall back to searching for a solution file (*.sln) and then appending asembly name + /// to the solution directory. The application root directory will be used to discover views and content files. + /// + /// + /// The application assemblies will be loaded from the dependency context of the assembly containing + /// . This means that project dependencies of the assembly containing + /// will be loaded as application assemblies. + /// + /// + public WebApplicationFactory() : + this(builder => { }, new WebApplicationFactoryClientOptions()) + { + } + + private WebApplicationFactory(Action configuration, WebApplicationFactoryClientOptions options) + { + _configuration = configuration; + ClientOptions = options; + } + + /// + /// Gets the created by this . + /// + public TestServer Server => _server; + + /// + /// Gets the of factories created from this factory + /// by further customizing the when calling + /// . + /// + public IReadOnlyList> Factories => _derivedFactories.AsReadOnly(); + + /// + /// Gets the used by . + /// + public WebApplicationFactoryClientOptions ClientOptions { get; } + + /// + /// Creates a new with a + /// that is further customized by . + /// + /// + /// An to configure the . + /// + /// A new . + public WebApplicationFactory WithWebHostBuilder(Action configuration) + { + var factory = new WebApplicationFactory(builder => + { + _configuration(builder); + configuration(builder); + }, + new WebApplicationFactoryClientOptions(ClientOptions)); + + _derivedFactories.Add(factory); + + return factory; + } + + private void EnsureServer() + { + if (_server != null) + { + return; + } + + EnsureDepsFile(); + + + var builder = CreateWebHostBuilder(); + SetContentRoot(builder); + ConfigureWebHost(builder); + _configuration(builder); + _server = CreateServer(builder); + } + + private void SetContentRoot(IWebHostBuilder builder) + { + var metadataAttributes = GetContentRootMetadataAttributes( + typeof(TEntryPoint).Assembly.FullName, + typeof(TEntryPoint).Assembly.GetName().Name); + + string contentRoot = null; + for (var i = 0; i < metadataAttributes.Length; i++) + { + var contentRootAttribute = metadataAttributes[i]; + var contentRootCandidate = Path.Combine( + AppContext.BaseDirectory, + contentRootAttribute.ContentRootPath); + + var contentRootMarker = Path.Combine( + contentRootCandidate, + Path.GetFileName(contentRootAttribute.ContentRootTest)); + + if (File.Exists(contentRootMarker)) + { + contentRoot = contentRootCandidate; + break; + } + } + + if (contentRoot != null) + { + builder.UseContentRoot(contentRoot); + } + else + { + builder.UseSolutionRelativeContentRoot(typeof(TEntryPoint).Assembly.GetName().Name); + } + } + + private WebApplicationFactoryContentRootAttribute[] GetContentRootMetadataAttributes( + string tEntryPointAssemblyFullName, + string tEntryPointAssemblyName) + { + var testAssembly = GetTestAssemblies(); + var metadataAttributes = testAssembly + .SelectMany(a => a.GetCustomAttributes()) + .Where(a => string.Equals(a.Key, tEntryPointAssemblyFullName, StringComparison.OrdinalIgnoreCase) || + string.Equals(a.Key, tEntryPointAssemblyName, StringComparison.OrdinalIgnoreCase)) + .OrderBy(a => a.Priority) + .ToArray(); + + return metadataAttributes; + } + + /// + /// Gets the assemblies containing the functional tests. The + /// applied to these + /// assemblies defines the content root to use for the given + /// . + /// + /// The list of containing tests. + protected virtual IEnumerable GetTestAssemblies() + { + try + { + // The default dependency context will be populated in .net core applications. + var context = DependencyContext.Default; + if (context != null) + { + // Find the list of projects + var projects = context.CompileLibraries.Where(l => l.Type == "project"); + + // Find the list of projects runtime information and their assembly names. + var runtimeProjectLibraries = context.RuntimeLibraries + .Where(r => projects.Any(p => p.Name == r.Name)) + .ToDictionary(r => r, r => r.GetDefaultAssemblyNames(context).ToArray()); + + var entryPointAssemblyName = typeof(TEntryPoint).Assembly.GetName().Name; + + // Find the project containing TEntryPoint + var entryPointRuntimeLibrary = runtimeProjectLibraries + .Single(rpl => rpl.Value.Any(a => string.Equals(a.Name, entryPointAssemblyName, StringComparison.Ordinal))); + + // Find the list of projects referencing TEntryPoint. + var candidates = runtimeProjectLibraries + .Where(rpl => rpl.Key.Dependencies + .Any(d => string.Equals(d.Name, entryPointRuntimeLibrary.Key.Name, StringComparison.Ordinal))); + + return candidates.SelectMany(rl => rl.Value).Select(Assembly.Load); + } + else + { + // The app domain friendly name will be populated in full framework. + return new[] { Assembly.Load(AppDomain.CurrentDomain.FriendlyName) }; + } + } + catch (Exception) + { + } + + return Array.Empty(); + } + + private void EnsureDepsFile() + { + var depsFileName = $"{typeof(TEntryPoint).Assembly.GetName().Name}.deps.json"; + var depsFile = new FileInfo(Path.Combine(AppContext.BaseDirectory, depsFileName)); + if (!depsFile.Exists) + { + throw new InvalidOperationException(Resources.FormatMissingDepsFile( + depsFile.FullName, + Path.GetFileName(depsFile.FullName))); + } + } + + /// + /// Creates a used to set up . + /// + /// + /// The default implementation of this method looks for a public static IWebHostBuilder CreateDefaultBuilder(string[] args) + /// method defined on the entry point of the assembly of and invokes it passing an empty string + /// array as arguments. + /// + /// A instance. + protected virtual IWebHostBuilder CreateWebHostBuilder() => + WebHostBuilderFactory.CreateFromTypesAssemblyEntryPoint(Array.Empty()) ?? + throw new InvalidOperationException(Resources.FormatMissingCreateWebHostBuilderMethod( + nameof(IWebHostBuilder), + typeof(TEntryPoint).Assembly.EntryPoint.DeclaringType.FullName, + typeof(WebApplicationFactory).Name, + nameof(CreateWebHostBuilder))); + + /// + /// Creates the with the bootstrapped application in . + /// + /// The used to + /// create the server. + /// The with the bootstrapped application. + protected virtual TestServer CreateServer(IWebHostBuilder builder) => new TestServer(builder); + + /// + /// Gives a fixture an opportunity to configure the application before it gets built. + /// + /// The for the application. + protected virtual void ConfigureWebHost(IWebHostBuilder builder) + { + } + + /// + /// Creates an instance of that automatically follows + /// redirects and handles cookies. + /// + /// The . + public HttpClient CreateClient() => + CreateClient(ClientOptions); + + /// + /// Creates an instance of that automatically follows + /// redirects and handles cookies. + /// + /// The . + public HttpClient CreateClient(WebApplicationFactoryClientOptions options) => + CreateDefaultClient(options.BaseAddress, options.CreateHandlers()); + + /// + /// Creates a new instance of an that can be used to + /// send to the server. The base address of the + /// instance will be set to http://localhost. + /// + /// A list of instances to set up on the + /// . + /// The . + public HttpClient CreateDefaultClient(params DelegatingHandler[] handlers) => + CreateDefaultClient(new Uri("http://localhost"), handlers); + + /// + /// Creates a new instance of an that can be used to + /// send to the server. + /// + /// The base address of the instance. + /// A list of instances to set up on the + /// . + /// The . + public HttpClient CreateDefaultClient(Uri baseAddress, params DelegatingHandler[] handlers) + { + EnsureServer(); + if (handlers == null || handlers.Length == 0) + { + var client = _server.CreateClient(); + client.BaseAddress = baseAddress; + + return client; + } + else + { + for (var i = handlers.Length - 1; i > 0; i--) + { + handlers[i - 1].InnerHandler = handlers[i]; + } + + var serverHandler = _server.CreateHandler(); + handlers[handlers.Length - 1].InnerHandler = serverHandler; + + var client = new HttpClient(handlers[0]) + { + BaseAddress = baseAddress + }; + + _clients.Add(client); + + return client; + } + } + + /// + public void Dispose() + { + foreach (var client in _clients) + { + client.Dispose(); + } + + foreach (var factory in _derivedFactories) + { + factory.Dispose(); + } + + _server?.Dispose(); + } + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactoryClientOptions.cs b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactoryClientOptions.cs new file mode 100644 index 0000000000..c934b2fd56 --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactoryClientOptions.cs @@ -0,0 +1,83 @@ +// 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.Collections.Generic; +using System.Linq; +using System.Net.Http; +using Microsoft.AspNetCore.Mvc.Testing.Handlers; + +namespace Microsoft.AspNetCore.Mvc.Testing +{ + /// + /// The default options to use to when creating + /// instances by calling + /// . + /// + public class WebApplicationFactoryClientOptions + { + /// + /// Initializes a new instance of . + /// + public WebApplicationFactoryClientOptions() + { + } + + // Copy constructor + internal WebApplicationFactoryClientOptions(WebApplicationFactoryClientOptions clientOptions) + { + BaseAddress = clientOptions.BaseAddress; + AllowAutoRedirect = clientOptions.AllowAutoRedirect; + MaxAutomaticRedirections = clientOptions.MaxAutomaticRedirections; + HandleCookies = clientOptions.HandleCookies; + } + + /// + /// Gets or sets the base address of instances created by calling + /// . + /// The default is http://localhost. + /// + public Uri BaseAddress { get; set; } = new Uri("http://localhost"); + + /// + /// Gets or sets whether or not instances created by calling + /// + /// should automatically follow redirect responses. + /// The default is true. + /// /// + public bool AllowAutoRedirect { get; set; } = true; + + /// + /// Gets or sets the maximum number of redirect responses that instances + /// created by calling + /// should follow. + /// The default is 7. + /// + public int MaxAutomaticRedirections { get; set; } = RedirectHandler.DefaultMaxRedirects; + + /// + /// Gets or sets whether instances created by calling + /// + /// should handle cookies. + /// The default is true. + /// + public bool HandleCookies { get; set; } = true; + + internal DelegatingHandler[] CreateHandlers() + { + return CreateHandlersCore().ToArray(); + + IEnumerable CreateHandlersCore() + { + if (AllowAutoRedirect) + { + yield return new RedirectHandler(MaxAutomaticRedirections); + } + if (HandleCookies) + { + yield return new CookieContainerHandler(); + } + } + } + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactoryContentRootAttribute.cs b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactoryContentRootAttribute.cs new file mode 100644 index 0000000000..a2ea31cb45 --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactoryContentRootAttribute.cs @@ -0,0 +1,83 @@ +// 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.Globalization; +using System.IO; +using System.Reflection; + +namespace Microsoft.AspNetCore.Mvc.Testing +{ + /// + /// Metadata that uses to find out the content + /// root for the web application represented by TEntryPoint. + /// will iterate over all the instances of + /// , filter the instances whose + /// is equal to TEntryPoint , + /// order them by in ascending order. + /// will check for the existence of the marker + /// in Path.Combine(, Path.GetFileName())" + /// and if the file exists it will set the content root to . + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)] + public sealed class WebApplicationFactoryContentRootAttribute : Attribute + { + /// + /// Initializes a new instance of . + /// + /// + /// The key of this . This + /// key is used by to determine what of the + /// instances on the test assembly should be used + /// to match a given TEntryPoint class. + /// + /// The path to the content root. This path can be either relative or absolute. + /// In case the path is relative, the path will be combined with + /// + /// + /// A file that will be use as a marker to determine that the content root path for the given context is correct. + /// + /// + /// The priority of this content root attribute compared to other attributes. When + /// multiple instances are applied for the + /// same key, they are processed with , ordered in ascending order and applied + /// in priority until a match is found. + /// + public WebApplicationFactoryContentRootAttribute( + string key, + string contentRootPath, + string contentRootTest, + string priority) + { + Key = key; + ContentRootPath = contentRootPath; + ContentRootTest = contentRootTest; + if (int.TryParse(priority, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedPriority)) + { + Priority = parsedPriority; + } + } + + /// + /// Gets the key for the content root associated with this project. Typically . + /// + public string Key { get; } + + /// + /// Gets the content root path for a given project. This content root can be relative or absolute. If it is a + /// relative path, it will be combined with . + /// + public string ContentRootPath { get; } + + /// + /// A marker file used to ensure that the path the content root is being set to is correct. + /// + public string ContentRootTest { get; } + + /// + /// Gets a number for determining the probing order when multiple + /// instances with the same key are present on the test . + /// + public int Priority { get; } + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs deleted file mode 100644 index 0e321955bc..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs +++ /dev/null @@ -1,200 +0,0 @@ -// 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 System.Net.Http; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.TestHost; - -namespace Microsoft.AspNetCore.Mvc.Testing -{ - /// - /// Fixture for bootstrapping an application in memory for functional end to end tests. - /// - /// The applications startup class. - public class WebApplicationTestFixture : IDisposable where TStartup : class - { - private readonly TestServer _server; - - /// - /// - /// Creates a TestServer instance using the MVC application defined by. - /// The startup code defined in will be executed to configure the application. - /// - /// - /// This constructor will infer the application root directive by searching for a solution file (*.sln) and then - /// appending the path{AssemblyName} to the solution directory.The application root directory will be - /// used to discover views and content files. - /// - /// - /// The application assemblies will be loaded from the dependency context of the assembly containing - /// .This means that project dependencies of the assembly containing - /// will be loaded as application assemblies. - /// - /// - public WebApplicationTestFixture() - : this(typeof(TStartup).Assembly.GetName().Name) - { - } - - /// - /// - /// Creates a TestServer instance using the MVC application defined by. - /// The startup code defined in will be executed to configure the application. - /// - /// - /// This constructor will infer the application root directive by searching for a solution file (*.sln) and then - /// appending the path to the solution directory.The application root - /// directory will be used to discover views and content files. - /// - /// - /// The application assemblies will be loaded from the dependency context of the assembly containing - /// .This means that project dependencies of the assembly containing - /// will be loaded as application assemblies. - /// - /// - /// The path to the project folder relative to the solution file of your - /// application. The folder of the first .sln file found traversing up the folder hierarchy from the test execution - /// folder is considered as the base path. - protected WebApplicationTestFixture(string solutionRelativePath) - : this("*.sln", solutionRelativePath) - { - } - - /// - /// - /// Creates a TestServer instance using the MVC application defined by. - /// The startup code defined in will be executed to configure the application. - /// - /// - /// This constructor will infer the application root directive by searching for a solution file that matches the pattern - /// and then appending the path - /// to the solution directory.The application root directory will be used to discover views and content files. - /// - /// - /// The application assemblies will be loaded from the dependency context of the assembly containing - /// .This means that project dependencies of the assembly containing - /// will be loaded as application assemblies. - /// - /// - /// The glob pattern to use when searching for a solution file by - /// traversing up the folder hierarchy from the test execution folder. - /// The path to the project folder relative to the solution file of your - /// application. The folder of the first sln file that matches the - /// found traversing up the folder hierarchy from the test execution folder is considered as the base path. - protected WebApplicationTestFixture(string solutionSearchPattern, string solutionRelativePath) - { - EnsureDepsFile(); - - var builder = CreateWebHostBuilder(); - builder - .UseStartup() - .UseSolutionRelativeContentRoot(solutionRelativePath); - - ConfigureWebHost(builder); - _server = CreateServer(builder); - - Client = CreateClient(); - } - - private void EnsureDepsFile() - { - var depsFileName = $"{typeof(TStartup).Assembly.GetName().Name}.deps.json"; - var depsFile = new FileInfo(Path.Combine(AppContext.BaseDirectory, depsFileName)); - if (!depsFile.Exists) - { - throw new InvalidOperationException(Resources.FormatMissingDepsFile( - depsFile.FullName, - Path.GetFileName(depsFile.FullName))); - } - } - - /// - /// Creates a used to setup . - /// - /// The default implementation of this method looks for a public static IWebHostBuilder CreateDefaultBuilder(string[] args) - /// method defined on the entry point of the assembly of and invokes it passing an empty string - /// array as arguments. In case this method can't be found, - /// - /// - /// A instance. - protected virtual IWebHostBuilder CreateWebHostBuilder() => - WebHostBuilderFactory.CreateFromTypesAssemblyEntryPoint(Array.Empty()) ?? new WebHostBuilder(); - - /// - /// Creates the with the bootstrapped application in . - /// - /// The used to - /// create the server. - /// The with the bootstrapped application. - protected virtual TestServer CreateServer(IWebHostBuilder builder) => new TestServer(builder); - - /// - /// Gives a fixture an opportunity to configure the application before it gets built. - /// - /// The for the application. - protected virtual void ConfigureWebHost(IWebHostBuilder builder) - { - } - - /// - /// Gets an instance of the used to send to the server. - /// - public HttpClient Client { get; } - - /// - /// Creates a new instance of an that can be used to - /// send to the server. - /// - /// The - public HttpClient CreateClient() - { - var client = _server.CreateClient(); - client.BaseAddress = new Uri("http://localhost"); - - return client; - } - - /// - /// Creates a new instance of an that can be used to - /// send to the server. - /// - /// The base address of the instance. - /// A list of instances to setup on the - /// . - /// The . - public HttpClient CreateClient(Uri baseAddress, params DelegatingHandler[] handlers) - { - if (handlers == null || handlers.Length == 0) - { - var client = _server.CreateClient(); - client.BaseAddress = baseAddress; - - return client; - } - else - { - - for (var i = handlers.Length - 1; i > 1; i--) - { - handlers[i - 1].InnerHandler = handlers[i]; - } - - var serverHandler = _server.CreateHandler(); - handlers[handlers.Length - 1].InnerHandler = serverHandler; - var client = new HttpClient(handlers[0]); - client.BaseAddress = baseAddress; - - return client; - } - } - - /// - public void Dispose() - { - Client.Dispose(); - _server.Dispose(); - } - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Testing.targets b/src/Microsoft.AspNetCore.Mvc.Testing/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Testing.targets index c1637af208..f90a0792b3 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Testing.targets +++ b/src/Microsoft.AspNetCore.Mvc.Testing/build/netstandard2.0/Microsoft.AspNetCore.Mvc.Testing.targets @@ -15,11 +15,43 @@ true - + + + <_ContentRootProjectReferences Include="@(ReferencePath)" Condition="'%(ReferencePath.ReferenceSourceTarget)' == 'ProjectReference'" /> + + + + - + + + + + + <_Parameter1>%(WebApplicationFactoryContentRootAttribute.AssemblyName) + <_Parameter2>%(WebApplicationFactoryContentRootAttribute.ContentRootPath) + <_Parameter3>%(WebApplicationFactoryContentRootAttribute.ContentRootTest) + <_Parameter4>%(WebApplicationFactoryContentRootAttribute.Priority) + + + + + + + + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryAuthTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryAuthTests.cs index 02f791c7a7..86e31edfff 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryAuthTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryAuthTests.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public AntiforgeryAuthTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryTests.cs index 43967aca1f..78fc00bec6 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryTests.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public AntiforgeryTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs index bf0fc0cfa1..3f7ff1a9cb 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public ApiBehaviorTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiExplorerTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiExplorerTest.cs index 7b8ae8d175..774e0de862 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiExplorerTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiExplorerTest.cs @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public ApiExplorerTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApplicationModelTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApplicationModelTest.cs index 490f2d1a06..a698af7ba4 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApplicationModelTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApplicationModelTest.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public ApplicationModelTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AsyncActionsTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AsyncActionsTests.cs index 222d029176..9f932f87a2 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AsyncActionsTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/AsyncActionsTests.cs @@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public AsyncActionsTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs index b717f398d1..6df040c055 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs @@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public BasicTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CombineAuthorizeTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CombineAuthorizeTests.cs index 7f9b520466..839c10081e 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CombineAuthorizeTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CombineAuthorizeTests.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public CombineAuthorizeTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CompilationOptionsTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CompilationOptionsTests.cs index b381d58ad4..a0efcd00d1 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CompilationOptionsTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CompilationOptionsTests.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public CompilationOptionsTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ConsumesAttributeTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ConsumesAttributeTests.cs index 979a0a7bf2..181094b715 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ConsumesAttributeTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ConsumesAttributeTests.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public ConsumesAttributeTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ContentNegotiationTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ContentNegotiationTest.cs index 75a684c4a4..850ae97c7d 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ContentNegotiationTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ContentNegotiationTest.cs @@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public ContentNegotiationTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ControllerFromServicesTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ControllerFromServicesTests.cs index fcc926097b..01fb7c83b7 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ControllerFromServicesTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ControllerFromServicesTests.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public ControllerFromServicesTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CorsTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CorsTests.cs index 8bb83b8b53..5d92c7e6eb 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CorsTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/CorsTests.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public CorsTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DefaultOrderTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DefaultOrderTest.cs index ba389c6036..676ff499fa 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DefaultOrderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DefaultOrderTest.cs @@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public DefaultOrderTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DefaultValuesTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DefaultValuesTest.cs index 1a4fb90a3d..cfca842fee 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DefaultValuesTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DefaultValuesTest.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public DefaultValuesTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DirectivesTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DirectivesTest.cs index ccfd07949b..15ebfe4e45 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DirectivesTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DirectivesTest.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public DirectivesTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DoNotRespectBrowserAcceptHeaderTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DoNotRespectBrowserAcceptHeaderTests.cs index b486b66009..b446613c26 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DoNotRespectBrowserAcceptHeaderTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/DoNotRespectBrowserAcceptHeaderTests.cs @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public DoNotRespectBrowserAcceptHeaderTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ErrorPageTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ErrorPageTests.cs index 8ede2636f9..644a4ac067 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ErrorPageTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ErrorPageTests.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests "'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false."); public ErrorPageTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FileResultTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FileResultTests.cs index 150e490239..f49cc631fa 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FileResultTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FileResultTests.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public FileResultTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FiltersTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FiltersTest.cs index d08aed992a..e3deb769d3 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FiltersTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FiltersTest.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public FiltersTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FlushPointTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FlushPointTest.cs index 5f69a2226f..1ea8536830 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FlushPointTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FlushPointTest.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public FlushPointTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FormFileUploadTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FormFileUploadTest.cs index 9e4208693c..e75fbf039c 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FormFileUploadTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/FormFileUploadTest.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public FormFileUploadTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/GlobalAuthorizationFilterTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/GlobalAuthorizationFilterTest.cs index 94ea6c7033..aaab315e59 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/GlobalAuthorizationFilterTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/GlobalAuthorizationFilterTest.cs @@ -5,6 +5,8 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests @@ -13,9 +15,13 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public GlobalAuthorizationFilterTest(MvcTestFixture fixture) { - Client = fixture.Client; + var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder); + Client = factory.CreateDefaultClient(); } + private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => + builder.UseStartup(); + public HttpClient Client { get; } [Fact] @@ -66,7 +72,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests Assert.Equal( "http://localhost/Home/Login?ReturnUrl=%2FAdministration%2FEitherCookie", response.Headers.Location.ToString()); - } - + } + } } \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/HtmlGenerationTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/HtmlGenerationTest.cs index 07d5712894..9e3521d851 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/HtmlGenerationTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/HtmlGenerationTest.cs @@ -23,8 +23,8 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests MvcTestFixture fixture, MvcEncodedTestFixture encodedFixture) { - Client = fixture.Client; - EncodedClient = encodedFixture.Client; + Client = fixture.CreateDefaultClient(); + EncodedClient = encodedFixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/HtmlHelperOptionsTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/HtmlHelperOptionsTest.cs index 76faef248d..41d1c2e9a7 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/HtmlHelperOptionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/HtmlHelperOptionsTest.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public HtmlHelperOptionsTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcSampleFixture.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcSampleFixture.cs deleted file mode 100644 index 53ebde8045..0000000000 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcSampleFixture.cs +++ /dev/null @@ -1,13 +0,0 @@ -// 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.IO; - -namespace Microsoft.AspNetCore.Mvc.FunctionalTests -{ - public class MvcSampleFixture : MvcTestFixture - where TStartup : class - { - public MvcSampleFixture() : base(Path.Combine("samples", typeof(TStartup).Assembly.GetName().Name)) { } - } -} diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs index f1c9ed99e0..579de362fd 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs @@ -1,6 +1,7 @@ // 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.Globalization; using System.IO; using Microsoft.AspNetCore.Hosting; @@ -9,19 +10,9 @@ using Microsoft.AspNetCore.TestHost; namespace Microsoft.AspNetCore.Mvc.FunctionalTests { - public class MvcTestFixture : WebApplicationTestFixture + public class MvcTestFixture : WebApplicationFactory where TStartup : class { - public MvcTestFixture() - : base(Path.Combine("test", "WebSites", typeof(TStartup).Assembly.GetName().Name)) - { - } - - protected MvcTestFixture(string solutionRelativePath) - : base(solutionRelativePath) - { - } - protected override void ConfigureWebHost(IWebHostBuilder builder) => builder.UseRequestCulture("en-GB", "en-US"); diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputFormatterTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputFormatterTests.cs index 21a4b615d7..e0d6950fb0 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputFormatterTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputFormatterTests.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public InputFormatterTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputObjectValidationTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputObjectValidationTests.cs index 1562a189aa..dbe25c6c92 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputObjectValidationTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputObjectValidationTests.cs @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public InputObjectValidationTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputValidationTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputValidationTests.cs index 4a8583d345..5c5c9e7189 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputValidationTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/InputValidationTests.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public InputValidationTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonOutputFormatterTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonOutputFormatterTests.cs index c8e8b2cd59..09ed5b4a07 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonOutputFormatterTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonOutputFormatterTests.cs @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public JsonOutputFormatterTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonPatchInputFormatterTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonPatchInputFormatterTest.cs index b70029aa61..211ff54a5e 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonPatchInputFormatterTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonPatchInputFormatterTest.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public JsonPatchSampleTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonResultTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonResultTest.cs index ebf4e8846f..f885d4ed28 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/JsonResultTest.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public JsonResultTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/LinkGenerationTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/LinkGenerationTests.cs index ab29918aee..f2328da15b 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/LinkGenerationTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/LinkGenerationTests.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public LinkGenerationTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Microsoft.AspNetCore.Mvc.FunctionalTests.csproj b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Microsoft.AspNetCore.Mvc.FunctionalTests.csproj index d0790249be..54e7694ffa 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Microsoft.AspNetCore.Mvc.FunctionalTests.csproj +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Microsoft.AspNetCore.Mvc.FunctionalTests.csproj @@ -16,6 +16,24 @@ + + + + + + + diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSandboxTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSandboxTest.cs index 7387eb4200..45d88acbf7 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSandboxTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSandboxTest.cs @@ -7,11 +7,11 @@ using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests { - public class MvcSandboxTest : IClassFixture> + public class MvcSandboxTest : IClassFixture> { - public MvcSandboxTest(MvcSampleFixture fixture) + public MvcSandboxTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/OutputFormatterTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/OutputFormatterTest.cs index 6f782f4838..41cf8029ac 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/OutputFormatterTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/OutputFormatterTest.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public OutputFormatterTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorBuildTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorBuildTest.cs index fc5920d906..8be08f4d48 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorBuildTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorBuildTest.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public RazorBuildTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPageExecutionInstrumentationTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPageExecutionInstrumentationTest.cs index f943484df2..b6e87cf1a5 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPageExecutionInstrumentationTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPageExecutionInstrumentationTest.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public RazorPageExecutionInstrumentationTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPageModelTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPageModelTest.cs index fa9ae59b8d..66daab9783 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPageModelTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPageModelTest.cs @@ -2,9 +2,11 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests @@ -13,9 +15,13 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public RazorPageModelTest(MvcTestFixture fixture) { - Client = fixture.Client; + var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder); + Client = factory.CreateDefaultClient(); } + private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => + builder.UseStartup(); + public HttpClient Client { get; } [Fact] diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesNamespaceTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesNamespaceTest.cs index e1feea929f..f5260d9dca 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesNamespaceTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesNamespaceTest.cs @@ -1,12 +1,10 @@ // 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.Collections.Generic; using System.Linq; -using System.Net; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests @@ -15,9 +13,13 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public RazorPagesNamespaceTest(MvcTestFixture fixture) { - Client = fixture.Client; + var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder); + Client = factory.CreateDefaultClient(); } + private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => + builder.UseStartup(); + public HttpClient Client { get; } [Fact] diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs index ea36e1bb7c..2783d7ee44 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs @@ -9,6 +9,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Reflection; using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests @@ -19,9 +20,13 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public RazorPagesTest(MvcTestFixture fixture) { - Client = fixture.Client; + var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder); + Client = factory.CreateDefaultClient(); } + private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => + builder.UseStartup(); + public HttpClient Client { get; } [Fact] diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesViewSearchTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesViewSearchTest.cs index 1493658850..a22742bf5d 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesViewSearchTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesViewSearchTest.cs @@ -1,12 +1,10 @@ // 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.Collections.Generic; using System.Linq; -using System.Net; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests @@ -15,9 +13,13 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public RazorPagesViewSearchTest(MvcTestFixture fixture) { - Client = fixture.Client; + var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder); + Client = factory.CreateDefaultClient(); } + private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => + builder.UseStartup(); + public HttpClient Client { get; } [Fact] diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesWithBasePathTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesWithBasePathTest.cs index c5d97817cb..3fdebf7eb0 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesWithBasePathTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesWithBasePathTest.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public RazorPagesWithBasePathTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorViewLocationSpecificationTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorViewLocationSpecificationTest.cs index 517bf01dd7..e2de2dd2f8 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorViewLocationSpecificationTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorViewLocationSpecificationTest.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public RazorViewLocationSpecificationTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RemoteAttributeValidationTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RemoteAttributeValidationTest.cs index 0cc85d70d2..5e1919c462 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RemoteAttributeValidationTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RemoteAttributeValidationTest.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public RemoteAttributeValidationTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestFormLimitsTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestFormLimitsTest.cs index e0d5beef47..285b6cc397 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestFormLimitsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestFormLimitsTest.cs @@ -2,10 +2,12 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Http; using System.Reflection; using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests @@ -14,9 +16,13 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public RequestFormLimitsTest(MvcTestFixture fixture) { - Client = fixture.Client; + var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder); + Client = factory.CreateDefaultClient(); } + private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => + builder.UseStartup(); + public HttpClient Client { get; } [Fact] @@ -66,7 +72,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests var result = await response.Content.ReadAsStringAsync(); Assert.Equal(expected, result); } - + [Fact] public async Task OverrideControllerLevelLimits_UsingDefaultLimits() { diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestServicesTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestServicesTest.cs index 56f0699d4f..29fd69112c 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestServicesTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestServicesTest.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public RequestServicesTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestSizeLimitTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestSizeLimitTest.cs index 7376e77fc4..8e5ce315d3 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestSizeLimitTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RequestSizeLimitTest.cs @@ -3,12 +3,14 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Reflection; using System.Text; using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests @@ -23,9 +25,13 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public RequestSizeLimitTest(MvcTestFixture fixture) { - Client = fixture.Client; + var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder); + Client = factory.CreateDefaultClient(); } + private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => + builder.UseStartup(); + public HttpClient Client { get; } [Fact] diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RespectBrowserAcceptHeaderTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RespectBrowserAcceptHeaderTests.cs index 035aef5a30..e796b034cd 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RespectBrowserAcceptHeaderTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RespectBrowserAcceptHeaderTests.cs @@ -1,9 +1,11 @@ // 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.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests @@ -15,9 +17,13 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public RespectBrowserAcceptHeaderTests(MvcTestFixture fixture) { - Client = fixture.Client; + var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder); + Client = factory.CreateDefaultClient(); } + private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => + builder.UseStartup(); + public HttpClient Client { get; } [Fact] diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RouteDataTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RouteDataTest.cs index 57330d5e7b..3ea689b1a4 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RouteDataTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RouteDataTest.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public RouteDataTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RoutingTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RoutingTests.cs index 97083dac6c..e2917d8f1e 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RoutingTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RoutingTests.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public RoutingTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/SerializableErrorTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/SerializableErrorTests.cs index 6c86fd75dd..7f073ee9a3 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/SerializableErrorTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/SerializableErrorTests.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public SerializableErrorTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/SimpleTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/SimpleTests.cs index 94cebf8e4b..b5169c96ab 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/SimpleTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/SimpleTests.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public SimpleTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/StreamOutputFormatterTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/StreamOutputFormatterTest.cs index a8c517ad6a..23db399359 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/StreamOutputFormatterTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/StreamOutputFormatterTest.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public StreamOutputFormatterTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelperComponentTagHelperTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelperComponentTagHelperTest.cs index 35f7d55be9..c388e93f3a 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelperComponentTagHelperTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelperComponentTagHelperTest.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public TagHelperComponentTagHelperTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelpersFromServicesTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelpersFromServicesTest.cs index 2f083e06df..6d1c7cf47d 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelpersFromServicesTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelpersFromServicesTest.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public TagHelpersFromServicesTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelpersTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelpersTest.cs index ae2eb576be..b3c4d051aa 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelpersTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelpersTest.cs @@ -25,8 +25,8 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests MvcTestFixture fixture, MvcEncodedTestFixture encodedFixture) { - Client = fixture.Client; - EncodedClient = encodedFixture.Client; + Client = fixture.CreateDefaultClient(); + EncodedClient = encodedFixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesTest.cs index b27db0d5dd..b68cd88081 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesTest.cs @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public TempDataInCookiesTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } protected override HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesUsingCookieConsentTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesUsingCookieConsentTest.cs index c109e64a41..4c8c173a6f 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesUsingCookieConsentTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInCookiesUsingCookieConsentTest.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; using Microsoft.Net.Http.Headers; using Xunit; @@ -20,9 +21,13 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public TempDataInCookiesUsingCookieConsentTest( MvcTestFixture fixture) { - _client = fixture.Client; + var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder); + _client = factory.CreateDefaultClient(); } + private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => + builder.UseStartup(); + [Fact] public async Task CookieTempDataProviderCookie_SetInResponse_OnGrantingConsent() { diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInSessionTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInSessionTest.cs index 39e3edc551..6c669f103c 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInSessionTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataInSessionTest.cs @@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public TempDataInSessionTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } protected override HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataPropertyTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataPropertyTest.cs index a6268cfa8d..18a1870efd 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataPropertyTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TempDataPropertyTest.cs @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public TempDataPropertyTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } [Fact] diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TestingInfrastructureTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TestingInfrastructureTests.cs new file mode 100644 index 0000000000..9e5be478f6 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TestingInfrastructureTests.cs @@ -0,0 +1,97 @@ +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Formatting; +using System.Threading.Tasks; +using BasicWebSite; +using BasicWebSite.Controllers; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Microsoft.AspNetCore.Mvc.FunctionalTests +{ + public class TestingInfrastructureTests : IClassFixture> + { + public TestingInfrastructureTests(WebApplicationFactory fixture) + { + var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder); + Client = factory.CreateClient(); + } + + private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => + builder.ConfigureTestServices(s => s.AddSingleton()); + + public HttpClient Client { get; } + + [Fact] + public async Task TestingInfrastructure_CanOverrideServiceFromWithinTheTest() + { + // Act + var response = await Client.GetStringAsync("Testing/Builder"); + + // Assert + Assert.Equal("Test", response); + } + + [Fact] + public async Task TestingInfrastructure_RedirectHandlerWorksWithPreserveMethod() + { + // Act + var request = new HttpRequestMessage(HttpMethod.Post, "Testing/RedirectHandler/2") + { + Content = new ObjectContent(new Number { Value = 5 }, new JsonMediaTypeFormatter()) + }; + request.Headers.Add("X-Pass-Thru", "Some-Value"); + var response = await Client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + var xPassThruValue = Assert.Single(response.Headers.GetValues("X-Pass-Thru")); + Assert.Equal("Some-Value", xPassThruValue); + + var handlerResponse = await response.Content.ReadAsAsync(); + Assert.Equal(5, handlerResponse.Url); + Assert.Equal(5, handlerResponse.Body); + } + + [Fact] + public async Task TestingInfrastructure_PostRedirectGetWorksWithCookies() + { + // Act + var acquireToken = await Client.GetAsync("Testing/AntiforgerySimulator/3"); + Assert.Equal(HttpStatusCode.OK, acquireToken.StatusCode); + + var response = await Client.PostAsync( + "Testing/PostRedirectGet/Post/3", + content: null); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + var handlerResponse = await response.Content.ReadAsAsync(); + Assert.Equal(4, handlerResponse.TempDataValue); + Assert.Equal("Value-4", handlerResponse.CookieValue); + } + + [Fact] + public async Task TestingInfrastructure_PutWithoutBodyFollowsRedirects() + { + // Act + var response = await Client.PutAsync("Testing/Put/3", content: null); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(5, await response.Content.ReadAsAsync()); + } + + private class OverridenService : TestService + { + public OverridenService() + { + Message = "Test"; + } + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/UrlResolutionTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/UrlResolutionTest.cs index b8a7a42253..4ff086953b 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/UrlResolutionTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/UrlResolutionTest.cs @@ -18,8 +18,8 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests MvcTestFixture fixture, MvcEncodedTestFixture encodedFixture) { - Client = fixture.Client; - EncodedClient = encodedFixture.Client; + Client = fixture.CreateDefaultClient(); + EncodedClient = encodedFixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/VersioningTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/VersioningTests.cs index d9c45a381b..02a86d0871 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/VersioningTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/VersioningTests.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public VersioningTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ViewComponentFromServicesTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ViewComponentFromServicesTests.cs index 446f159a02..f219c34613 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ViewComponentFromServicesTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ViewComponentFromServicesTests.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public ViewComponentFromServicesTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ViewEngineTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ViewEngineTests.cs index ab6c1f9620..363f534469 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ViewEngineTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ViewEngineTests.cs @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public ViewEngineTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimActionResultTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimActionResultTest.cs index 5df40f3129..1c4b7bea7b 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimActionResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimActionResultTest.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public WebApiCompatShimActionResultTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimActionSelectionTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimActionSelectionTest.cs index 0611c0fe1b..e13e6fff31 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimActionSelectionTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimActionSelectionTest.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public WebApiCompatShimActionSelectionTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimBasicTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimBasicTest.cs index 39c8d8baf8..9080f544e9 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimBasicTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimBasicTest.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public WebApiCompatShimBasicTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimParameterBindingTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimParameterBindingTest.cs index 0828bc8cf6..4c09b37d2e 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimParameterBindingTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/WebApiCompatShimParameterBindingTest.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public WebApiCompatShimParameterBindingTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlDataContractSerializerFormattersWrappingTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlDataContractSerializerFormattersWrappingTest.cs index 5bd38a326c..5e6443a862 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlDataContractSerializerFormattersWrappingTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlDataContractSerializerFormattersWrappingTest.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public XmlDataContractSerializerFormattersWrappingTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlDataContractSerializerInputFormatterTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlDataContractSerializerInputFormatterTest.cs index 97cac2b253..3b97473fdd 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlDataContractSerializerInputFormatterTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlDataContractSerializerInputFormatterTest.cs @@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public XmlDataContractSerializerInputFormatterTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlOutputFormatterTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlOutputFormatterTests.cs index cc9e8eb5be..fb4102d2c0 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlOutputFormatterTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlOutputFormatterTests.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public XmlOutputFormatterTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlSerializerFormattersWrappingTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlSerializerFormattersWrappingTest.cs index 51e7e501e1..2b8ddd9a06 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlSerializerFormattersWrappingTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlSerializerFormattersWrappingTest.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public XmlSerializerFormattersWrappingTest(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlSerializerInputFormatterTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlSerializerInputFormatterTests.cs index e9e6675cff..2f9cb57cba 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlSerializerInputFormatterTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/XmlSerializerInputFormatterTests.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public XmlSerializerInputFormatterTests(MvcTestFixture fixture) { - Client = fixture.Client; + Client = fixture.CreateDefaultClient(); } public HttpClient Client { get; } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/msbuild.binlog b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/msbuild.binlog new file mode 100644 index 0000000000..372871a1fb Binary files /dev/null and b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/msbuild.binlog differ diff --git a/test/WebSites/ApiExplorerWebSite/Startup.cs b/test/WebSites/ApiExplorerWebSite/Startup.cs index e73c55fc87..e794f807ef 100644 --- a/test/WebSites/ApiExplorerWebSite/Startup.cs +++ b/test/WebSites/ApiExplorerWebSite/Startup.cs @@ -51,15 +51,18 @@ namespace ApiExplorerWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseKestrel() - .UseIISIntegration() - .UseStartup() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseKestrel() + .UseIISIntegration() + .UseStartup(); } } diff --git a/test/WebSites/ApplicationModelWebSite/Startup.cs b/test/WebSites/ApplicationModelWebSite/Startup.cs index d750fcf946..37e97287fb 100644 --- a/test/WebSites/ApplicationModelWebSite/Startup.cs +++ b/test/WebSites/ApplicationModelWebSite/Startup.cs @@ -38,15 +38,18 @@ namespace ApplicationModelWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/BasicWebSite/Controllers/TestingController.cs b/test/WebSites/BasicWebSite/Controllers/TestingController.cs index 37256b3bb9..07e76a3d6b 100644 --- a/test/WebSites/BasicWebSite/Controllers/TestingController.cs +++ b/test/WebSites/BasicWebSite/Controllers/TestingController.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace BasicWebSite.Controllers { - public class TestingController + public class TestingController : Controller { public TestingController(TestService service) { @@ -17,5 +18,94 @@ namespace BasicWebSite.Controllers [HttpGet("Testing/Builder")] public string Get() => Service.Message; + + [HttpPost("Testing/RedirectHandler/{value}")] + public IActionResult RedirectHandler( + [FromRoute]int value, + [FromBody] Number number, + [FromHeader(Name = "X-Pass-Thru")] string passThruValue) + { + Response.Headers.Add("X-Pass-Thru", passThruValue); + if (value < number.Value) + { + return RedirectToActionPreserveMethod( + nameof(RedirectHandler), + "Testing", + new { value = value + 1 }); + } + + return Ok(new RedirectHandlerResponse { Url = value, Body = number.Value }); + } + + [HttpGet("Testing/AntiforgerySimulator/{value}")] + public IActionResult AntiforgerySimulator([FromRoute]int value) + { + Response.Cookies.Append( + "AntiforgerySimulator", + $"Cookie-{value.ToString(CultureInfo.InvariantCulture)}"); + + return Ok(); + } + + + [HttpPost("Testing/PostRedirectGet/Post/{value}")] + public IActionResult PostRedirectGetPost([FromRoute]int value) + { + var compareValue = $"Cookie-{value.ToString(CultureInfo.InvariantCulture)}"; + if (!Request.Cookies.ContainsKey("AntiforgerySimulator")) + { + return BadRequest("Missing AntiforgerySimulator cookie"); + } + + if (!string.Equals(compareValue, Request.Cookies["AntiforgerySimulator"])) + { + return BadRequest("Values don't match"); + } + + TempData["Value"] = value + 1; + Response.Cookies.Append("Message", $"Value-{(value + 1).ToString(CultureInfo.InvariantCulture)}"); + + return RedirectToAction(nameof(PostRedirectGetGet)); + } + + [HttpGet("Testing/PostRedirectGet/Get/{value}")] + public IActionResult PostRedirectGetGet([FromRoute]int value) + { + return Ok(new PostRedirectGetGetResponse + { + TempDataValue = (int)TempData["Value"], + CookieValue = Request.Cookies["Message"] + }); + } + + [HttpPut("Testing/Put/{value}")] + public IActionResult PutNoBody([FromRoute]int value) + { + if (value < 5) + { + return RedirectToActionPermanentPreserveMethod(nameof(PutNoBody), "Testing", new { value = value + 1 }); + } + else + { + return Ok(value); + } + } + } + + public class PostRedirectGetGetResponse + { + public int TempDataValue { get; set; } + public string CookieValue { get; set; } + } + + public class RedirectHandlerResponse + { + public int Url { get; set; } + public int Body { get; set; } + } + + public class Number + { + public int Value { get; set; } } } diff --git a/test/WebSites/ControllersFromServicesWebSite/Startup.cs b/test/WebSites/ControllersFromServicesWebSite/Startup.cs index fa78d51a20..05653ebde6 100644 --- a/test/WebSites/ControllersFromServicesWebSite/Startup.cs +++ b/test/WebSites/ControllersFromServicesWebSite/Startup.cs @@ -65,15 +65,18 @@ namespace ControllersFromServicesWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/CorsWebSite/Startup.cs b/test/WebSites/CorsWebSite/Startup.cs index 73cbbd4b1e..6ee44e7f39 100644 --- a/test/WebSites/CorsWebSite/Startup.cs +++ b/test/WebSites/CorsWebSite/Startup.cs @@ -79,14 +79,17 @@ namespace CorsWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/ErrorPageMiddlewareWebSite/Startup.cs b/test/WebSites/ErrorPageMiddlewareWebSite/Startup.cs index 6da154499f..864fce79ea 100644 --- a/test/WebSites/ErrorPageMiddlewareWebSite/Startup.cs +++ b/test/WebSites/ErrorPageMiddlewareWebSite/Startup.cs @@ -24,15 +24,18 @@ namespace ErrorPageMiddlewareWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/FilesWebSite/Startup.cs b/test/WebSites/FilesWebSite/Startup.cs index c75c9720f8..aaa363af54 100644 --- a/test/WebSites/FilesWebSite/Startup.cs +++ b/test/WebSites/FilesWebSite/Startup.cs @@ -26,14 +26,17 @@ namespace FilesWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string [] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/FormatterWebSite/Program.cs b/test/WebSites/FormatterWebSite/Program.cs index 15970bd9eb..16c54a70dc 100644 --- a/test/WebSites/FormatterWebSite/Program.cs +++ b/test/WebSites/FormatterWebSite/Program.cs @@ -10,14 +10,17 @@ namespace FormatterWebSite { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/HtmlGenerationWebSite/Startup.cs b/test/WebSites/HtmlGenerationWebSite/Startup.cs index d2ecbea617..a0c93159d6 100644 --- a/test/WebSites/HtmlGenerationWebSite/Startup.cs +++ b/test/WebSites/HtmlGenerationWebSite/Startup.cs @@ -44,14 +44,17 @@ namespace HtmlGenerationWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/RazorBuildWebSite/Startup.cs b/test/WebSites/RazorBuildWebSite/Startup.cs index 4d17168cae..b66bede92b 100644 --- a/test/WebSites/RazorBuildWebSite/Startup.cs +++ b/test/WebSites/RazorBuildWebSite/Startup.cs @@ -22,14 +22,17 @@ namespace RazorBuildWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/RazorPageExecutionInstrumentationWebSite/Startup.cs b/test/WebSites/RazorPageExecutionInstrumentationWebSite/Startup.cs index 120fc741d5..76b1886b01 100644 --- a/test/WebSites/RazorPageExecutionInstrumentationWebSite/Startup.cs +++ b/test/WebSites/RazorPageExecutionInstrumentationWebSite/Startup.cs @@ -44,15 +44,18 @@ namespace RazorPageExecutionInstrumentationWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/RazorPagesWebSite/Program.cs b/test/WebSites/RazorPagesWebSite/Program.cs index 2c88d76f44..b5fdb843f7 100644 --- a/test/WebSites/RazorPagesWebSite/Program.cs +++ b/test/WebSites/RazorPagesWebSite/Program.cs @@ -10,14 +10,17 @@ namespace RazorPagesWebSite { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() - .UseStartup() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseIISIntegration() + .UseStartup(); } } diff --git a/test/WebSites/RazorWebSite/Startup.cs b/test/WebSites/RazorWebSite/Startup.cs index e1d770d61f..17fee9d6d7 100644 --- a/test/WebSites/RazorWebSite/Startup.cs +++ b/test/WebSites/RazorWebSite/Startup.cs @@ -75,15 +75,18 @@ namespace RazorWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/RoutingWebSite/Startup.cs b/test/WebSites/RoutingWebSite/Startup.cs index 2fed21a9b8..2a9f4482dd 100644 --- a/test/WebSites/RoutingWebSite/Startup.cs +++ b/test/WebSites/RoutingWebSite/Startup.cs @@ -45,15 +45,18 @@ namespace RoutingWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/SecurityWebSite/Program.cs b/test/WebSites/SecurityWebSite/Program.cs index e9748d7609..464fbffbea 100644 --- a/test/WebSites/SecurityWebSite/Program.cs +++ b/test/WebSites/SecurityWebSite/Program.cs @@ -10,13 +10,16 @@ namespace SecurityWebSite { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseKestrel() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup(); } } diff --git a/test/WebSites/SimpleWebSite/Startup.cs b/test/WebSites/SimpleWebSite/Startup.cs index 34cb446f8d..bab22ea874 100644 --- a/test/WebSites/SimpleWebSite/Startup.cs +++ b/test/WebSites/SimpleWebSite/Startup.cs @@ -32,15 +32,18 @@ namespace SimpleWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/TagHelpersWebSite/Startup.cs b/test/WebSites/TagHelpersWebSite/Startup.cs index f9200d9ea0..f6c470b7fc 100644 --- a/test/WebSites/TagHelpersWebSite/Startup.cs +++ b/test/WebSites/TagHelpersWebSite/Startup.cs @@ -23,15 +23,18 @@ namespace TagHelpersWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/VersioningWebSite/Startup.cs b/test/WebSites/VersioningWebSite/Startup.cs index 72cfc00409..cdf090a0a6 100644 --- a/test/WebSites/VersioningWebSite/Startup.cs +++ b/test/WebSites/VersioningWebSite/Startup.cs @@ -27,15 +27,18 @@ namespace VersioningWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/WebApiCompatShimWebSite/Startup.cs b/test/WebSites/WebApiCompatShimWebSite/Startup.cs index a02a76c3b0..3699ab9e60 100644 --- a/test/WebSites/WebApiCompatShimWebSite/Startup.cs +++ b/test/WebSites/WebApiCompatShimWebSite/Startup.cs @@ -34,15 +34,18 @@ namespace WebApiCompatShimWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } } diff --git a/test/WebSites/XmlFormattersWebSite/Startup.cs b/test/WebSites/XmlFormattersWebSite/Startup.cs index 5414e57643..31bce95f56 100644 --- a/test/WebSites/XmlFormattersWebSite/Startup.cs +++ b/test/WebSites/XmlFormattersWebSite/Startup.cs @@ -77,15 +77,18 @@ namespace XmlFormattersWebSite public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .UseKestrel() - .UseIISIntegration() + var host = CreateWebHostBuilder(args) .Build(); host.Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseStartup() + .UseKestrel() + .UseIISIntegration(); } }