-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update UseServerCertificateSelector to call the original selector (
#290)
- Loading branch information
1 parent
ba32cda
commit dbe32b5
Showing
3 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
test/LettuceEncrypt.UnitTests/KestrelHttpsOptionsExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) Nate McMaster. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Security.Cryptography.X509Certificates; | ||
using McMaster.AspNetCore.Kestrel.Certificates; | ||
using Microsoft.AspNetCore.Connections; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Server.Kestrel.Https; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace LettuceEncrypt.UnitTests; | ||
|
||
using SelectorFunc = Func<ConnectionContext, string, X509Certificate2>; | ||
|
||
public class KestrelHttpsOptionsExtensionsTests | ||
{ | ||
[Fact] | ||
public void UseServerCertificateSelectorFallsbackToOriginalSelector() | ||
{ | ||
var injectedSelector = new Mock<IServerCertificateSelector>(); | ||
injectedSelector | ||
.Setup(c => c.Select(It.IsAny<ConnectionContext>(), It.IsAny<string>())) | ||
.Returns(() => null); | ||
|
||
var originalSelectorWasCalled = false; | ||
SelectorFunc originalSelector = (_, __) => { originalSelectorWasCalled = true; return null; }; | ||
|
||
var options = new HttpsConnectionAdapterOptions | ||
{ | ||
ServerCertificateSelector = originalSelector | ||
}; | ||
|
||
KestrelHttpsOptionsExtensions.UseServerCertificateSelector(options, injectedSelector.Object); | ||
options.ServerCertificateSelector(null, null); | ||
|
||
Assert.NotSame(options.ServerCertificateSelector, originalSelector); | ||
Assert.True(originalSelectorWasCalled); | ||
injectedSelector.VerifyAll(); | ||
} | ||
|
||
[Fact] | ||
public void UseServerCertificateSelectorDoesNotCallFallback() | ||
{ | ||
var injectedSelector = new Mock<IServerCertificateSelector>(); | ||
injectedSelector | ||
.Setup(c => c.Select(It.IsAny<ConnectionContext>(), It.IsAny<string>())) | ||
.Returns(() => TestUtils.CreateTestCert("foo.test")); | ||
|
||
var originalSelectorWasCalled = false; | ||
SelectorFunc originalSelector = (_, __) => { originalSelectorWasCalled = true; return null; }; | ||
|
||
var options = new HttpsConnectionAdapterOptions | ||
{ | ||
ServerCertificateSelector = originalSelector | ||
}; | ||
|
||
KestrelHttpsOptionsExtensions.UseServerCertificateSelector(options, injectedSelector.Object); | ||
options.ServerCertificateSelector(null, null); | ||
|
||
Assert.NotSame(options.ServerCertificateSelector, originalSelector); | ||
Assert.False(originalSelectorWasCalled); | ||
injectedSelector.VerifyAll(); | ||
} | ||
} |