Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* added logic of apicontroller where the [frombody] #35

Merged
merged 5 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ public override IEnumerable<string> GetTemplates<TController>(TestServerAction a
var putAttributes = action.MethodInfo.GetCustomAttributes<HttpPutAttribute>()
.OfType<HttpMethodAttribute>();

var patchAttributes = action.MethodInfo.GetCustomAttributes<HttpPatchAttribute>()
.OfType<HttpMethodAttribute>();

var deleteAttributes = action.MethodInfo.GetCustomAttributes<HttpDeleteAttribute>()
.OfType<HttpMethodAttribute>();

var verbAttribute = getAttributes
.Union(postAttributes)
.Union(putAttributes)
.Union(patchAttributes)
.Union(deleteAttributes)
.SingleOrDefault();

Expand Down
10 changes: 9 additions & 1 deletion src/Acheve.TestHost/Routing/TestServerAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ public TestServerAction(MethodInfo methodInfo)
ArgumentValues = new Dictionary<int, TestServerArgument>();
}

public void AddArgument(int order, Expression expression)
public void AddArgument(int order, Expression expression, bool activeBodyApiController)
{
var argument = MethodInfo.GetParameters()[order];
var isFromBody = argument.GetCustomAttributes<FromBodyAttribute>().Any();
var isFromForm = argument.GetCustomAttributes<FromFormAttribute>().Any();
var isFromHeader = argument.GetCustomAttributes<FromHeaderAttribute>().Any();

bool isPrimitive = argument.ParameterType.IsPrimitive || argument.ParameterType.Name.Equals(typeof(string));
bool hasNoAttributes = !isFromBody && !isFromForm && !isFromHeader;

if (activeBodyApiController && hasNoAttributes && !isPrimitive)
{
isFromBody = true;
}

if (!ArgumentValues.ContainsKey(order))
{
switch (expression)
Expand Down
5 changes: 4 additions & 1 deletion src/Acheve.TestHost/TestServerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,15 @@ private static TestServerAction GetTestServerAction<TController>(Expression<Func
var methodCall = (MethodCallExpression)actionSelector.Body;

var action = new TestServerAction(methodCall.Method);
bool haveAttributeApiController = typeof(TController).GetTypeInfo().GetCustomAttribute(typeof(ApiControllerAttribute)) != null;
bool isGetOrDelete = action.MethodInfo.GetCustomAttributes().FirstOrDefault(attr => attr.GetType() == typeof(HttpGetAttribute)
|| attr.GetType() == typeof(HttpDeleteAttribute)) != null;

var index = 0;

foreach (var item in methodCall.Arguments)
{
action.AddArgument(index, item);
action.AddArgument(index, item, haveAttributeApiController && !isGetOrDelete);

++index;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Mvc;

namespace UnitTests.Acheve.TestHost.Builders
{
[Route("api/values")]
[ApiController]
public class ValuesV5Controller : ControllerBase
{
[HttpPost]
public IActionResult Post1(Pagination pagination)
{
return Ok();
}

[HttpPost("{id:int}")]
public IActionResult Post2(int id, Pagination pagination1)
{
if (pagination1 == null)
{
return BadRequest();
}

return Ok();
}

[HttpPatch("{id:int}")]
public IActionResult Patch1(int id, Pagination pagination1)
{
if (pagination1 == null)
{
return BadRequest();
}

return Ok();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using UnitTests.Acheve.TestHost.Builders;
using Xunit;
Expand Down Expand Up @@ -736,9 +737,9 @@ public void create_valid_request_using_from_header_primitive_arguments_and_from_

var requestPost2 = server.CreateHttpApiRequest<ValuesV3Controller>(
controller => controller.Post6(header, complexParameter));

requestPost2.GetRequest().Headers.GetValues("custom").First().Should().Be(header);
requestPost2.GetConfiguredAddress()
.Should().Be("api/values/post6");
requestPost2.GetConfiguredAddress().Should().Be("api/values/post6");
}

[Fact]
Expand Down Expand Up @@ -1269,6 +1270,65 @@ public void create_request_supporting_guid_types_on_parameters_and_numbes_on_par
.Should().Be($"api/bugs/prm1/{guidValue}");
}

[Fact]
public void create_valid_request_without_using_frombody_with_apicontroller_attribute()
{
var server = new TestServerBuilder().UseDefaultStartup()
.Build();

var complexParameter = new Pagination()
{
PageCount = 10,
PageIndex = 1
};

var requestPost1 = server.CreateHttpApiRequest<ValuesV5Controller>(controller => controller.Post1(complexParameter));

string body = requestPost1.GetRequest().Content.ReadAsStringAsync().Result;
JsonSerializer.Deserialize<Pagination>(body).PageIndex.Should().Be(complexParameter.PageIndex);
JsonSerializer.Deserialize<Pagination>(body).PageCount.Should().Be(complexParameter.PageCount);
}

[Fact]
public void create_valid_request_without_using_frombody_with_apicontroller_attribute_and_route_parameter()
{
var server = new TestServerBuilder().UseDefaultStartup()
.Build();

var complexParameter = new Pagination()
{
PageCount = 10,
PageIndex = 1
};

var requestPost2 = server.CreateHttpApiRequest<ValuesV5Controller>(controller => controller.Post2(1, complexParameter));

string body = requestPost2.GetRequest().Content.ReadAsStringAsync().Result;
JsonSerializer.Deserialize<Pagination>(body).PageIndex.Should().Be(complexParameter.PageIndex);
JsonSerializer.Deserialize<Pagination>(body).PageCount.Should().Be(complexParameter.PageCount);
requestPost2.GetConfiguredAddress().StartsWith("api/values/1").Should().Be(true);
}

[Fact]
public void create_valid_request_of_patch_without_using_frombody_with_apicontroller_attribute_and_route_parameter()
{
var server = new TestServerBuilder().UseDefaultStartup()
.Build();

var complexParameter = new Pagination()
{
PageCount = 10,
PageIndex = 1
};

var requestPost2 = server.CreateHttpApiRequest<ValuesV5Controller>(controller => controller.Patch1(1, complexParameter));

string body = requestPost2.GetRequest().Content.ReadAsStringAsync().Result;
JsonSerializer.Deserialize<Pagination>(body).PageIndex.Should().Be(complexParameter.PageIndex);
JsonSerializer.Deserialize<Pagination>(body).PageCount.Should().Be(complexParameter.PageCount);
requestPost2.GetConfiguredAddress().StartsWith("api/values/1").Should().Be(true);
}

private class PrivateNonControllerClass
{
public int SomeAction()
Expand Down