зеркало из https://github.com/aspnet/Mvc.git
Updating functional tests to restore CallContextServiceLocator.Locator.Service on test finish
This commit is contained in:
Родитель
b19764d922
Коммит
ad208442d8
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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("<User xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=" +
|
||||
"\"http://schemas.datacontract.org/2004/07/MvcSample.Web.Models\"><About>I like playing Football" +
|
||||
"</About><Address>My address</Address><Age>13</Age><Alive>true</Alive><Dependent><About i:nil=\"true\" />" +
|
||||
"<Address>Dependents address</Address><Age>0</Age><Alive>false</Alive><Dependent 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\" />" +
|
||||
"<Name>My name</Name><Password>Secure string</Password><Profession>Software Engineer</Profession></User>",
|
||||
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("<User xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=" +
|
||||
"\"http://schemas.datacontract.org/2004/07/MvcSample.Web.Models\"><About>I like playing Football" +
|
||||
"</About><Address>My address</Address><Age>13</Age><Alive>true</Alive><Dependent><About i:nil=\"true\" />" +
|
||||
"<Address>Dependents address</Address><Age>0</Age><Alive>false</Alive><Dependent 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\" />" +
|
||||
"<Name>My name</Name><Password>Secure string</Password><Profession>Software Engineer</Profession></User>",
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
{
|
||||
"ServeCDNContent": "true",
|
||||
"CDNServerBaseUrl" : "http://cdn.contoso.com",
|
||||
"GenerateLowercaseUrls": "true"
|
||||
}
|
|
@ -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<AppOptions>(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
|
||||
|
|
Загрузка…
Ссылка в новой задаче