[Fixes #367] Add extensions on WebHostBuilder for super simple HTTP service application building
This commit is contained in:
parent
45247e4d9c
commit
20967cfa76
|
|
@ -0,0 +1,50 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Routing;
|
||||||
|
using Microsoft.AspNetCore.Routing.Constraints;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace RoutingSample.Web
|
||||||
|
{
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(10);
|
||||||
|
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
var webHost = GetWebHostBuilder().Build();
|
||||||
|
webHost.Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
// For unit testing
|
||||||
|
public static IWebHostBuilder GetWebHostBuilder()
|
||||||
|
{
|
||||||
|
return new WebHostBuilder()
|
||||||
|
.UseKestrel()
|
||||||
|
.UseIISIntegration()
|
||||||
|
.ConfigureServices(services => services.AddRouting())
|
||||||
|
.Configure(app => app.UseRouter(routes =>
|
||||||
|
{
|
||||||
|
routes.DefaultHandler = new RouteHandler((httpContext) =>
|
||||||
|
{
|
||||||
|
var request = httpContext.Request;
|
||||||
|
return httpContext.Response.WriteAsync($"Verb = {request.Method.ToUpperInvariant()} - Path = {request.Path} - Route values - {string.Join(", ", httpContext.GetRouteData().Values)}");
|
||||||
|
});
|
||||||
|
|
||||||
|
routes.MapGet("api/get/{id}", (request, response, routeData) => response.WriteAsync($"API Get {routeData.Values["id"]}"))
|
||||||
|
.MapMiddlewareRoute("api/middleware", (appBuilder) => appBuilder.Use((httpContext, next) => httpContext.Response.WriteAsync("Middleware!")))
|
||||||
|
.MapRoute(
|
||||||
|
name: "AllVerbs",
|
||||||
|
template: "api/all/{name}/{lastName?}",
|
||||||
|
defaults: new { lastName = "Doe" },
|
||||||
|
constraints: new { lastName = new RegexRouteConstraint(new Regex("[a-zA-Z]{3}", RegexOptions.CultureInvariant, RegexMatchTimeout)) });
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,56 +0,0 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Routing;
|
|
||||||
using Microsoft.AspNetCore.Routing.Constraints;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
|
|
||||||
namespace RoutingSample.Web
|
|
||||||
{
|
|
||||||
public class Startup
|
|
||||||
{
|
|
||||||
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(10);
|
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
|
||||||
{
|
|
||||||
services.AddRouting();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app)
|
|
||||||
{
|
|
||||||
app.UseRouter(routes =>
|
|
||||||
{
|
|
||||||
routes.DefaultHandler = new RouteHandler((c) =>
|
|
||||||
{
|
|
||||||
return c.Response.WriteAsync($"Verb = {c.Request.Method.ToUpperInvariant()} - Path = {c.Request.Path} - Route values - {string.Join(", ", c.GetRouteData().Values)}");
|
|
||||||
});
|
|
||||||
|
|
||||||
routes.MapGet("api/get/{id}", (c) => c.Response.WriteAsync($"API Get {c.GetRouteData().Values["id"]}"));
|
|
||||||
|
|
||||||
routes.MapRoute("api/middleware", (IApplicationBuilder fork) => fork.Use((c, n) => c.Response.WriteAsync("Middleware!")));
|
|
||||||
|
|
||||||
routes.MapRoute(
|
|
||||||
name: "AllVerbs",
|
|
||||||
template: "api/all/{name}/{lastName?}",
|
|
||||||
defaults: new { lastName = "Doe" },
|
|
||||||
constraints: new { lastName = new RegexRouteConstraint(new Regex("[a-zA-Z]{3}", RegexOptions.CultureInvariant, RegexMatchTimeout))});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Main(string[] args)
|
|
||||||
{
|
|
||||||
var host = new WebHostBuilder()
|
|
||||||
.UseKestrel()
|
|
||||||
.UseIISIntegration()
|
|
||||||
.UseStartup<Startup>()
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
host.Run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Routing.Constraints;
|
using Microsoft.AspNetCore.Routing.Constraints;
|
||||||
|
|
@ -41,7 +42,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
/// <param name="template">The route template.</param>
|
/// <param name="template">The route template.</param>
|
||||||
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
||||||
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
||||||
public static IRouteBuilder MapRoute(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action)
|
public static IRouteBuilder MapMiddlewareRoute(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action)
|
||||||
{
|
{
|
||||||
var nested = builder.ApplicationBuilder.New();
|
var nested = builder.ApplicationBuilder.New();
|
||||||
action(nested);
|
action(nested);
|
||||||
|
|
@ -69,9 +70,25 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
/// <param name="template">The route template.</param>
|
/// <param name="template">The route template.</param>
|
||||||
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
||||||
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
||||||
public static IRouteBuilder MapDelete(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action)
|
public static IRouteBuilder MapMiddlewareDelete(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action)
|
||||||
{
|
{
|
||||||
return builder.MapVerb("DELETE", template, action);
|
return builder.MapMiddlewareVerb("DELETE", template, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a route to the <see cref="IRouteBuilder"/> that only matches HTTP DELETE requests for the given
|
||||||
|
/// <paramref name="template"/>, and <paramref name="handler"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder">The <see cref="IRouteBuilder"/>.</param>
|
||||||
|
/// <param name="template">The route template.</param>
|
||||||
|
/// <param name="handler">The route handler.</param>
|
||||||
|
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
||||||
|
public static IRouteBuilder MapDelete(
|
||||||
|
this IRouteBuilder builder,
|
||||||
|
string template,
|
||||||
|
Func<HttpRequest, HttpResponse, RouteData, Task> handler)
|
||||||
|
{
|
||||||
|
return builder.MapVerb("DELETE", template, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -95,9 +112,25 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
/// <param name="template">The route template.</param>
|
/// <param name="template">The route template.</param>
|
||||||
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
||||||
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
||||||
public static IRouteBuilder MapGet(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action)
|
public static IRouteBuilder MapMiddlewareGet(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action)
|
||||||
{
|
{
|
||||||
return builder.MapVerb("GET", template, action);
|
return builder.MapMiddlewareVerb("GET", template, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a route to the <see cref="IRouteBuilder"/> that only matches HTTP GET requests for the given
|
||||||
|
/// <paramref name="template"/>, and <paramref name="handler"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder">The <see cref="IRouteBuilder"/>.</param>
|
||||||
|
/// <param name="template">The route template.</param>
|
||||||
|
/// <param name="handler">The route handler.</param>
|
||||||
|
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
||||||
|
public static IRouteBuilder MapGet(
|
||||||
|
this IRouteBuilder builder,
|
||||||
|
string template,
|
||||||
|
Func<HttpRequest, HttpResponse, RouteData, Task> handler)
|
||||||
|
{
|
||||||
|
return builder.MapVerb("GET", template, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -121,9 +154,25 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
/// <param name="template">The route template.</param>
|
/// <param name="template">The route template.</param>
|
||||||
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
||||||
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
||||||
public static IRouteBuilder MapPost(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action)
|
public static IRouteBuilder MapMiddlewarePost(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action)
|
||||||
{
|
{
|
||||||
return builder.MapVerb("POST", template, action);
|
return builder.MapMiddlewareVerb("POST", template, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a route to the <see cref="IRouteBuilder"/> that only matches HTTP POST requests for the given
|
||||||
|
/// <paramref name="template"/>, and <paramref name="handler"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder">The <see cref="IRouteBuilder"/>.</param>
|
||||||
|
/// <param name="template">The route template.</param>
|
||||||
|
/// <param name="handler">The route handler.</param>
|
||||||
|
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
||||||
|
public static IRouteBuilder MapPost(
|
||||||
|
this IRouteBuilder builder,
|
||||||
|
string template,
|
||||||
|
Func<HttpRequest, HttpResponse, RouteData, Task> handler)
|
||||||
|
{
|
||||||
|
return builder.MapVerb("POST", template, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -147,9 +196,48 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
/// <param name="template">The route template.</param>
|
/// <param name="template">The route template.</param>
|
||||||
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
||||||
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
||||||
public static IRouteBuilder MapPut(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action)
|
public static IRouteBuilder MapMiddlewarePut(this IRouteBuilder builder, string template, Action<IApplicationBuilder> action)
|
||||||
{
|
{
|
||||||
return builder.MapVerb("PUT", template, action);
|
return builder.MapMiddlewareVerb("PUT", template, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a route to the <see cref="IRouteBuilder"/> that only matches HTTP PUT requests for the given
|
||||||
|
/// <paramref name="template"/>, and <paramref name="handler"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder">The <see cref="IRouteBuilder"/>.</param>
|
||||||
|
/// <param name="template">The route template.</param>
|
||||||
|
/// <param name="handler">The route handler.</param>
|
||||||
|
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
||||||
|
public static IRouteBuilder MapPut(
|
||||||
|
this IRouteBuilder builder,
|
||||||
|
string template,
|
||||||
|
Func<HttpRequest, HttpResponse, RouteData, Task> handler)
|
||||||
|
{
|
||||||
|
return builder.MapVerb("PUT", template, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a route to the <see cref="IRouteBuilder"/> that only matches HTTP requests for the given
|
||||||
|
/// <paramref name="verb"/>, <paramref name="template"/>, and <paramref name="handler"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder">The <see cref="IRouteBuilder"/>.</param>
|
||||||
|
/// <param name="verb">The HTTP verb allowed by the route.</param>
|
||||||
|
/// <param name="template">The route template.</param>
|
||||||
|
/// <param name="handler">The route handler.</param>
|
||||||
|
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
||||||
|
public static IRouteBuilder MapVerb(
|
||||||
|
this IRouteBuilder builder,
|
||||||
|
string verb,
|
||||||
|
string template,
|
||||||
|
Func<HttpRequest, HttpResponse, RouteData, Task> handler)
|
||||||
|
{
|
||||||
|
RequestDelegate requestDelegate = (httpContext) =>
|
||||||
|
{
|
||||||
|
return handler(httpContext.Request, httpContext.Response, httpContext.GetRouteData());
|
||||||
|
};
|
||||||
|
|
||||||
|
return builder.MapVerb(verb, template, requestDelegate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -188,7 +276,7 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
/// <param name="template">The route template.</param>
|
/// <param name="template">The route template.</param>
|
||||||
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
/// <param name="action">The action to apply to the <see cref="IApplicationBuilder"/>.</param>
|
||||||
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
/// <returns>A reference to the <paramref name="builder"/> after this operation has completed.</returns>
|
||||||
public static IRouteBuilder MapVerb(
|
public static IRouteBuilder MapMiddlewareVerb(
|
||||||
this IRouteBuilder builder,
|
this IRouteBuilder builder,
|
||||||
string verb,
|
string verb,
|
||||||
string template,
|
string template,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapRoute(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapGet(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapPost(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapPut(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapDelete(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapVerb(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String verb, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapRoute(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapGet(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapPost(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapPut(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapDelete(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"OldTypeId": "public static class Microsoft.AspNetCore.Routing.RequestDelegateRouteBuilderExtensions",
|
||||||
|
"OldMemberId": "public static Microsoft.AspNetCore.Routing.IRouteBuilder MapVerb(this Microsoft.AspNetCore.Routing.IRouteBuilder builder, System.String verb, System.String template, System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder> action)",
|
||||||
|
"Kind": "Removal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
@ -1,21 +1,27 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
{
|
{
|
||||||
public class RoutingSampleTest : IClassFixture<RoutingTestFixture<RoutingSample.Web.Startup>>
|
public class RoutingSampleTest : IDisposable
|
||||||
{
|
{
|
||||||
public RoutingSampleTest(RoutingTestFixture<RoutingSample.Web.Startup> fixture)
|
private readonly HttpClient _client;
|
||||||
{
|
private readonly TestServer _testServer;
|
||||||
Client = fixture.Client;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpClient Client { get; }
|
public RoutingSampleTest()
|
||||||
|
{
|
||||||
|
var webHostBuilder = RoutingSample.Web.Program.GetWebHostBuilder();
|
||||||
|
_testServer = new TestServer(webHostBuilder);
|
||||||
|
_client = _testServer.CreateClient();
|
||||||
|
_client.BaseAddress = new Uri("http://localhost");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Routing_CanRouteRequestDelegate_ToSpecificHttpVerb()
|
public async Task Routing_CanRouteRequestDelegate_ToSpecificHttpVerb()
|
||||||
|
|
@ -24,7 +30,7 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
var message = new HttpRequestMessage(HttpMethod.Get, "api/get/5");
|
var message = new HttpRequestMessage(HttpMethod.Get, "api/get/5");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await Client.SendAsync(message);
|
var response = await _client.SendAsync(message);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
@ -38,7 +44,7 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
var message = new HttpRequestMessage(HttpMethod.Get, "api/middleware");
|
var message = new HttpRequestMessage(HttpMethod.Get, "api/middleware");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await Client.SendAsync(message);
|
var response = await _client.SendAsync(message);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
@ -60,7 +66,7 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
var expectedBody = $"Verb = {httpVerb} - Path = /api/all/Joe/Duf - Route values - [name, Joe], [lastName, Duf]";
|
var expectedBody = $"Verb = {httpVerb} - Path = /api/all/Joe/Duf - Route values - [name, Joe], [lastName, Duf]";
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await Client.SendAsync(message);
|
var response = await _client.SendAsync(message);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
@ -68,5 +74,11 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
var body = await response.Content.ReadAsStringAsync();
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
Assert.Equal(expectedBody, body);
|
Assert.Equal(expectedBody, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_testServer.Dispose();
|
||||||
|
_client.Dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,101 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Routing.FunctionalTests
|
||||||
|
{
|
||||||
|
public class WebHostBuilderExtensionsTest
|
||||||
|
{
|
||||||
|
public static TheoryData<Action<IRouteBuilder>, HttpRequestMessage, string> MatchesRequest
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return new TheoryData<Action<IRouteBuilder>, HttpRequestMessage, string>()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
(rb) => rb.MapGet("greeting/{name}", (req, resp, routeData) => resp.WriteAsync($"Hello! {routeData.Values["name"]}")),
|
||||||
|
new HttpRequestMessage(HttpMethod.Get, "greeting/James"),
|
||||||
|
"Hello! James"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(rb) => rb.MapPost(
|
||||||
|
"greeting/{name}",
|
||||||
|
async (req, resp, routeData) =>
|
||||||
|
{
|
||||||
|
var streamReader = new StreamReader(req.Body);
|
||||||
|
var data = await streamReader.ReadToEndAsync();
|
||||||
|
await resp.WriteAsync($"{routeData.Values["name"]} {data}");
|
||||||
|
}),
|
||||||
|
new HttpRequestMessage(HttpMethod.Post, "greeting/James") { Content = new StringContent("Biography") },
|
||||||
|
"James Biography"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(rb) => rb.MapPut(
|
||||||
|
"greeting/{name}",
|
||||||
|
async (req, resp, routeData) =>
|
||||||
|
{
|
||||||
|
var streamReader = new StreamReader(req.Body);
|
||||||
|
var data = await streamReader.ReadToEndAsync();
|
||||||
|
await resp.WriteAsync($"{routeData.Values["name"]} {data}");
|
||||||
|
}),
|
||||||
|
new HttpRequestMessage(HttpMethod.Put, "greeting/James") { Content = new StringContent("Biography") },
|
||||||
|
"James Biography"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(rb) => rb.MapDelete("greeting/{name}", (req, resp, routeData) => resp.WriteAsync($"Hello! {routeData.Values["name"]}")),
|
||||||
|
new HttpRequestMessage(HttpMethod.Delete, "greeting/James"),
|
||||||
|
"Hello! James"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(rb) => rb.MapVerb(
|
||||||
|
"POST",
|
||||||
|
"greeting/{name}",
|
||||||
|
async (req, resp, routeData) =>
|
||||||
|
{
|
||||||
|
var streamReader = new StreamReader(req.Body);
|
||||||
|
var data = await streamReader.ReadToEndAsync();
|
||||||
|
await resp.WriteAsync($"{routeData.Values["name"]} {data}");
|
||||||
|
}),
|
||||||
|
new HttpRequestMessage(HttpMethod.Post, "greeting/James") { Content = new StringContent("Biography") },
|
||||||
|
"James Biography"
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(MatchesRequest))]
|
||||||
|
public async Task UseRouter_MapGet_MatchesRequest(Action<IRouteBuilder> routeBuilder, HttpRequestMessage request, string expected)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var webhostbuilder = new WebHostBuilder();
|
||||||
|
webhostbuilder
|
||||||
|
.ConfigureServices(services => services.AddRouting())
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseRouter(routeBuilder);
|
||||||
|
});
|
||||||
|
var testServer = new TestServer(webhostbuilder);
|
||||||
|
var client = testServer.CreateClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.SendAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
var actual = await response.Content.ReadAsStringAsync();
|
||||||
|
Assert.Equal(expected, actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -25,19 +25,19 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
return new TheoryData<Action<IRouteBuilder>, Action<HttpContext>>()
|
return new TheoryData<Action<IRouteBuilder>, Action<HttpContext>>()
|
||||||
{
|
{
|
||||||
{ b => { b.MapRoute("api/{id}", NullHandler); }, null },
|
{ b => { b.MapRoute("api/{id}", NullHandler); }, null },
|
||||||
{ b => { b.MapRoute("api/{id}", app => { }); }, null },
|
{ b => { b.MapMiddlewareRoute("api/{id}", app => { }); }, null },
|
||||||
|
|
||||||
{ b => { b.MapDelete("api/{id}", NullHandler); }, c => { c.Request.Method = "DELETE"; } },
|
{ b => { b.MapDelete("api/{id}", NullHandler); }, c => { c.Request.Method = "DELETE"; } },
|
||||||
{ b => { b.MapDelete("api/{id}", app => { }); }, c => { c.Request.Method = "DELETE"; } },
|
{ b => { b.MapMiddlewareDelete("api/{id}", app => { }); }, c => { c.Request.Method = "DELETE"; } },
|
||||||
{ b => { b.MapGet("api/{id}", NullHandler); }, c => { c.Request.Method = "GET"; } },
|
{ b => { b.MapGet("api/{id}", NullHandler); }, c => { c.Request.Method = "GET"; } },
|
||||||
{ b => { b.MapGet("api/{id}", app => { }); }, c => { c.Request.Method = "GET"; } },
|
{ b => { b.MapMiddlewareGet("api/{id}", app => { }); }, c => { c.Request.Method = "GET"; } },
|
||||||
{ b => { b.MapPost("api/{id}", NullHandler); }, c => { c.Request.Method = "POST"; } },
|
{ b => { b.MapPost("api/{id}", NullHandler); }, c => { c.Request.Method = "POST"; } },
|
||||||
{ b => { b.MapPost("api/{id}", app => { }); }, c => { c.Request.Method = "POST"; } },
|
{ b => { b.MapMiddlewarePost("api/{id}", app => { }); }, c => { c.Request.Method = "POST"; } },
|
||||||
{ b => { b.MapPut("api/{id}", NullHandler); }, c => { c.Request.Method = "PUT"; } },
|
{ b => { b.MapPut("api/{id}", NullHandler); }, c => { c.Request.Method = "PUT"; } },
|
||||||
{ b => { b.MapPut("api/{id}", app => { }); }, c => { c.Request.Method = "PUT"; } },
|
{ b => { b.MapMiddlewarePut("api/{id}", app => { }); }, c => { c.Request.Method = "PUT"; } },
|
||||||
|
|
||||||
{ b => { b.MapVerb("PUT", "api/{id}", NullHandler); }, c => { c.Request.Method = "PUT"; } },
|
{ b => { b.MapVerb("PUT", "api/{id}", NullHandler); }, c => { c.Request.Method = "PUT"; } },
|
||||||
{ b => { b.MapVerb("PUT", "api/{id}", app => { }); }, c => { c.Request.Method = "PUT"; } },
|
{ b => { b.MapMiddlewareVerb("PUT", "api/{id}", app => { }); }, c => { c.Request.Method = "PUT"; } },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,29 +73,29 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
return new TheoryData<Action<IRouteBuilder>, Action<HttpContext>>()
|
return new TheoryData<Action<IRouteBuilder>, Action<HttpContext>>()
|
||||||
{
|
{
|
||||||
{ b => { b.MapRoute("api/{id}/extra", NullHandler); }, null },
|
{ b => { b.MapRoute("api/{id}/extra", NullHandler); }, null },
|
||||||
{ b => { b.MapRoute("api/{id}/extra", app => { }); }, null },
|
{ b => { b.MapMiddlewareRoute("api/{id}/extra", app => { }); }, null },
|
||||||
|
|
||||||
{ b => { b.MapDelete("api/{id}", NullHandler); }, c => { c.Request.Method = "GET"; } },
|
{ b => { b.MapDelete("api/{id}", NullHandler); }, c => { c.Request.Method = "GET"; } },
|
||||||
{ b => { b.MapDelete("api/{id}", app => { }); }, c => { c.Request.Method = "PUT"; } },
|
{ b => { b.MapMiddlewareDelete("api/{id}", app => { }); }, c => { c.Request.Method = "PUT"; } },
|
||||||
{ b => { b.MapDelete("api/{id}/extra", NullHandler); }, c => { c.Request.Method = "DELETE"; } },
|
{ b => { b.MapDelete("api/{id}/extra", NullHandler); }, c => { c.Request.Method = "DELETE"; } },
|
||||||
{ b => { b.MapDelete("api/{id}/extra", app => { }); }, c => { c.Request.Method = "DELETE"; } },
|
{ b => { b.MapMiddlewareDelete("api/{id}/extra", app => { }); }, c => { c.Request.Method = "DELETE"; } },
|
||||||
{ b => { b.MapGet("api/{id}", NullHandler); }, c => { c.Request.Method = "PUT"; } },
|
{ b => { b.MapGet("api/{id}", NullHandler); }, c => { c.Request.Method = "PUT"; } },
|
||||||
{ b => { b.MapGet("api/{id}", app => { }); }, c => { c.Request.Method = "POST"; } },
|
{ b => { b.MapMiddlewareGet("api/{id}", app => { }); }, c => { c.Request.Method = "POST"; } },
|
||||||
{ b => { b.MapGet("api/{id}/extra", NullHandler); }, c => { c.Request.Method = "GET"; } },
|
{ b => { b.MapGet("api/{id}/extra", NullHandler); }, c => { c.Request.Method = "GET"; } },
|
||||||
{ b => { b.MapGet("api/{id}/extra", app => { }); }, c => { c.Request.Method = "GET"; } },
|
{ b => { b.MapMiddlewareGet("api/{id}/extra", app => { }); }, c => { c.Request.Method = "GET"; } },
|
||||||
{ b => { b.MapPost("api/{id}", NullHandler); }, c => { c.Request.Method = "MEH"; } },
|
{ b => { b.MapPost("api/{id}", NullHandler); }, c => { c.Request.Method = "MEH"; } },
|
||||||
{ b => { b.MapPost("api/{id}", app => { }); }, c => { c.Request.Method = "DELETE"; } },
|
{ b => { b.MapMiddlewarePost("api/{id}", app => { }); }, c => { c.Request.Method = "DELETE"; } },
|
||||||
{ b => { b.MapPost("api/{id}/extra", NullHandler); }, c => { c.Request.Method = "POST"; } },
|
{ b => { b.MapPost("api/{id}/extra", NullHandler); }, c => { c.Request.Method = "POST"; } },
|
||||||
{ b => { b.MapPost("api/{id}/extra", app => { }); }, c => { c.Request.Method = "POST"; } },
|
{ b => { b.MapMiddlewarePost("api/{id}/extra", app => { }); }, c => { c.Request.Method = "POST"; } },
|
||||||
{ b => { b.MapPut("api/{id}", NullHandler); }, c => { c.Request.Method = "BLEH"; } },
|
{ b => { b.MapPut("api/{id}", NullHandler); }, c => { c.Request.Method = "BLEH"; } },
|
||||||
{ b => { b.MapPut("api/{id}", app => { }); }, c => { c.Request.Method = "HEAD"; } },
|
{ b => { b.MapMiddlewarePut("api/{id}", app => { }); }, c => { c.Request.Method = "HEAD"; } },
|
||||||
{ b => { b.MapPut("api/{id}/extra", NullHandler); }, c => { c.Request.Method = "PUT"; } },
|
{ b => { b.MapPut("api/{id}/extra", NullHandler); }, c => { c.Request.Method = "PUT"; } },
|
||||||
{ b => { b.MapPut("api/{id}/extra", app => { }); }, c => { c.Request.Method = "PUT"; } },
|
{ b => { b.MapMiddlewarePut("api/{id}/extra", app => { }); }, c => { c.Request.Method = "PUT"; } },
|
||||||
|
|
||||||
{ b => { b.MapVerb("PUT", "api/{id}", NullHandler); }, c => { c.Request.Method = "POST"; } },
|
{ b => { b.MapVerb("PUT", "api/{id}", NullHandler); }, c => { c.Request.Method = "POST"; } },
|
||||||
{ b => { b.MapVerb("PUT", "api/{id}", app => { }); }, c => { c.Request.Method = "HEAD"; } },
|
{ b => { b.MapMiddlewareVerb("PUT", "api/{id}", app => { }); }, c => { c.Request.Method = "HEAD"; } },
|
||||||
{ b => { b.MapVerb("PUT", "api/{id}/extra", NullHandler); }, c => { c.Request.Method = "PUT"; } },
|
{ b => { b.MapVerb("PUT", "api/{id}/extra", NullHandler); }, c => { c.Request.Method = "PUT"; } },
|
||||||
{ b => { b.MapVerb("PUT", "api/{id}/extra", app => { }); }, c => { c.Request.Method = "PUT"; } },
|
{ b => { b.MapMiddlewareVerb("PUT", "api/{id}/extra", app => { }); }, c => { c.Request.Method = "PUT"; } },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue