Updating functional tests to restore CallContextServiceLocator.Locator.Service on test finish
This commit is contained in:
parent
b19764d922
commit
ad208442d8
|
|
@ -1,8 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.TestHost;
|
using Microsoft.AspNet.TestHost;
|
||||||
|
|
@ -25,6 +22,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task CustomUrlHelper_GeneratesUrlFromController()
|
public async Task CustomUrlHelper_GeneratesUrlFromController()
|
||||||
|
{
|
||||||
|
using (TestHelper.ReplaceCallContextServiceLocationService(_services))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_services, _app);
|
var server = TestServer.Create(_services, _app);
|
||||||
|
|
@ -32,16 +31,18 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await client.GetAsync("http://localhost/Home/UrlContent");
|
var response = await client.GetAsync("http://localhost/Home/UrlContent");
|
||||||
|
var responseData = await response.Content.ReadAsStringAsync();
|
||||||
string responseData = await response.Content.ReadAsStringAsync();
|
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
Assert.Equal(_cdnServerBaseUrl + "/bootstrap.min.css", responseData);
|
Assert.Equal(_cdnServerBaseUrl + "/bootstrap.min.css", responseData);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task CustomUrlHelper_GeneratesUrlFromView()
|
public async Task CustomUrlHelper_GeneratesUrlFromView()
|
||||||
|
{
|
||||||
|
using (TestHelper.ReplaceCallContextServiceLocationService(_services))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_services, _app);
|
var server = TestServer.Create(_services, _app);
|
||||||
|
|
@ -49,18 +50,20 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await client.GetAsync("http://localhost/Home/Index");
|
var response = await client.GetAsync("http://localhost/Home/Index");
|
||||||
|
var responseData = await response.Content.ReadAsStringAsync();
|
||||||
string responseData = await response.Content.ReadAsStringAsync();
|
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
Assert.Contains(_cdnServerBaseUrl + "/bootstrap.min.css", responseData);
|
Assert.Contains(_cdnServerBaseUrl + "/bootstrap.min.css", responseData);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("http://localhost/Home/LinkByUrlRouteUrl", "/api/simplepoco/10")]
|
[InlineData("http://localhost/Home/LinkByUrlRouteUrl", "/api/simplepoco/10")]
|
||||||
[InlineData("http://localhost/Home/LinkByUrlAction", "/home/urlcontent")]
|
[InlineData("http://localhost/Home/LinkByUrlAction", "/home/urlcontent")]
|
||||||
public async Task LowercaseUrls_LinkGeneration(string url, string expectedLink)
|
public async Task LowercaseUrls_LinkGeneration(string url, string expectedLink)
|
||||||
|
{
|
||||||
|
using (TestHelper.ReplaceCallContextServiceLocationService(_services))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_services, _app);
|
var server = TestServer.Create(_services, _app);
|
||||||
|
|
@ -68,8 +71,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await client.GetAsync(url);
|
var response = await client.GetAsync(url);
|
||||||
|
var responseData = await response.Content.ReadAsStringAsync();
|
||||||
string responseData = await response.Content.ReadAsStringAsync();
|
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
@ -77,3 +79,4 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,6 @@ using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.TestHost;
|
using Microsoft.AspNet.TestHost;
|
||||||
|
|
@ -23,6 +22,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Home_Index_ReturnsSuccess()
|
public async Task Home_Index_ReturnsSuccess()
|
||||||
|
{
|
||||||
|
using (TestHelper.ReplaceCallContextServiceLocationService(_services))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_services, _app);
|
var server = TestServer.Create(_services, _app);
|
||||||
|
|
@ -35,9 +36,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
Assert.NotNull(response);
|
Assert.NotNull(response);
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Home_NotFoundAction_Returns404()
|
public async Task Home_NotFoundAction_Returns404()
|
||||||
|
{
|
||||||
|
using (TestHelper.ReplaceCallContextServiceLocationService(_services))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_services, _app);
|
var server = TestServer.Create(_services, _app);
|
||||||
|
|
@ -50,9 +54,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
Assert.NotNull(response);
|
Assert.NotNull(response);
|
||||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Home_CreateUser_ReturnsXmlBasedOnAcceptHeader()
|
public async Task Home_CreateUser_ReturnsXmlBasedOnAcceptHeader()
|
||||||
|
{
|
||||||
|
using (TestHelper.ReplaceCallContextServiceLocationService(_services))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_services, _app);
|
var server = TestServer.Create(_services, _app);
|
||||||
|
|
@ -73,7 +80,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
"<GPA>0</GPA><Log i:nil=\"true\" /><Name>Dependents name</Name><Password i:nil=\"true\" />" +
|
"<GPA>0</GPA><Log i:nil=\"true\" /><Name>Dependents name</Name><Password i:nil=\"true\" />" +
|
||||||
"<Profession i:nil=\"true\" /></Dependent><GPA>13.37</GPA><Log i:nil=\"true\" />" +
|
"<Profession i:nil=\"true\" /></Dependent><GPA>13.37</GPA><Log i:nil=\"true\" />" +
|
||||||
"<Name>My name</Name><Password>Secure string</Password><Profession>Software Engineer</Profession></User>",
|
"<Name>My name</Name><Password>Secure string</Password><Profession>Software Engineer</Profession></User>",
|
||||||
new StreamReader(await response.Content.ReadAsStreamAsync(), Encoding.UTF8).ReadToEnd());
|
await response.Content.ReadAsStringAsync());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -81,6 +89,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
[InlineData("http://localhost/Filters/AllGranted", HttpStatusCode.Unauthorized)]
|
[InlineData("http://localhost/Filters/AllGranted", HttpStatusCode.Unauthorized)]
|
||||||
[InlineData("http://localhost/Filters/NotGrantedClaim", HttpStatusCode.Unauthorized)]
|
[InlineData("http://localhost/Filters/NotGrantedClaim", HttpStatusCode.Unauthorized)]
|
||||||
public async Task FiltersController_Tests(string url, HttpStatusCode statusCode)
|
public async Task FiltersController_Tests(string url, HttpStatusCode statusCode)
|
||||||
|
{
|
||||||
|
using (TestHelper.ReplaceCallContextServiceLocationService(_services))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_services, _app);
|
var server = TestServer.Create(_services, _app);
|
||||||
|
|
@ -93,9 +103,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
Assert.NotNull(response);
|
Assert.NotNull(response);
|
||||||
Assert.Equal(statusCode, response.StatusCode);
|
Assert.Equal(statusCode, response.StatusCode);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task FiltersController_Crash_ThrowsException()
|
public async Task FiltersController_Crash_ThrowsException()
|
||||||
|
{
|
||||||
|
using (TestHelper.ReplaceCallContextServiceLocationService(_services))
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_services, _app);
|
var server = TestServer.Create(_services, _app);
|
||||||
|
|
@ -107,8 +120,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
// Assert
|
// Assert
|
||||||
Assert.NotNull(response);
|
Assert.NotNull(response);
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
Assert.Equal("Boom HelloWorld",
|
Assert.Equal("Boom HelloWorld", await response.Content.ReadAsStringAsync());
|
||||||
new StreamReader(await response.Content.ReadAsStreamAsync(), Encoding.UTF8).ReadToEnd());
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -15,7 +15,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
public static class TestHelper
|
public static class TestHelper
|
||||||
{
|
{
|
||||||
// Path from Mvc\\test\\Microsoft.AspNet.Mvc.FunctionalTests
|
// 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)
|
public static IServiceProvider CreateServices(string applicationWebSiteName)
|
||||||
{
|
{
|
||||||
|
|
@ -58,9 +58,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
typeof(ILoggerFactory),
|
typeof(ILoggerFactory),
|
||||||
NullLoggerFactory.Instance);
|
NullLoggerFactory.Instance);
|
||||||
|
|
||||||
var tempServiceProvider = services.BuildServiceProvider(originalProvider);
|
return services.BuildServiceProvider(originalProvider);
|
||||||
CallContextServiceLocator.Locator.ServiceProvider = tempServiceProvider;
|
|
||||||
return tempServiceProvider;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the path relative to the application base path.
|
// Calculate the path relative to the application base path.
|
||||||
|
|
@ -72,6 +70,17 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
Path.Combine(appEnvironment.ApplicationBasePath, websitePath, applicationWebSiteName));
|
Path.Combine(appEnvironment.ApplicationBasePath, websitePath, applicationWebSiteName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a disposable action that replaces the service provider <see cref="CallContextServiceLocator.Locator"/>
|
||||||
|
/// with the passed in service that is switched back on <see cref="IDisposable.Dispose"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>This is required for config since it uses the static property to get to
|
||||||
|
/// <see cref="IApplicationEnvironment"/>.</remarks>
|
||||||
|
public static IDisposable ReplaceCallContextServiceLocationService(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
return new CallContextProviderAction(serviceProvider);
|
||||||
|
}
|
||||||
|
|
||||||
private static Type CreateAssemblyProviderType(string siteName)
|
private static Type CreateAssemblyProviderType(string siteName)
|
||||||
{
|
{
|
||||||
// Creates a service type that will limit MVC to only the controllers in the test site.
|
// 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]);
|
var providerType = typeof(TestAssemblyProvider<>).MakeGenericType(assembly.GetExportedTypes()[0]);
|
||||||
return providerType;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
{
|
|
||||||
"ServeCDNContent": "true",
|
|
||||||
"CDNServerBaseUrl" : "http://cdn.contoso.com",
|
|
||||||
"GenerateLowercaseUrls": "true"
|
|
||||||
}
|
|
||||||
|
|
@ -12,16 +12,15 @@ namespace UrlHelperWebSite
|
||||||
public void Configure(IApplicationBuilder app)
|
public void Configure(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
var configuration = app.GetTestConfiguration();
|
var configuration = app.GetTestConfiguration();
|
||||||
configuration.AddJsonFile("config.json");
|
|
||||||
|
|
||||||
// Set up application services
|
// Set up application services
|
||||||
app.UsePerRequestServices(services =>
|
app.UsePerRequestServices(services =>
|
||||||
{
|
{
|
||||||
services.ConfigureOptions<AppOptions>(optionsSetup =>
|
services.ConfigureOptions<AppOptions>(optionsSetup =>
|
||||||
{
|
{
|
||||||
optionsSetup.ServeCDNContent = Convert.ToBoolean(configuration.Get("ServeCDNContent"));
|
optionsSetup.ServeCDNContent = true;
|
||||||
optionsSetup.CDNServerBaseUrl = configuration.Get("CDNServerBaseUrl");
|
optionsSetup.CDNServerBaseUrl = "http://cdn.contoso.com";
|
||||||
optionsSetup.GenerateLowercaseUrls = Convert.ToBoolean(configuration.Get("GenerateLowercaseUrls"));
|
optionsSetup.GenerateLowercaseUrls = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add MVC services to the services container
|
// Add MVC services to the services container
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue