Skip to content

Commit 7782b6c

Browse files
committed
Add MapActionTest to Routing.FunctionalTests
1 parent 57d1efa commit 7782b6c

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
#nullable enable
5+
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Net.Http.Json;
9+
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Builder;
11+
using Microsoft.AspNetCore.Hosting;
12+
using Microsoft.AspNetCore.Http;
13+
using Microsoft.AspNetCore.Http.Api;
14+
using Microsoft.AspNetCore.Mvc.Routing;
15+
using Microsoft.AspNetCore.TestHost;
16+
using Microsoft.Extensions.DependencyInjection;
17+
using Microsoft.Extensions.Hosting;
18+
using Xunit;
19+
20+
namespace Microsoft.AspNetCore.Routing.FunctionalTests
21+
{
22+
public class MapActionTest
23+
{
24+
[Fact]
25+
public async Task MapAction_FromBodyWorksWithJsonPayload()
26+
{
27+
[HttpPost("/EchoTodo")]
28+
Todo EchoTodo([FromBody] Todo todo) => todo;
29+
30+
using var host = new HostBuilder()
31+
.ConfigureWebHost(webHostBuilder =>
32+
{
33+
webHostBuilder
34+
.Configure(app =>
35+
{
36+
app.UseRouting();
37+
app.UseEndpoints(b => b.MapAction((Func<Todo, Todo>)EchoTodo));
38+
})
39+
.UseTestServer();
40+
})
41+
.ConfigureServices(services =>
42+
{
43+
services.AddAuthorization();
44+
services.AddRouting();
45+
})
46+
.Build();
47+
48+
using var server = host.GetTestServer();
49+
await host.StartAsync();
50+
var client = server.CreateClient();
51+
52+
var todo = new Todo
53+
{
54+
Name = "Custom Todo"
55+
};
56+
57+
var response = await client.PostAsJsonAsync("/EchoTodo", todo);
58+
response.EnsureSuccessStatusCode();
59+
60+
var echoedTodo = await response.Content.ReadFromJsonAsync<Todo>();
61+
62+
Assert.Equal(todo.Name, echoedTodo?.Name);
63+
}
64+
65+
private class Todo
66+
{
67+
public int Id { get; set; }
68+
public string Name { get; set; } = "Todo";
69+
public bool IsComplete { get; set; }
70+
}
71+
72+
private class FromBodyAttribute : Attribute, IFromBodyMetadata { }
73+
74+
private class HttpPostAttribute : Attribute, IRouteTemplateProvider, IHttpMethodMetadata
75+
{
76+
public HttpPostAttribute(string template)
77+
{
78+
Template = template;
79+
}
80+
81+
public string Template { get; }
82+
83+
public IReadOnlyList<string> HttpMethods { get; } = new[] { "POST" };
84+
85+
public int? Order => null;
86+
87+
public string? Name => null;
88+
89+
public bool AcceptCorsPreflight => false;
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)