diff --git a/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs b/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs index 3808b6144b..f9f4ae8c8e 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs @@ -14,6 +14,11 @@ namespace Microsoft.AspNet.Server.Kestrel { public KestrelServerInformation(IConfiguration configuration) { + if (configuration == null) + { + throw new ArgumentNullException(nameof(configuration)); + } + Addresses = GetAddresses(configuration); ThreadCount = GetThreadCount(configuration); NoDelay = GetNoDelay(configuration); diff --git a/test/Microsoft.AspNet.Server.KestrelTests/KestrelServerInformationTests.cs b/test/Microsoft.AspNet.Server.KestrelTests/KestrelServerInformationTests.cs index 1c56258554..2513d93e84 100644 --- a/test/Microsoft.AspNet.Server.KestrelTests/KestrelServerInformationTests.cs +++ b/test/Microsoft.AspNet.Server.KestrelTests/KestrelServerInformationTests.cs @@ -11,6 +11,12 @@ namespace Microsoft.AspNet.Server.KestrelTests { public class KestrelServerInformationTests { + [Fact] + public void NullConfigurationThrows() + { + Assert.Throws(() => new KestrelServerInformation(null)); + } + [Fact] public void SetThreadCountUsingConfiguration() { diff --git a/test/Microsoft.AspNet.Server.KestrelTests/KestrelServerTests.cs b/test/Microsoft.AspNet.Server.KestrelTests/KestrelServerTests.cs new file mode 100644 index 0000000000..23e2ff68a7 --- /dev/null +++ b/test/Microsoft.AspNet.Server.KestrelTests/KestrelServerTests.cs @@ -0,0 +1,82 @@ +// 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 Microsoft.AspNet.Hosting.Server; +using Microsoft.AspNet.Http.Features; +using Microsoft.AspNet.Server.Kestrel; +using Microsoft.AspNet.Server.Kestrel.Infrastructure; +using Microsoft.Extensions.Configuration; +using Xunit; + +namespace Microsoft.AspNet.Server.KestrelTests +{ + public class KestrelServerTests + { + [Theory] + [InlineData(0)] + [InlineData(-1337)] + public void StartWithNonPositiveThreadCountThrows(int threadCount) + { + var server = CreateServer(configuration => + new KestrelServerInformation(configuration) + { + ThreadCount = threadCount + }); + + var exception = Assert.Throws(() => StartDummyApplication(server)); + + Assert.Equal("threadCount", exception.ParamName); + } + + [Fact] + public void StartWithInvalidAddressThrows() + { + var server = CreateServer(configuration => + new KestrelServerInformation(configuration) + { + Addresses = {"http:/asdf"} + }); + + var exception = Assert.Throws(() => StartDummyApplication(server)); + + Assert.Contains("Unrecognized listening address", exception.Message); + } + + [Fact] + public void StartWithEmptyAddressesThrows() + { + var server = CreateServer(configuration => + { + var information = new KestrelServerInformation(configuration); + + information.Addresses.Clear(); + + return information; + }); + + var exception = Assert.Throws(() => StartDummyApplication(server)); + + Assert.Equal("No recognized listening addresses were configured.", exception.Message); + } + + private static KestrelServer CreateServer(Func serverInformationFactory) + { + var configuration = new ConfigurationBuilder().Build(); + var information = serverInformationFactory(configuration); + + var features = new FeatureCollection(); + features.Set(information); + + var lifetime = new LifetimeNotImplemented(); + var logger = new TestKestrelTrace.TestLogger(); + + return new KestrelServer(features, lifetime, logger); + } + + private static void StartDummyApplication(IServer server) + { + server.Start(new DummyApplication(context => TaskUtilities.CompletedTask)); + } + } +}