Make preview check for delivery API content case insensitive. (#18731)

This commit is contained in:
Andy Butland
2025-03-20 06:53:43 +01:00
committed by kjac
parent 442de9a803
commit 5f37cd3d20
2 changed files with 33 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Umbraco.Cms.Core.DeliveryApi;
namespace Umbraco.Cms.Api.Delivery.Services;
@@ -11,5 +11,5 @@ internal sealed class RequestPreviewService : RequestHeaderHandler, IRequestPrev
}
/// <inheritdoc />
public bool IsPreview() => GetHeaderValue("Preview") == "true";
public bool IsPreview() => string.Equals(GetHeaderValue("Preview"), "true", StringComparison.OrdinalIgnoreCase);
}

View File

@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Http;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Api.Delivery.Services;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Cms.Api.Delivery.Services;
[TestFixture]
public class RequestPreviewServiceTests
{
[TestCase(null, false)]
[TestCase("", false)]
[TestCase("false", false)]
[TestCase("true", true)]
[TestCase("True", true)]
public void IsPreview_Returns_Expected_Result(string? headerValue, bool expected)
{
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["Preview"] = headerValue;
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
httpContextAccessorMock
.Setup(x => x.HttpContext)
.Returns(httpContext);
var sut = new RequestPreviewService(httpContextAccessorMock.Object);
var result = sut.IsPreview();
Assert.AreEqual(expected, result);
}
}