diff --git a/src/ServiceControl.UnitTests/Infrastructure/WebApi/SortInfoModelBinderTests.cs b/src/ServiceControl.UnitTests/Infrastructure/WebApi/SortInfoModelBinderTests.cs new file mode 100644 index 0000000000..01a40a5be9 --- /dev/null +++ b/src/ServiceControl.UnitTests/Infrastructure/WebApi/SortInfoModelBinderTests.cs @@ -0,0 +1,52 @@ +namespace ServiceControl.UnitTests.Infrastructure.WebApi +{ + using System.Collections.Generic; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Http; + using Microsoft.AspNetCore.Mvc; + using Microsoft.AspNetCore.Mvc.ModelBinding; + using Microsoft.Extensions.Primitives; + using NUnit.Framework; + using Persistence.Infrastructure; + using ServiceControl.Infrastructure.WebApi; + + [TestFixture] + public class SortInfoModelBinderTests + { + // Failed-message endpoints (/api/errors) support these sort fields in the + // RavenDB layer. The model binder must not silently rewrite them away. + [TestCase("time_of_failure")] + [TestCase("modified")] + public async Task Preserves_valid_failed_message_sort_field(string sort) + { + var sortInfo = await Bind(sort, "asc"); + + Assert.That(sortInfo.Sort, Is.EqualTo(sort)); + } + + static async Task Bind(string sort, string direction) + { + var httpContext = new DefaultHttpContext + { + Request = + { + Query = new QueryCollection(new Dictionary + { + ["sort"] = sort, + ["direction"] = direction, + }) + } + }; + + var bindingContext = new DefaultModelBindingContext + { + ActionContext = new ActionContext { HttpContext = httpContext }, + ModelName = "sortInfo", + }; + + await new SortInfoModelBinder().BindModelAsync(bindingContext); + + return (SortInfo)bindingContext.Result.Model; + } + } +} diff --git a/src/ServiceControl/Infrastructure/WebApi/SortInfoModelBinder.cs b/src/ServiceControl/Infrastructure/WebApi/SortInfoModelBinder.cs index 0e45358a62..c78414947f 100644 --- a/src/ServiceControl/Infrastructure/WebApi/SortInfoModelBinder.cs +++ b/src/ServiceControl/Infrastructure/WebApi/SortInfoModelBinder.cs @@ -41,6 +41,8 @@ public Task BindModelAsync(ModelBindingContext bindingContext) "delivery_time", "processing_time", "status", - "message_id" + "message_id", + "modified", + "time_of_failure" }.ToFrozenSet(); } \ No newline at end of file