Skip to content

Commit 21fc3af

Browse files
committed
Fixed error messages in route resolving (closes #206)
1 parent 2314f16 commit 21fc3af

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

src/MyWebApi.Tests/BuildersTests/AttributesTests/ActionAttributesTestBuilderTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public void ChangingRouteToShouldNotThrowExceptionWithActionWithTheAttribute()
8383
.ActionAttributes(attributes => attributes.ChangingRouteTo("/api/test"));
8484
}
8585

86-
8786
[Test]
8887
public void ChangingRouteToShouldNotThrowExceptionWithActionWithTheAttributeAndCasingDifference()
8988
{

src/MyWebApi.Tests/BuildersTests/RoutesTests/RoutesTestBuilderTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,32 @@ public void ToShouldThrowExceptionWithUnsupportedMediaType()
314314
}));
315315
}
316316

317+
[Test]
318+
[ExpectedException(
319+
typeof(RouteAssertionException),
320+
ExpectedMessage = "Expected route '/Route/PostMethodWithModel' to match VoidAction action in RouteController but route does not exist.")]
321+
public void NonExistendRouteShouldThrowProperErrorException()
322+
{
323+
MyWebApi
324+
.Routes()
325+
.ShouldMap("/Route/PostMethodWithModel")
326+
.WithHttpMethod(HttpMethod.Post)
327+
.To<RouteController>(c => c.VoidAction());
328+
}
329+
330+
[Test]
331+
[ExpectedException(
332+
typeof(RouteAssertionException),
333+
ExpectedMessage = "Expected route 'api/Route/GetMethod' to match GetMethod action in RouteController but the 'id' parameter could not be found.")]
334+
public void ToShouldThrowExceptionWithMoreParameters()
335+
{
336+
MyWebApi
337+
.Routes()
338+
.ShouldMap("api/Route/GetMethod")
339+
.WithHttpMethod(HttpMethod.Get)
340+
.To<RouteController>(c => c.GetMethod(1));
341+
}
342+
317343
[Test]
318344
public void ToNotAllowedMethodShouldWorkCorrectly()
319345
{

src/MyWebApi.Tests/Setups/Controllers/RouteController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public IHttpActionResult GetMethod()
3030
return this.Ok();
3131
}
3232

33+
[HttpGet]
34+
public IHttpActionResult GetMethod(int id)
35+
{
36+
return this.Ok();
37+
}
38+
3339
public IHttpActionResult QueryString(string first, int second)
3440
{
3541
return this.Ok();

src/MyWebApi/Builders/Routes/ShouldMapTestBuilder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ private void ValidateRouteInformation<TController>()
306306

307307
expectedRouteValues.Arguments.ForEach(arg =>
308308
{
309+
if (!actualRouteValues.ActionArguments.ContainsKey(arg.Key))
310+
{
311+
this.ThrowNewRouteAssertionException(actual: string.Format(
312+
"the '{0}' parameter could not be found",
313+
arg.Key));
314+
}
315+
309316
var expectedArgumentInfo = arg.Value;
310317
var actualArgumentInfo = actualRouteValues.ActionArguments[arg.Key];
311318
if (Reflection.AreNotDeeplyEqual(expectedArgumentInfo.Value, actualArgumentInfo.Value))

src/MyWebApi/Utilities/RouteResolvers/InternalRouteResolver.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static ResolvedRouteInfo Resolve(HttpConfiguration config, HttpRequestMes
3939
var routeData = config.Routes.GetRouteData(request);
4040
if (routeData == null)
4141
{
42+
request.RequestUri = originalRoute;
4243
return new ResolvedRouteInfo("route does not exist");
4344
}
4445

0 commit comments

Comments
 (0)