diff --git a/src/Acheve.TestHost/Extensions/TypeExtensions.cs b/src/Acheve.TestHost/Extensions/TypeExtensions.cs index 6a29ed6..4bffff0 100644 --- a/src/Acheve.TestHost/Extensions/TypeExtensions.cs +++ b/src/Acheve.TestHost/Extensions/TypeExtensions.cs @@ -13,7 +13,8 @@ internal static bool IsPrimitiveType(this Type typeToInspect) || type == typeof(string) || type == typeof(decimal) || type == typeof(Guid) - || type.IsDateTime(); + || type.IsDateTime() + || type.IsEnum; } internal static bool IsDateTime(this Type typeToInspect) diff --git a/src/Acheve.TestHost/Routing/UriDiscover.cs b/src/Acheve.TestHost/Routing/UriDiscover.cs index f9d83db..2914d12 100644 --- a/src/Acheve.TestHost/Routing/UriDiscover.cs +++ b/src/Acheve.TestHost/Routing/UriDiscover.cs @@ -215,7 +215,9 @@ private static TestServerAction GetTestServerAction(LambdaExpressio throw new InvalidOperationException($"The action selector is not a valid lambda expression"); } - var methodCall = (MethodCallExpression)actionSelector.Body; + var methodCall = actionSelector.Body as MethodCallExpression; + if (methodCall is null) + methodCall = (actionSelector.Body as UnaryExpression).Operand as MethodCallExpression; var action = new TestServerAction(methodCall.Method); bool haveAttributeApiController = typeof(TController).GetTypeInfo().GetCustomAttribute(typeof(ApiControllerAttribute)) != null; diff --git a/tests/UnitTests/Acheve.TestHost/Routing/Builders/ValuesV5Controller.cs b/tests/UnitTests/Acheve.TestHost/Routing/Builders/ValuesV5Controller.cs index 05d3d09..b9412af 100644 --- a/tests/UnitTests/Acheve.TestHost/Routing/Builders/ValuesV5Controller.cs +++ b/tests/UnitTests/Acheve.TestHost/Routing/Builders/ValuesV5Controller.cs @@ -125,4 +125,12 @@ public ActionResult PostWithDifferentFroms(ParamWithDif [HttpPut($"{nameof(PutWithDifferentFroms)}/{{{nameof(ParamWithDifferentFroms.ParamFromRoute)}}}")] public ActionResult PutWithDifferentFroms(ParamWithDifferentFroms request) => Ok(request); + + [HttpGet($"{nameof(GetWithEnumInRoute)}/{{request}}")] + public string GetWithEnumInRoute([FromRoute] SampleEnumeration request) + => request.ToString(); + + [HttpGet($"{nameof(GetWithEnumInQuery)}")] + public string GetWithEnumInQuery([FromQuery] SampleEnumeration request) + => request.ToString(); } diff --git a/tests/UnitTests/Acheve.TestHost/Routing/Models/SampleEnumeration.cs b/tests/UnitTests/Acheve.TestHost/Routing/Models/SampleEnumeration.cs new file mode 100644 index 0000000..7de4b21 --- /dev/null +++ b/tests/UnitTests/Acheve.TestHost/Routing/Models/SampleEnumeration.cs @@ -0,0 +1,8 @@ +namespace UnitTests.Acheve.TestHost.Routing.Models; + +public enum SampleEnumeration +{ + Value1, + Value2, + Value3 +} diff --git a/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs b/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs index e319f82..e0987d7 100644 --- a/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs +++ b/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs @@ -1954,6 +1954,40 @@ public async Task Create_put_request_with_object_with_different_froms() content.ParamFromBody.Should().Be(model.ParamFromBody); } + [Fact] + public async Task Create_get_request_with_enum_from_query() + { + var server = new TestServerBuilder() + .UseDefaultStartup() + .Build(); + + var enumValue = SampleEnumeration.Value3; + + var request = server.CreateHttpApiRequest(controller => controller.GetWithEnumInQuery(enumValue)); + var responseMessage = await request.GetAsync(); + + await responseMessage.IsSuccessStatusCodeOrThrow(); + var response = await responseMessage.Content.ReadAsStringAsync(); + response.Should().Be(enumValue.ToString()); + } + + [Fact] + public async Task Create_get_request_with_enum_from_route() + { + var server = new TestServerBuilder() + .UseDefaultStartup() + .Build(); + + var enumValue = SampleEnumeration.Value3; + + var request = server.CreateHttpApiRequest(controller => controller.GetWithEnumInRoute(enumValue)); + var responseMessage = await request.GetAsync(); + + await responseMessage.IsSuccessStatusCodeOrThrow(); + var response = await responseMessage.Content.ReadAsStringAsync(); + response.Should().Be(enumValue.ToString()); + } + private class PrivateNonControllerClass { public int SomeAction()