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

Commit dc7515b

Browse files
committed
More tests
1 parent c015a72 commit dc7515b

File tree

5 files changed

+257
-22
lines changed

5 files changed

+257
-22
lines changed

src/Kestrel.Core/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Runtime.CompilerServices;
55

66
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.Kestrel, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
7+
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.Kestrel.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
78
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.FunctionalTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
89
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.FunctionalTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
910
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.Kestrel.Core.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]

test/Kestrel.FunctionalTests/HttpsTests.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.IO;
76
using System.Net;
87
using System.Net.Security;
98
using System.Net.Sockets;
@@ -15,8 +14,10 @@
1514
using Microsoft.AspNetCore.Hosting;
1615
using Microsoft.AspNetCore.Http;
1716
using Microsoft.AspNetCore.Server.Kestrel.Core;
17+
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
1818
using Microsoft.AspNetCore.Server.Kestrel.Https;
1919
using Microsoft.AspNetCore.Server.Kestrel.Https.Internal;
20+
using Microsoft.AspNetCore.Server.Kestrel.Internal;
2021
using Microsoft.AspNetCore.Testing;
2122
using Microsoft.Extensions.DependencyInjection;
2223
using Microsoft.Extensions.Logging;
@@ -30,7 +31,10 @@ public class HttpsTests
3031
private KestrelServerOptions CreateServerOptions()
3132
{
3233
var serverOptions = new KestrelServerOptions();
33-
serverOptions.ApplicationServices = new ServiceCollection().AddLogging().BuildServiceProvider();
34+
serverOptions.ApplicationServices = new ServiceCollection()
35+
.AddLogging()
36+
.AddSingleton<IDefaultHttpsProvider, DefaultHttpsProvider>()
37+
.BuildServiceProvider();
3438
return serverOptions;
3539
}
3640

