Skip to content

Commit

Permalink
Enum in FromQuery and FromRoute (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio1192 authored Aug 13, 2024
1 parent adf9320 commit 1ac0440
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Acheve.TestHost/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/Acheve.TestHost/Routing/UriDiscover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ private static TestServerAction GetTestServerAction<TController>(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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,12 @@ public ActionResult<ParamWithDifferentFroms> PostWithDifferentFroms(ParamWithDif
[HttpPut($"{nameof(PutWithDifferentFroms)}/{{{nameof(ParamWithDifferentFroms.ParamFromRoute)}}}")]
public ActionResult<ParamWithDifferentFroms> 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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace UnitTests.Acheve.TestHost.Routing.Models;

public enum SampleEnumeration
{
Value1,
Value2,
Value3
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValuesV5Controller>(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<ValuesV5Controller>(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()
Expand Down

0 comments on commit 1ac0440

Please sign in to comment.