TestServer should not capture startup errors by default

This commit is contained in:
John Luo 2016-01-13 10:59:29 -08:00
parent f7be1fb80e
commit c747ce630d
4 changed files with 45 additions and 2 deletions

View File

@ -2,6 +2,7 @@
// 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 Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting.Server;
using Microsoft.Extensions.Configuration;
@ -13,6 +14,8 @@ namespace Microsoft.AspNet.Hosting
{
IWebApplication Build();
IDictionary<string, string> Settings { get; }
IWebApplicationBuilder UseConfiguration(IConfiguration configuration);
IWebApplicationBuilder UseServer(IServerFactory factory);

View File

@ -17,7 +17,6 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNet.Hosting
{
@ -38,7 +37,7 @@ namespace Microsoft.AspNet.Hosting
// Only one of these should be set
private IServerFactory _serverFactory;
private Dictionary<string, string> _settings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private IDictionary<string, string> _settings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
public WebApplicationBuilder()
{
@ -46,6 +45,14 @@ namespace Microsoft.AspNet.Hosting
_loggerFactory = new LoggerFactory();
}
public IDictionary<string, string> Settings
{
get
{
return _settings;
}
}
public IWebApplicationBuilder UseSetting(string key, string value)
{
_settings[key] = value;

View File

@ -22,6 +22,11 @@ namespace Microsoft.AspNet.TestHost
public TestServer(IWebApplicationBuilder builder)
{
if (!builder.Settings.ContainsKey(WebApplicationDefaults.CaptureStartupErrorsKey))
{
builder.UseCaptureStartupErrors(false);
}
var application = builder.UseServer(this).Build();
application.Start();
_appInstance = application;

View File

@ -30,6 +30,34 @@ namespace Microsoft.AspNet.TestHost
new TestServer(new WebApplicationBuilder().Configure(app => { }));
}
[Fact]
public void DoesNotCaptureStartupErrorsByDefault()
{
var builder = new WebApplicationBuilder()
.Configure(app =>
{
throw new InvalidOperationException();
});
Assert.Throws<InvalidOperationException>(() => new TestServer(builder));
}
[Fact]
public void CaptureStartupErrorsSettingPreserved()
{
var builder = new WebApplicationBuilder()
.UseCaptureStartupErrors(true)
.Configure(app =>
{
throw new InvalidOperationException();
});
// Does not throw
new TestServer(builder);
}
[Fact]
public void ApplicationServicesAvailableFromTestServer()
{