@@ -40,7 +44,13 @@ public void UseHttpsDefaultsToDefaultCert()
4044
var serverOptions = CreateServerOptions();
4145
var defaultCert = new X509Certificate2(TestResources.TestCertificatePath, "testPassword");
4246
serverOptions.OverrideDefaultCertificate(defaultCert);
47+
4348
serverOptions.ListenLocalhost(5000, options =>
49+
{
50+
options.UseHttps();
51+
});
52+
53+
serverOptions.ListenLocalhost(5001, options =>
4454
{
4555
options.UseHttps(opt =>
4656
{

test/Kestrel.Tests/ConfigurationReaderTests.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void ReadCertificatesWhenEmptyCertificatsSection_ReturnsEmptyCollection()
2727
{
2828
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
2929
{
30-
new KeyValuePair<string, string>( "Certificates", "" ),
30+
new KeyValuePair<string, string>("Certificates", ""),
3131
}).Build();
3232
var reader = new ConfigurationReader(config);
3333
var certificates = reader.Certificates;
@@ -40,12 +40,12 @@ public void ReadCertificatsSection_ReturnsCollection()
4040
{
4141
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
4242
{
43-
new KeyValuePair<string, string>( "Certificates:FileCert:Path", "/path/cert.pfx" ),
44-
new KeyValuePair<string, string>( "Certificates:FileCert:Password", "certpassword" ),
45-
new KeyValuePair<string, string>( "Certificates:StoreCert:Subject", "certsubject" ),
46-
new KeyValuePair<string, string>( "Certificates:StoreCert:Store", "certstore" ),
47-
new KeyValuePair<string, string>( "Certificates:StoreCert:Location", "cetlocation" ),
48-
new KeyValuePair<string, string>( "Certificates:StoreCert:AllowInvalid", "true" ),
43+
new KeyValuePair<string, string>("Certificates:FileCert:Path", "/path/cert.pfx"),
44+
new KeyValuePair<string, string>("Certificates:FileCert:Password", "certpassword"),
45+
new KeyValuePair<string, string>("Certificates:StoreCert:Subject", "certsubject"),
46+
new KeyValuePair<string, string>("Certificates:StoreCert:Store", "certstore"),
47+
new KeyValuePair<string, string>("Certificates:StoreCert:Location", "cetlocation"),
48+
new KeyValuePair<string, string>("Certificates:StoreCert:AllowInvalid", "true"),
4949
}).Build();
5050
var reader = new ConfigurationReader(config);
5151
var certificates = reader.Certificates;
@@ -82,7 +82,7 @@ public void ReadEndpointsWhenEmptyEndpointsSection_ReturnsEmptyCollection()
8282
{
8383
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
8484
{
85-
new KeyValuePair<string, string>( "Endpoints", "" ),
85+
new KeyValuePair<string, string>("Endpoints", ""),
8686
}).Build();
8787
var reader = new ConfigurationReader(config);
8888
var endpoints = reader.Endpoints;
@@ -95,7 +95,7 @@ public void ReadEndpointWithMissingUrl_Throws()
9595
{
9696
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
9797
{
98-
new KeyValuePair<string, string>( "Endpoints:End1", "" ),
98+
new KeyValuePair<string, string>("Endpoints:End1", ""),
9999
}).Build();
100100
var reader = new ConfigurationReader(config);
101101
Assert.Throws<InvalidOperationException>(() => reader.Endpoints);
@@ -106,7 +106,7 @@ public void ReadEndpointWithEmptyUrl_Throws()
106106
{
107107
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
108108
{
109-
new KeyValuePair<string, string>( "Endpoints:End1:Url", "" ),
109+
new KeyValuePair<string, string>("Endpoints:End1:Url", ""),
110110
}).Build();
111111
var reader = new ConfigurationReader(config);
112112
Assert.Throws<InvalidOperationException>(() => reader.Endpoints);
@@ -117,16 +117,16 @@ public void ReadEndpointsSection_ReturnsCollection()
117117
{
118118
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
119119
{
120-
new KeyValuePair<string, string>( "Endpoints:End1:Url", "http://*:5001" ),
121-
new KeyValuePair<string, string>( "Endpoints:End2:Url", "https://*:5002" ),
122-
new KeyValuePair<string, string>( "Endpoints:End3:Url", "https://*:5003" ),
123-
new KeyValuePair<string, string>( "Endpoints:End3:Certificate:Path", "/path/cert.pfx" ),
124-
new KeyValuePair<string, string>( "Endpoints:End3:Certificate:Password", "certpassword" ),
125-
new KeyValuePair<string, string>( "Endpoints:End4:Url", "https://*:5004" ),
126-
new KeyValuePair<string, string>( "Endpoints:End4:Certificate:Subject", "certsubject" ),
127-
new KeyValuePair<string, string>( "Endpoints:End4:Certificate:Store", "certstore" ),
128-
new KeyValuePair<string, string>( "Endpoints:End4:Certificate:Location", "cetlocation" ),
129-
new KeyValuePair<string, string>( "Endpoints:End4:Certificate:AllowInvalid", "true" ),
120+
new KeyValuePair<string, string>("Endpoints:End1:Url", "http://*:5001"),
121+
new KeyValuePair<string, string>("Endpoints:End2:Url", "https://*:5002"),
122+
new KeyValuePair<string, string>("Endpoints:End3:Url", "https://*:5003"),
123+
new KeyValuePair<string, string>("Endpoints:End3:Certificate:Path", "/path/cert.pfx"),
124+
new KeyValuePair<string, string>("Endpoints:End3:Certificate:Password", "certpassword"),
125+
new KeyValuePair<string, string>("Endpoints:End4:Url", "https://*:5004"),
126+
new KeyValuePair<string, string>("Endpoints:End4:Certificate:Subject", "certsubject"),
127+
new KeyValuePair<string, string>("Endpoints:End4:Certificate:Store", "certstore"),
128+
new KeyValuePair<string, string>("Endpoints:End4:Certificate:Location", "cetlocation"),
129+
new KeyValuePair<string, string>("Endpoints:End4:Certificate:AllowInvalid", "true"),
130130
}).Build();
131131
var reader = new ConfigurationReader(config);
132132
var endpoints = reader.Endpoints;

test/Kestrel.Tests/Kestrel.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<Compile Include="..\shared\**\*.cs" />
11+
<Content Include="..\shared\TestCertificates\*.pfx" CopyToOutputDirectory="PreserveNewest" />
12+
</ItemGroup>
13+
914
<ItemGroup>
1015
<ProjectReference Include="..\..\src\Kestrel\Kestrel.csproj" />
16+
<PackageReference Include="Microsoft.AspNetCore.Testing" Version="$(MicrosoftAspNetCoreTestingPackageVersion)" />
1117
</ItemGroup>
1218

1319
</Project>
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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 System.Collections.Generic;
6+
using System.Linq;
7+
using System.Security.Cryptography.X509Certificates;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.AspNetCore.Server.Kestrel.Core;
10+
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
11+
using Microsoft.AspNetCore.Server.Kestrel.Https;
12+
using Microsoft.AspNetCore.Server.Kestrel.Internal;
13+
using Microsoft.AspNetCore.Testing;
14+
using Microsoft.Extensions.Configuration;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Xunit;
17+
18+
namespace Microsoft.AspNetCore.Server.Kestrel.Tests
19+
{
20+
public class KestrelConfigurationBuilderTests
21+
{
22+
private KestrelServerOptions CreateServerOptions()
23+
{
24+
var serverOptions = new KestrelServerOptions();
25+
serverOptions.ApplicationServices = new ServiceCollection()
26+
.AddLogging()
27+
.AddSingleton<IDefaultHttpsProvider, DefaultHttpsProvider>()
28+
.BuildServiceProvider();
29+
return serverOptions;
30+
}
31+
32+
[Fact]
33+
public void ConfigureNamedEndpoint_OnlyRunForMatchingConfig()
34+
{
35+
var found = false;
36+
var serverOptions = CreateServerOptions();
37+
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
38+
{
39+
new KeyValuePair<string, string>("Endpoints:Found:Url", "http://*:5001"),
40+
}).Build();
41+
serverOptions.Configure(config)
42+
.Endpoint("Found", endpointOptions => found = true)
43+
.Endpoint("NotFound", endpointOptions => throw new NotImplementedException())
44+
.Build();
45+
46+
Assert.Single(serverOptions.ListenOptions);
47+
Assert.Equal(5001, serverOptions.ListenOptions[0].IPEndPoint.Port);
48+
49+
Assert.True(found);
50+
}
51+
52+
[Fact]
53+
public void ConfigureEndpoint_OnlyRunWhenBuildIsCalled()
54+
{
55+
var run = false;
56+
var serverOptions = CreateServerOptions();
57+
serverOptions.Configure()
58+
.LocalhostEndpoint(5001, endpointOptions => run = true);
59+
60+
Assert.Empty(serverOptions.ListenOptions);
61+
62+
serverOptions.ConfigurationBuilder.Build();
63+
64+
Assert.Single(serverOptions.ListenOptions);
65+
Assert.Equal(5001, serverOptions.ListenOptions[0].IPEndPoint.Port);
66+
67+
Assert.True(run);
68+
}
69+
70+
[Fact]
71+
public void CallBuildTwice_OnlyRunsOnce()
72+
{
73+
var serverOptions = CreateServerOptions();
74+
var builder = serverOptions.Configure()
75+
.LocalhostEndpoint(5001);
76+
77+
Assert.Empty(serverOptions.ListenOptions);
78+
Assert.Equal(builder, serverOptions.ConfigurationBuilder);
79+
80+
builder.Build();
81+
82+
Assert.Single(serverOptions.ListenOptions);
83+
Assert.Equal(5001, serverOptions.ListenOptions[0].IPEndPoint.Port);
84+
Assert.Null(serverOptions.ConfigurationBuilder);
85+
86+
builder.Build();
87+
88+
Assert.Single(serverOptions.ListenOptions);
89+
Assert.Equal(5001, serverOptions.ListenOptions[0].IPEndPoint.Port);
90+
Assert.Null(serverOptions.ConfigurationBuilder);
91+
}
92+
93+
[Fact]
94+
public void Configure_IsReplacable()
95+
{
96+
var run1 = false;
97+
var serverOptions = CreateServerOptions();
98+
var config1 = new ConfigurationBuilder().AddInMemoryCollection(new[]
99+
{
100+
new KeyValuePair<string, string>("Endpoints:End1:Url", "http://*:5001"),
101+
}).Build();
102+
serverOptions.Configure(config1)
103+
.LocalhostEndpoint(5001, endpointOptions => run1 = true);
104+
105+
Assert.Empty(serverOptions.ListenOptions);
106+
Assert.False(run1);
107+
108+
var run2 = false;
109+
var config2 = new ConfigurationBuilder().AddInMemoryCollection(new[]
110+
{
111+
new KeyValuePair<string, string>("Endpoints:End2:Url", "http://*:5002"),
112+
}).Build();
113+
serverOptions.Configure(config2)
114+
.LocalhostEndpoint(5003, endpointOptions => run2 = true);
115+
116+
serverOptions.ConfigurationBuilder.Build();
117+
118+
Assert.Equal(2, serverOptions.ListenOptions.Count);
119+
Assert.Equal(5002, serverOptions.ListenOptions[0].IPEndPoint.Port);
120+
Assert.Equal(5003, serverOptions.ListenOptions[1].IPEndPoint.Port);
121+
122+
Assert.False(run1);
123+
Assert.True(run2);
124+
}
125+
126+
[Fact]
127+
public void ConfigureDefaultsAppliesToNewConfigureEndpoints()
128+
{
129+
var serverOptions = CreateServerOptions();
130+
serverOptions.ListenLocalhost(5000);
131+
132+
Assert.True(serverOptions.ListenOptions[0].NoDelay);
133+
134+
serverOptions.ConfigureEndpointDefaults(opt =>
135+
{
136+
opt.NoDelay = false;
137+
});
138+
139+
serverOptions.ConfigureHttpsDefaults(opt =>
140+
{
141+
opt.ServerCertificate = new X509Certificate2(TestResources.TestCertificatePath, "testPassword");
142+
opt.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
143+
});
144+
145+
var ran1 = false;
146+
var ran2 = false;
147+
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
148+
{
149+
new KeyValuePair<string, string>("Endpoints:End1:Url", "https://*:5001"),
150+
}).Build();
151+
serverOptions.Configure(config)
152+
.Endpoint("End1", opt =>
153+
{
154+
ran1 = true;
155+
Assert.True(opt.IsHttps);
156+
Assert.Equal(ClientCertificateMode.RequireCertificate, opt.Https.ClientCertificateMode);
157+
Assert.False(opt.Listener.NoDelay);
158+
})
159+
.LocalhostEndpoint(5002, opt =>
160+
{
161+
ran2 = true;
162+
Assert.False(opt.NoDelay);
163+
})
164+
.Build();
165+
166+
Assert.True(ran1);
167+
Assert.True(ran2);
168+
169+
Assert.NotNull(serverOptions.ListenOptions[0].ConnectionAdapters.Where(adapter => adapter.IsHttps).SingleOrDefault());
170+
Assert.Null(serverOptions.ListenOptions[1].ConnectionAdapters.Where(adapter => adapter.IsHttps).SingleOrDefault());
171+
}
172+
173+
[Fact]
174+
public void ConfigureEndpointDefaultCanEnableHttps()
175+
{
176+
var serverOptions = CreateServerOptions();
177+
178+
serverOptions.ConfigureEndpointDefaults(opt =>
179+
{
180+
opt.NoDelay = false;
181+
opt.UseHttps(new X509Certificate2(TestResources.TestCertificatePath, "testPassword"));
182+
});
183+
184+
serverOptions.ConfigureHttpsDefaults(opt =>
185+
{
186+
opt.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
187+
});
188+
189+
var ran1 = false;
190+
var ran2 = false;
191+
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
192+
{
193+
new KeyValuePair<string, string>("Endpoints:End1:Url", "https://*:5001"),
194+
}).Build();
195+
serverOptions.Configure(config)
196+
.Endpoint("End1", opt =>
197+
{
198+
ran1 = true;
199+
Assert.True(opt.IsHttps);
200+
Assert.Equal(ClientCertificateMode.RequireCertificate, opt.Https.ClientCertificateMode);
201+
Assert.False(opt.Listener.NoDelay);
202+
})
203+
.LocalhostEndpoint(5002, opt =>
204+
{
205+
ran2 = true;
206+
Assert.False(opt.NoDelay);
207+
})
208+
.Build();
209+
210+
Assert.True(ran1);
211+
Assert.True(ran2);
212+
213+
// You only get Https once per endpoint.
214+
Assert.NotNull(serverOptions.ListenOptions[0].ConnectionAdapters.Where(adapter => adapter.IsHttps).SingleOrDefault());
215+
Assert.NotNull(serverOptions.ListenOptions[1].ConnectionAdapters.Where(adapter => adapter.IsHttps).SingleOrDefault());
216+
}
217+
}
218+
}

0 commit comments

Comments
 (0)