From ad208442d8c27f3c25481fc553603398dd5706ef Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 8 Oct 2014 12:08:55 -0700 Subject: [PATCH] Updating functional tests to restore CallContextServiceLocator.Locator.Service on test finish --- .../CustomUrlHelperTests.cs | 69 +++++----- .../MvcSampleTests.cs | 121 ++++++++++-------- .../TestHelper.cs | 33 ++++- test/WebSites/UrlHelperWebSite/Config.json | 5 - test/WebSites/UrlHelperWebSite/Startup.cs | 7 +- 5 files changed, 135 insertions(+), 100 deletions(-) delete mode 100644 test/WebSites/UrlHelperWebSite/Config.json diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/CustomUrlHelperTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/CustomUrlHelperTests.cs index 6d0c4fefaa..b94d625bda 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/CustomUrlHelperTests.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/CustomUrlHelperTests.cs @@ -1,8 +1,5 @@ using System; -using System.IO; using System.Net; -using System.Net.Http; -using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.TestHost; @@ -26,35 +23,39 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests [Fact] public async Task CustomUrlHelper_GeneratesUrlFromController() { - // Arrange - var server = TestServer.Create(_services, _app); - var client = server.CreateClient(); + using (TestHelper.ReplaceCallContextServiceLocationService(_services)) + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); - // Act - var response = await client.GetAsync("http://localhost/Home/UrlContent"); + // Act + var response = await client.GetAsync("http://localhost/Home/UrlContent"); + var responseData = await response.Content.ReadAsStringAsync(); - string responseData = await response.Content.ReadAsStringAsync(); - - //Assert - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.Equal(_cdnServerBaseUrl + "/bootstrap.min.css", responseData); + //Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(_cdnServerBaseUrl + "/bootstrap.min.css", responseData); + } } [Fact] public async Task CustomUrlHelper_GeneratesUrlFromView() { - // Arrange - var server = TestServer.Create(_services, _app); - var client = server.CreateClient(); + using (TestHelper.ReplaceCallContextServiceLocationService(_services)) + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); - // Act - var response = await client.GetAsync("http://localhost/Home/Index"); + // Act + var response = await client.GetAsync("http://localhost/Home/Index"); + var responseData = await response.Content.ReadAsStringAsync(); - string responseData = await response.Content.ReadAsStringAsync(); - - //Assert - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.Contains(_cdnServerBaseUrl + "/bootstrap.min.css", responseData); + //Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Contains(_cdnServerBaseUrl + "/bootstrap.min.css", responseData); + } } [Theory] @@ -62,18 +63,20 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests [InlineData("http://localhost/Home/LinkByUrlAction", "/home/urlcontent")] public async Task LowercaseUrls_LinkGeneration(string url, string expectedLink) { - // Arrange - var server = TestServer.Create(_services, _app); - var client = server.CreateClient(); + using (TestHelper.ReplaceCallContextServiceLocationService(_services)) + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); - // Act - var response = await client.GetAsync(url); + // Act + var response = await client.GetAsync(url); + var responseData = await response.Content.ReadAsStringAsync(); - string responseData = await response.Content.ReadAsStringAsync(); - - //Assert - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.Equal(expectedLink, responseData, ignoreCase: false); + //Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedLink, responseData, ignoreCase: false); + } } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcSampleTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcSampleTests.cs index b31ba85ee1..01176a3865 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcSampleTests.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcSampleTests.cs @@ -6,7 +6,6 @@ using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; -using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.TestHost; @@ -24,56 +23,65 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests [Fact] public async Task Home_Index_ReturnsSuccess() { - // Arrange - var server = TestServer.Create(_services, _app); - var client = server.CreateClient(); + using (TestHelper.ReplaceCallContextServiceLocationService(_services)) + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); - // Act - var response = await client.GetAsync("http://localhost/Home/Index"); + // Act + var response = await client.GetAsync("http://localhost/Home/Index"); - // Assert - Assert.NotNull(response); - Assert.Equal(HttpStatusCode.OK, response.StatusCode); + // Assert + Assert.NotNull(response); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } } [Fact] public async Task Home_NotFoundAction_Returns404() { - // Arrange - var server = TestServer.Create(_services, _app); - var client = server.CreateClient(); + using (TestHelper.ReplaceCallContextServiceLocationService(_services)) + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); - // Act - var response = await client.GetAsync("http://localhost/Home/NotFound"); + // Act + var response = await client.GetAsync("http://localhost/Home/NotFound"); - // Assert - Assert.NotNull(response); - Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + // Assert + Assert.NotNull(response); + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + } } [Fact] public async Task Home_CreateUser_ReturnsXmlBasedOnAcceptHeader() { - // Arrange - var server = TestServer.Create(_services, _app); - var client = server.CreateClient(); - var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Home/ReturnUser"); - request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8")); - - // Act - var response = await client.SendAsync(request); + using (TestHelper.ReplaceCallContextServiceLocationService(_services)) + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); + var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Home/ReturnUser"); + request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8")); - // Assert - Assert.NotNull(response); - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.Equal("I like playing Football" + - "
My address
13true" + - "
Dependents address
0false" + - "0Dependents name" + - "13.37" + - "My nameSecure stringSoftware Engineer
", - new StreamReader(await response.Content.ReadAsStreamAsync(), Encoding.UTF8).ReadToEnd()); + // Act + var response = await client.SendAsync(request); + + // Assert + Assert.NotNull(response); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal("I like playing Football" + + "
My address
13true" + + "
Dependents address
0false" + + "0Dependents name" + + "13.37" + + "My nameSecure stringSoftware Engineer
", + await response.Content.ReadAsStringAsync()); + } } [Theory] @@ -82,33 +90,38 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests [InlineData("http://localhost/Filters/NotGrantedClaim", HttpStatusCode.Unauthorized)] public async Task FiltersController_Tests(string url, HttpStatusCode statusCode) { - // Arrange - var server = TestServer.Create(_services, _app); - var client = server.CreateClient(); + using (TestHelper.ReplaceCallContextServiceLocationService(_services)) + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); - // Act - var response = await client.GetAsync(url); + // Act + var response = await client.GetAsync(url); - // Assert - Assert.NotNull(response); - Assert.Equal(statusCode, response.StatusCode); + // Assert + Assert.NotNull(response); + Assert.Equal(statusCode, response.StatusCode); + } } [Fact] public async Task FiltersController_Crash_ThrowsException() { - // Arrange - var server = TestServer.Create(_services, _app); - var client = server.CreateClient(); + using (TestHelper.ReplaceCallContextServiceLocationService(_services)) + { + // Arrange + var server = TestServer.Create(_services, _app); + var client = server.CreateClient(); - // Act - var response = await client.GetAsync("http://localhost/Filters/Crash?message=HelloWorld"); + // Act + var response = await client.GetAsync("http://localhost/Filters/Crash?message=HelloWorld"); - // Assert - Assert.NotNull(response); - Assert.Equal(HttpStatusCode.OK, response.StatusCode); - Assert.Equal("Boom HelloWorld", - new StreamReader(await response.Content.ReadAsStreamAsync(), Encoding.UTF8).ReadToEnd()); + // Assert + Assert.NotNull(response); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal("Boom HelloWorld", await response.Content.ReadAsStringAsync()); + } } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs index ec59194d90..eb23816a37 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/TestHelper.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests public static class TestHelper { // Path from Mvc\\test\\Microsoft.AspNet.Mvc.FunctionalTests - private static readonly string WebsitesDirectoryPath = Path.Combine("..", "websites"); + private static readonly string WebsitesDirectoryPath = Path.Combine("..", "WebSites"); public static IServiceProvider CreateServices(string applicationWebSiteName) { @@ -58,9 +58,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests typeof(ILoggerFactory), NullLoggerFactory.Instance); - var tempServiceProvider = services.BuildServiceProvider(originalProvider); - CallContextServiceLocator.Locator.ServiceProvider = tempServiceProvider; - return tempServiceProvider; + return services.BuildServiceProvider(originalProvider); } // Calculate the path relative to the application base path. @@ -72,6 +70,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests Path.Combine(appEnvironment.ApplicationBasePath, websitePath, applicationWebSiteName)); } + /// + /// Creates a disposable action that replaces the service provider + /// with the passed in service that is switched back on . + /// + /// This is required for config since it uses the static property to get to + /// . + public static IDisposable ReplaceCallContextServiceLocationService(IServiceProvider serviceProvider) + { + return new CallContextProviderAction(serviceProvider); + } + private static Type CreateAssemblyProviderType(string siteName) { // Creates a service type that will limit MVC to only the controllers in the test site. @@ -81,5 +90,21 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests var providerType = typeof(TestAssemblyProvider<>).MakeGenericType(assembly.GetExportedTypes()[0]); return providerType; } + + private sealed class CallContextProviderAction : IDisposable + { + private readonly IServiceProvider _originalProvider; + + public CallContextProviderAction(IServiceProvider provider) + { + _originalProvider = CallContextServiceLocator.Locator.ServiceProvider; + CallContextServiceLocator.Locator.ServiceProvider = provider; + } + + public void Dispose() + { + CallContextServiceLocator.Locator.ServiceProvider = _originalProvider; + } + } } } \ No newline at end of file diff --git a/test/WebSites/UrlHelperWebSite/Config.json b/test/WebSites/UrlHelperWebSite/Config.json deleted file mode 100644 index 8b9227c91e..0000000000 --- a/test/WebSites/UrlHelperWebSite/Config.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "ServeCDNContent": "true", - "CDNServerBaseUrl" : "http://cdn.contoso.com", - "GenerateLowercaseUrls": "true" -} \ No newline at end of file diff --git a/test/WebSites/UrlHelperWebSite/Startup.cs b/test/WebSites/UrlHelperWebSite/Startup.cs index 79c1565342..74843c00de 100644 --- a/test/WebSites/UrlHelperWebSite/Startup.cs +++ b/test/WebSites/UrlHelperWebSite/Startup.cs @@ -12,16 +12,15 @@ namespace UrlHelperWebSite public void Configure(IApplicationBuilder app) { var configuration = app.GetTestConfiguration(); - configuration.AddJsonFile("config.json"); // Set up application services app.UsePerRequestServices(services => { services.ConfigureOptions(optionsSetup => { - optionsSetup.ServeCDNContent = Convert.ToBoolean(configuration.Get("ServeCDNContent")); - optionsSetup.CDNServerBaseUrl = configuration.Get("CDNServerBaseUrl"); - optionsSetup.GenerateLowercaseUrls = Convert.ToBoolean(configuration.Get("GenerateLowercaseUrls")); + optionsSetup.ServeCDNContent = true; + optionsSetup.CDNServerBaseUrl = "http://cdn.contoso.com"; + optionsSetup.GenerateLowercaseUrls = true; }); // Add MVC services to the services container