Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit ebf6424

Browse files
committed
Merge branch 'khellang/kestrel-server-tests' into dev
2 parents dd02a1c + 4cc070f commit ebf6424

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

src/Microsoft.AspNet.Server.Kestrel/KestrelServerInformation.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public class KestrelServerInformation : IKestrelServerInformation, IServerAddres
1414
{
1515
public KestrelServerInformation(IConfiguration configuration)
1616
{
17+
if (configuration == null)
18+
{
19+
throw new ArgumentNullException(nameof(configuration));
20+
}
21+
1722
Addresses = GetAddresses(configuration);
1823
ThreadCount = GetThreadCount(configuration);
1924
NoDelay = GetNoDelay(configuration);

test/Microsoft.AspNet.Server.KestrelTests/KestrelServerInformationTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ namespace Microsoft.AspNet.Server.KestrelTests
1111
{
1212
public class KestrelServerInformationTests
1313
{
14+
[Fact]
15+
public void NullConfigurationThrows()
16+
{
17+
Assert.Throws<ArgumentNullException>(() => new KestrelServerInformation(null));
18+
}
19+
1420
[Fact]
1521
public void SetThreadCountUsingConfiguration()
1622
{
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)