|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using Microsoft.AspNet.Hosting.Server; |
| 6 | +using Microsoft.AspNet.Http.Features; |
| 7 | +using Microsoft.AspNet.Server.Kestrel; |
| 8 | +using Microsoft.AspNet.Server.Kestrel.Infrastructure; |
| 9 | +using Microsoft.Extensions.Configuration; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Microsoft.AspNet.Server.KestrelTests |
| 13 | +{ |
| 14 | + public class KestrelServerTests |
| 15 | + { |
| 16 | + [Theory] |
| 17 | + [InlineData(0)] |
| 18 | + [InlineData(-1337)] |
| 19 | + public void StartWithNonPositiveThreadCountThrows(int threadCount) |
| 20 | + { |
| 21 | + var server = CreateServer(configuration => |
| 22 | + new KestrelServerInformation(configuration) |
| 23 | + { |
| 24 | + ThreadCount = threadCount |
| 25 | + }); |
| 26 | + |
| 27 | + var exception = Assert.Throws<ArgumentOutOfRangeException>(() => StartDummyApplication(server)); |
| 28 | + |
| 29 | + Assert.Equal("threadCount", exception.ParamName); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void StartWithInvalidAddressThrows() |
| 34 | + { |
| 35 | + var server = CreateServer(configuration => |
| 36 | + new KestrelServerInformation(configuration) |
| 37 | + { |
| 38 | + Addresses = {"http:/asdf"} |
| 39 | + }); |
| 40 | + |
| 41 | + var exception = Assert.Throws<FormatException>(() => StartDummyApplication(server)); |
| 42 | + |
| 43 | + Assert.Contains("Unrecognized listening address", exception.Message); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void StartWithEmptyAddressesThrows() |
| 48 | + { |
| 49 | + var server = CreateServer(configuration => |
| 50 | + { |
| 51 | + var information = new KestrelServerInformation(configuration); |
| 52 | + |
| 53 | + information.Addresses.Clear(); |
| 54 | + |
| 55 | + return information; |
| 56 | + }); |
| 57 | + |
| 58 | + var exception = Assert.Throws<InvalidOperationException>(() => StartDummyApplication(server)); |
| 59 | + |
| 60 | + Assert.Equal("No recognized listening addresses were configured.", exception.Message); |
| 61 | + } |
| 62 | + |
| 63 | + private static KestrelServer CreateServer(Func<IConfiguration, IKestrelServerInformation> serverInformationFactory) |
| 64 | + { |
| 65 | + var configuration = new ConfigurationBuilder().Build(); |
| 66 | + var information = serverInformationFactory(configuration); |
| 67 | + |
| 68 | + var features = new FeatureCollection(); |
| 69 | + features.Set(information); |
| 70 | + |
| 71 | + var lifetime = new LifetimeNotImplemented(); |
| 72 | + var logger = new TestKestrelTrace.TestLogger(); |
| 73 | + |
| 74 | + return new KestrelServer(features, lifetime, logger); |
| 75 | + } |
| 76 | + |
| 77 | + private static void StartDummyApplication(IServer server) |
| 78 | + { |
| 79 | + server.Start(new DummyApplication(context => TaskUtilities.CompletedTask)); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments