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

Allow cancellation token in endpoints #76

Merged
merged 1 commit into from
Sep 21, 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
8 changes: 7 additions & 1 deletion src/Acheve.TestHost/Routing/TestServerAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ public void AddArgument(int order, Expression expression, bool activeBodyApiCont
var isFromHeader = argument.GetCustomAttributes<FromHeaderAttribute>().Any();
var isFromRoute = argument.GetCustomAttributes<FromRouteAttribute>().Any();

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

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

var isCancellationToken = argument.ParameterType == typeof(System.Threading.CancellationToken);
if (isCancellationToken)
{
isFromBody = isFromForm = isFromHeader = false;
}

if (!ArgumentValues.ContainsKey(order))
{
if (IsNullable(argument.ParameterType))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using System.Threading;

namespace UnitTests.Acheve.TestHost.Builders
{
Expand Down Expand Up @@ -44,5 +45,27 @@ public IActionResult Patch1(int id, Pagination pagination1)

return Ok();
}

[HttpGet(nameof(GetWithCancellationToken))]
public ActionResult<string> GetWithCancellationToken([FromQuery] string value, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return BadRequest();
}

return Ok(value);
}

[HttpPost(nameof(PostWithCancellationToken))]
public ActionResult<string> PostWithCancellationToken([FromBody] string value, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return BadRequest();
}

return Ok(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using UnitTests.Acheve.TestHost.Builders;
using UnitTests.Acheve.TestHost.Routing.Models;
Expand Down Expand Up @@ -1676,6 +1677,44 @@ public async Task Create_request_with_list_parameter()
response.Should().BeEquivalentTo(param);
}

[Fact]
public async Task Create_get_request_with_cancellation_token_parameter()
{
var server = new TestServerBuilder()
.UseDefaultStartup()
.Build();

var param = "one";
var source = new CancellationTokenSource();
var token = source.Token;

var request = server.CreateHttpApiRequest<ValuesV5Controller>(controller => controller.GetWithCancellationToken(param, token));
var responseMessage = await request.GetAsync();

responseMessage.EnsureSuccessStatusCode();
var response = await responseMessage.Content.ReadAsStringAsync();
response.Should().Be(param);
}

[Fact]
public async Task Create_post_request_with_cancellation_token_parameter()
{
var server = new TestServerBuilder()
.UseDefaultStartup()
.Build();

var param = "one";
var source = new CancellationTokenSource();
var token = source.Token;

var request = server.CreateHttpApiRequest<ValuesV5Controller>(controller => controller.PostWithCancellationToken(param, token));
var responseMessage = await request.PostAsync();

responseMessage.EnsureSuccessStatusCode();
var response = await responseMessage.Content.ReadAsStringAsync();
response.Should().Be(param);
}

private class PrivateNonControllerClass
{
public int SomeAction()
Expand Down