This repository was archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #367] Add extensions on WebHostBuilder for super simple HTTP s…
…ervice application building
- Loading branch information
Showing
6 changed files
with
260 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace RoutingSample.Web | ||
{ | ||
public class TestHttpServices | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var host = new WebHostBuilder() | ||
.UseKestrel() | ||
.UseRouter(routeBuilder => | ||
{ | ||
routeBuilder | ||
.MapGet("hello/{*name}", (req, resp, routeData) => resp.WriteAsync($"Hello, {routeData.Values["name"]}!")) | ||
.MapGet("goodbye/{*name}", (httpContext) => | ||
{ | ||
return httpContext.Response.WriteAsync($"Goodbye, {httpContext.GetRouteData().Values["name"]}!"); | ||
}) | ||
.MapGet("{*name}", (req, resp, routeData) => resp.WriteAsync($"Welcome, {routeData.Values["name"]}!")); | ||
}) | ||
.Build(); | ||
|
||
host.Run(); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Microsoft.AspNetCore.Routing/WebHostBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 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 Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Hosting | ||
{ | ||
public static class WebHostExtensions | ||
{ | ||
/// <summary> | ||
/// Adds routing related services and a <see cref="RouterMiddleware"/> middleware. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IWebHostBuilder"/>.</param> | ||
/// <param name="configureRoutes">An <see cref="Action{RouteBuilder}"/> to configure the provided <see cref="RouteBuilder"/>.</param> | ||
/// <returns></returns> | ||
public static IWebHostBuilder UseRouter(this IWebHostBuilder builder, Action<RouteBuilder> configureRoutes) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
{ | ||
if (configureRoutes == null) | ||
{ | ||
throw new ArgumentNullException(nameof(configureRoutes)); | ||
} | ||
|
||
return builder | ||
.ConfigureServices(services => services.AddRouting()) | ||
.Configure(app => | ||
{ | ||
var routeBuilder = new RouteBuilder(app); | ||
configureRoutes(routeBuilder); | ||
app.UseRouter(routeBuilder.Build()); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
test/Microsoft.AspNetCore.Routing.FunctionalTests/WebHostBuilderExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// 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.TestHost; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Routing.FunctionalTests | ||
{ | ||
public class WebHostBuilderExtensionsTest | ||
{ | ||
public static TheoryData<Action<RouteBuilder>, HttpRequestMessage, string> MatchesRequest | ||
{ | ||
get | ||
{ | ||
return new TheoryData<Action<RouteBuilder>, 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<RouteBuilder> routeBuilder, HttpRequestMessage request, string expected) | ||
{ | ||
// Arrange | ||
var webhostbuilder = new WebHostBuilder(); | ||
webhostbuilder.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); | ||
} | ||
} | ||
} |
1 comment
on commit 2702a9a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kichalla UseRouter should not be on IWebHostBuilder
.
This should be on IAppBuilder.