Skip to content

Commit

Permalink
Merge pull request #35 from dadjh85/master
Browse files Browse the repository at this point in the history
* added logic of apicontroller where the [frombody]
  • Loading branch information
Sergio1192 committed Mar 14, 2022
2 parents e4a071e + 3ef844d commit 15416f1
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
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

0 comments on commit 15416f1

Please sign in to comment.