Skip to content

Commit 4de92f5

Browse files
authored
Enable classes derived from EnableQueryAttribute to unit test OnActionExecuted in isolation (#776)
1 parent 9cf8554 commit 4de92f5

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Microsoft.AspNetCore.OData/Query/EnableQueryAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ private ODataQueryOptions CreateAndValidateQueryOptions(HttpRequest request, ODa
542542
{
543543
RequestQueryData requestQueryData = request.HttpContext.Items[nameof(RequestQueryData)] as RequestQueryData;
544544

545-
if (requestQueryData.QueryValidationRunBeforeActionExecution)
545+
if (requestQueryData != null && requestQueryData.QueryValidationRunBeforeActionExecution)
546546
{
547547
return requestQueryData.ProcessedQueryOptions;
548548
}

test/Microsoft.AspNetCore.OData.Tests/Query/EnableQueryAttributeTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,43 @@ private static IEnumerable<ControllerActionDescriptor> CreateDescriptors(string
318318
return descriptors;
319319
}
320320

321+
[Fact]
322+
public void Derived_Class_Can_Unit_Test_When_OnActionExecuted_Is_Overridden()
323+
{
324+
// https://github.com/OData/AspNetCoreOData/issues/239: unit tests for derived classes fail because RequestQueryOptions missing from HttpContext.Items
325+
IEdmModel model = new CustomersModelWithInheritance().Model;
326+
327+
var request = RequestFactory.Create("Get", "http://localhost/Customers?$filter=Id+eq+1", opt => opt.AddRouteComponents("odata", model, services => { }).Filter());
328+
329+
ODataUriParser parser = new ODataUriParser(model, new Uri("Customers", UriKind.Relative));
330+
HttpContext httpContext = request.HttpContext;
331+
332+
Assert.NotNull(httpContext.RequestServices);
333+
334+
httpContext.Request.Method = "Get";
335+
request.Configure("odata", model, parser.ParsePath());
336+
ActionDescriptor actionDescriptor = CreateDescriptors("CustomersController", typeof(CustomersController))
337+
.First(descriptor => descriptor.ActionName.StartsWith("Get", StringComparison.OrdinalIgnoreCase));
338+
ActionContext actionContext = new ActionContext(httpContext, new RouteData(), actionDescriptor);
339+
340+
ActionExecutedContext context = new ActionExecutedContext(actionContext, new List<IFilterMetadata>(), new CustomersController());
341+
context.Result = new ObjectResult(new List<Customer>() { new Customer { Id = 1, Name = "John Doe" } }) { StatusCode = 200 };
342+
343+
MyEnableQueryAttribute attribute = new MyEnableQueryAttribute();
344+
345+
// Act and Assert
346+
ExceptionAssert.DoesNotThrow(() => attribute.OnActionExecuted(context));
347+
348+
}
349+
350+
private class MyEnableQueryAttribute : EnableQueryAttribute
351+
{
352+
public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
353+
{
354+
base.OnActionExecuted(actionExecutedContext);
355+
}
356+
}
357+
321358
#if NETCORE // Following functionality is only supported in NetCore.
322359
[Fact]
323360
public void OnActionExecuting_Throws_Null_Context()

0 commit comments

Comments
 (0)