From 4030be585db1620a59e4105cbcae214351b3a6fb Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Mon, 20 Apr 2015 10:54:33 -0700 Subject: [PATCH] #265 Add implicit converters between string and PathString. --- src/Microsoft.AspNet.Http.Core/PathString.cs | 18 ++++++++++++++++++ .../MapPathMiddlewareTests.cs | 16 ++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.AspNet.Http.Core/PathString.cs b/src/Microsoft.AspNet.Http.Core/PathString.cs index 5e6f03ec70..11249afb91 100644 --- a/src/Microsoft.AspNet.Http.Core/PathString.cs +++ b/src/Microsoft.AspNet.Http.Core/PathString.cs @@ -236,5 +236,23 @@ namespace Microsoft.AspNet.Http { return left.Add(right); } + + /// + /// Implicitly creates a new PathString from the given string. + /// + /// + public static implicit operator PathString(string s) + { + return new PathString(s); + } + + /// + /// Implicitly calls ToString(). + /// + /// + public static implicit operator string(PathString path) + { + return path.ToString(); + } } } diff --git a/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs b/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs index 071e8fbdaf..a54a89d09e 100644 --- a/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs @@ -60,7 +60,7 @@ namespace Microsoft.AspNet.Builder.Extensions { HttpContext context = CreateRequest(basePath, requestPath); var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(new PathString(matchPath), UseSuccess); + builder.Map(matchPath, UseSuccess); var app = builder.Build(); app.Invoke(context).Wait(); @@ -81,7 +81,7 @@ namespace Microsoft.AspNet.Builder.Extensions { HttpContext context = CreateRequest(basePath, requestPath); var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(new PathString(matchPath), subBuilder => subBuilder.Run(Success)); + builder.Map(matchPath, subBuilder => subBuilder.Run(Success)); var app = builder.Build(); app.Invoke(context).Wait(); @@ -96,7 +96,7 @@ namespace Microsoft.AspNet.Builder.Extensions [InlineData("/foo/cho/")] public void MatchPathWithTrailingSlashThrowsException(string matchPath) { - Should.Throw(() => new ApplicationBuilder(serviceProvider: null).Map(new PathString(matchPath), map => { }).Build()); + Should.Throw(() => new ApplicationBuilder(serviceProvider: null).Map(matchPath, map => { }).Build()); } [Theory] @@ -111,7 +111,7 @@ namespace Microsoft.AspNet.Builder.Extensions { HttpContext context = CreateRequest(basePath, requestPath); var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(new PathString(matchPath), UseNotImplemented); + builder.Map(matchPath, UseNotImplemented); builder.Run(Success); var app = builder.Build(); app.Invoke(context).Wait(); @@ -133,7 +133,7 @@ namespace Microsoft.AspNet.Builder.Extensions { HttpContext context = CreateRequest(basePath, requestPath); var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(new PathString(matchPath), UseNotImplemented); + builder.Map(matchPath, UseNotImplemented); builder.Run(Success); var app = builder.Build(); app.Invoke(context).Wait(); @@ -147,12 +147,12 @@ namespace Microsoft.AspNet.Builder.Extensions public void ChainedRoutes_Success() { var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(new PathString("/route1"), map => + builder.Map("/route1", map => { - map.Map(new PathString("/subroute1"), UseSuccess); + map.Map("/subroute1", UseSuccess); map.Run(NotImplemented); }); - builder.Map(new PathString("/route2/subroute2"), UseSuccess); + builder.Map("/route2/subroute2", UseSuccess); var app = builder.Build(); HttpContext context = CreateRequest(string.Empty, "/route1");