Permalink
Browse files

Throw exceptions when Routes don't start with / or contain QueryStrin…

…g chars
  • Loading branch information...
mythz committed Mar 17, 2013
1 parent 2f549bd commit 2aa70b77d46b28862d9e3608ab8ab69e024b1639
@@ -168,8 +168,16 @@ public void RegisterRestPaths(Type requestType)
}
}
+ private static readonly char[] InvalidRouteChars = new[] {'?', '&'};
+
public void RegisterRestPath(RestPath restPath)
{
+ if (!restPath.Path.StartsWith("/"))
+ throw new ArgumentException("Route '{0}' on '{1}' must start with a '/'".Fmt(restPath.Path, restPath.RequestType.Name));
+ if (restPath.Path.IndexOfAny(InvalidRouteChars) != -1)
+ throw new ArgumentException("Route '{0}' on '{1}' contains invalid chars. " +
+ "See https://github.com/ServiceStack/ServiceStack/wiki/Routing for info on valid routes.".Fmt(restPath.Path, restPath.RequestType.Name));
+
List<RestPath> pathsAtFirstMatch;
if (!RestPathMap.TryGetValue(restPath.FirstMatchHashKey, out pathsAtFirstMatch))
{
@@ -1,4 +1,5 @@
-using System.Runtime.Serialization;
+using System;
+using System.Runtime.Serialization;
using NUnit.Framework;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceHost;
@@ -12,7 +13,7 @@ public class JustId : IReturn
public long Id { get; set; }
}
- [Route("/route/{Id}")]
+ [Route("/route/{Id}")]
public class FieldId : IReturn
{
public readonly long Id;
@@ -172,5 +173,14 @@ public void Can_use_non_default_for_nullable_value_types_on_querystring()
var url = new RequestWithValueTypes { Id = 1, Gender2 = Gender.Male }.ToUrl("GET");
Assert.That(url, Is.EqualTo("/route/1?gender2=Male"));
}
- }
+
+ [Test]
+ public void Starting_slash_test_Fails()
+ {
+ var serviceEndpoint = new Uri("http://localhost/api/", UriKind.Absolute);
+ var actionUrl = new Uri(new JustId { Id = 1 }.ToUrl("GET").Substring(), UriKind.Relative);
+
+ Assert.That(new Uri(serviceEndpoint, actionUrl).ToString(), Is.EqualTo("http://localhost/api/route/1"));
+ }
+ }
}
@@ -1,4 +1,5 @@
-using NUnit.Framework;
+using System;
+using NUnit.Framework;
using ServiceStack.ServiceHost.Tests.Support;
namespace ServiceStack.ServiceHost.Tests
@@ -121,5 +122,43 @@ public void Generic_service_can_be_registered_with_closed_types()
Assert.AreEqual(typeof(Generic3<int>).FullName, ((Generic1Response)serviceController.Execute(new Generic3<int>())).Data);
Assert.AreEqual(typeof(Generic3<double>).FullName, ((Generic1Response)serviceController.Execute(new Generic3<double>())).Data);
}
+
+
+ [Route("route/{Id}")]
+ public class NoSlashPrefix : IReturn
+ {
+ public long Id { get; set; }
+ }
+
+ [Route("/route?id={Id}")]
+ public class UsesQueryString : IReturn
+ {
+ public long Id { get; set; }
+ }
+
+ public class MyService : IService
+ {
+ public object Any(NoSlashPrefix request)
+ {
+ return null;
+ }
+
+ public object Any(UsesQueryString request)
+ {
+ return null;
+ }
+ }
+
+ [Test]
+ public void Does_throw_on_invalid_Route_Definitions()
+ {
+ var controller = new ServiceController(() => new[] {typeof (MyService)});
+
+ Assert.Throws<ArgumentException>(
+ () => controller.RegisterRestPaths(typeof(NoSlashPrefix)));
+
+ Assert.Throws<ArgumentException>(
+ () => controller.RegisterRestPaths(typeof(UsesQueryString)));
+ }
}
}

0 comments on commit 2aa70b7

Please sign in to comment.