-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for automatically setting theme based on system settings (#…
…57) * Add support for automatically setting theme based on the user's system settings * Add parameter validation
- Loading branch information
Showing
7 changed files
with
278 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text.Encodings.Web; | ||
using Microsoft.AspNetCore.Html; | ||
|
||
namespace AspNetCore.ReCaptcha.Tests; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="IHtmlContent"/>. | ||
/// </summary> | ||
public static class HtmlContentExtensions | ||
{ | ||
/// <summary> | ||
/// Returns the string representation of the HTML content. | ||
/// </summary> | ||
/// <param name="htmlContent">HTML content which can be written to a TextWriter.</param> | ||
/// <returns>The string representation of the HTML content.</returns> | ||
public static string ToHtmlString(this IHtmlContent htmlContent) | ||
{ | ||
switch (htmlContent) | ||
{ | ||
case null: | ||
throw new ArgumentNullException(nameof(htmlContent)); | ||
case HtmlString htmlString: | ||
return htmlString.Value; | ||
} | ||
|
||
using var writer = new StringWriter(); | ||
htmlContent.WriteTo(writer, HtmlEncoder.Default); | ||
return writer.ToString(); | ||
} | ||
} |
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
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,102 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace AspNetCore.ReCaptcha.Tests; | ||
|
||
public class ReCaptchaTagHelperTests | ||
{ | ||
private readonly ReCaptchaTagHelper _reCaptchaTagHelper; | ||
private readonly ReCaptchaSettings _reCaptchaSettings; | ||
|
||
public ReCaptchaTagHelperTests() | ||
{ | ||
// Setup mocks | ||
_reCaptchaSettings = new ReCaptchaSettings(); | ||
|
||
var mockReCaptchaSettingsSnapshot = new Mock<IOptionsSnapshot<ReCaptchaSettings>>(); | ||
mockReCaptchaSettingsSnapshot.Setup(m => m.Value) | ||
.Returns(_reCaptchaSettings); | ||
var reCaptchaSettingsSnapshot = mockReCaptchaSettingsSnapshot.Object; | ||
|
||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddSingleton<IOptions<ReCaptchaSettings>>(reCaptchaSettingsSnapshot); | ||
|
||
ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider(); | ||
|
||
var featureCollection = new FeatureCollection(); | ||
|
||
var mockHttpContext = new Mock<HttpContext>(); | ||
mockHttpContext.Setup(m => m.RequestServices) | ||
.Returns(serviceProvider); | ||
mockHttpContext.Setup(m => m.Features) | ||
.Returns(featureCollection); | ||
HttpContext httpContext = mockHttpContext.Object; | ||
|
||
var viewContext = new ViewContext | ||
{ | ||
HttpContext = httpContext | ||
}; | ||
|
||
// Setup SUT | ||
_reCaptchaTagHelper = new ReCaptchaTagHelper | ||
{ | ||
ViewContext = viewContext | ||
}; | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, true)] | ||
[InlineData(false, false)] | ||
public void ProcessConsidersAutoThemeSetting(bool autoTheme, bool expectScript) | ||
{ | ||
// Arrange | ||
TagHelperContext tagHelperContext = CreateTagHelperContext(); | ||
TagHelperOutput tagHelperOutput = CreateTagHelperOutput(); | ||
|
||
_reCaptchaTagHelper.AutoTheme = autoTheme; | ||
|
||
// Act | ||
_reCaptchaTagHelper.Process(tagHelperContext, tagHelperOutput); | ||
|
||
// Assert | ||
Assert.True(tagHelperOutput.Content.IsModified); | ||
string htmlString = tagHelperOutput.Content.GetContent(); | ||
const string mediaQueryString = "prefers-color-scheme"; | ||
if (expectScript) | ||
{ | ||
Assert.Contains(mediaQueryString, htmlString); | ||
} | ||
else | ||
{ | ||
Assert.DoesNotContain(mediaQueryString, htmlString); | ||
} | ||
} | ||
|
||
private static TagHelperOutput CreateTagHelperOutput() | ||
{ | ||
const string tagName = "foo-bar"; | ||
var attributes = new TagHelperAttributeList(); | ||
Task<TagHelperContent> GetChildContentAsync(bool useCachedResult, HtmlEncoder encoder) => | ||
Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()); | ||
|
||
return new TagHelperOutput(tagName, attributes, GetChildContentAsync); | ||
} | ||
|
||
private static TagHelperContext CreateTagHelperContext() | ||
{ | ||
var allAttributes = new TagHelperAttributeList(); | ||
var items = new Dictionary<object, object>(); | ||
const string uniqueId = "fizz-buzz"; | ||
|
||
return new TagHelperContext(allAttributes, items, uniqueId); | ||
} | ||
} |
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
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