public void RegisterRestPaths(Type requestType) { var attrs = appHost.GetRouteAttributes(requestType); foreach (RouteAttribute attr in attrs) { var restPath = new RestPath(requestType, attr.Path, attr.Verbs, attr.Summary, attr.Notes); var defaultAttr = attr as FallbackRouteAttribute; if (defaultAttr != null) { if (appHost.Config.FallbackRestPath != null) { throw new NotSupportedException(string.Format( "Config.FallbackRestPath is already defined. Only 1 [FallbackRoute] is allowed.")); } appHost.Config.FallbackRestPath = (httpMethod, pathInfo, filePath) => { var pathInfoParts = RestPath.GetPathPartsForMatching(pathInfo); return(restPath.IsMatch(httpMethod, pathInfoParts) ? restPath : null); }; continue; } if (!restPath.IsValid) { throw new NotSupportedException(string.Format( "RestPath '{0}' on Type '{1}' is not Valid", attr.Path, requestType.GetOperationName())); } RegisterRestPath(restPath); } }
public void Can_deserialize_SimpleType_in_middle_of_path() { var restPath = new RestPath(typeof(SimpleType), "/simple/{Name}/some-other-literal"); var request = restPath.CreateRequest("/simple/HelloWorld!/some-other-literal") as SimpleType; Assert.That(request, Is.Not.Null); Assert.That(request.Name, Is.EqualTo("HelloWorld!")); }
public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary <string, string> requestParams, object requestDto) { string contentType; var pathInfo = !restPath.IsWildCardPath ? GetSanitizedPathInfo(httpReq.PathInfo, out contentType) : httpReq.PathInfo; return(restPath.CreateRequest(pathInfo, requestParams, requestDto)); }
public void Can_deserialize_ComplexType_path() { var restPath = new RestPath(typeof(ComplexType), "/Complex/{Id}/{Name}/Unique/{UniqueId}"); var request = restPath.CreateRequest( "/complex/5/Is Alive/unique/4583B364-BBDC-427F-A289-C2923DEBD547") as ComplexType; Assert.That(request, Is.Not.Null); Assert.That(request.Id, Is.EqualTo(5)); Assert.That(request.Name, Is.EqualTo("Is Alive")); Assert.That(request.UniqueId, Is.EqualTo(new Guid("4583B364-BBDC-427F-A289-C2923DEBD547"))); }
public static object CreateRequest(IRequest httpReq, RestPath restPath) { var dtoFromBinder = GetCustomRequestFromBinder(httpReq, restPath.RequestType); if (dtoFromBinder != null) { return(dtoFromBinder); } var requestParams = httpReq.GetFlattenedRequestParams(); return(CreateRequest(httpReq, restPath, requestParams)); }
public void Can_deserialize_TestRequest_QueryStringSerializer_output() { // Setup new BasicAppHost(new Container(), typeof(TestService).Assembly).Init(); var restPath = new RestPath(typeof(TestRequest), "/service", "GET"); var restHandler = new RestHandler { RestPath = restPath }; var requestString = "ListOfA={ListOfB:[{Property:prop1},{Property:prop2}]}"; NameValueCollection queryString = HttpUtility.ParseQueryString(requestString); var httpReq = new MockHttpRequest("service", "GET", "application/json", "service", queryString, new MemoryStream(), new NameValueCollection()); var request2 = (TestRequest)restHandler.CreateRequest(httpReq, "service"); Assert.That(request2.ListOfA.Count, Is.EqualTo(1)); Assert.That(request2.ListOfA.First().ListOfB.Count, Is.EqualTo(2)); }
public void RegisterRestPaths(Type requestType) { var appHost = ServiceStackHost.Instance; var attrs = appHost.GetRouteAttributes(requestType); foreach (MediaBrowser.Model.Services.RouteAttribute attr in attrs) { var restPath = new RestPath(requestType, attr.Path, attr.Verbs, attr.Summary, attr.Notes); if (!restPath.IsValid) { throw new NotSupportedException(string.Format( "RestPath '{0}' on Type '{1}' is not Valid", attr.Path, requestType.GetOperationName())); } RegisterRestPath(restPath); } }
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.GetOperationName())); } 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.GetOperationName())); } List <RestPath> pathsAtFirstMatch; if (!RestPathMap.TryGetValue(restPath.FirstMatchHashKey, out pathsAtFirstMatch)) { pathsAtFirstMatch = new List <RestPath>(); RestPathMap[restPath.FirstMatchHashKey] = pathsAtFirstMatch; } pathsAtFirstMatch.Add(restPath); }
public virtual IServiceRoutes Add(RestPath restPath) { if (restPath == null || HasExistingRoute(restPath.RequestType, restPath.Path)) { return(this); } //Auto add Route Attributes so they're available in T.ToUrl() extension methods restPath.RequestType .AddAttributes(new RouteAttribute(restPath.Path, restPath.AllowedVerbs) { Priority = restPath.Priority, Summary = restPath.Summary, Notes = restPath.Notes, Matches = restPath.MatchRule }); restPaths.Add(restPath); return(this); }
public SlugRoute(string definition) { this.Definition = definition; var parts = definition.SplitOnFirst(' '); RestPath = new RestPath(typeof(SlugRequest), path: parts[1], verbs: parts[0] == "ANY" ? null : parts[0]); }
public static void SetRoute(this IRequest req, RestPath route) { req.Items[Keywords.Route] = route; }
public void RegisterRestPaths(Type requestType) { var attrs = appHost.GetRouteAttributes(requestType); foreach (RouteAttribute attr in attrs) { var restPath = new RestPath(requestType, attr.Path, attr.Verbs, attr.Summary, attr.Notes); var defaultAttr = attr as FallbackRouteAttribute; if (defaultAttr != null) { if (appHost.Config.FallbackRestPath != null) throw new NotSupportedException(string.Format( "Config.FallbackRestPath is already defined. Only 1 [FallbackRoute] is allowed.")); appHost.Config.FallbackRestPath = (httpMethod, pathInfo, filePath) => { var pathInfoParts = RestPath.GetPathPartsForMatching(pathInfo); return restPath.IsMatch(httpMethod, pathInfoParts) ? restPath : null; }; continue; } if (!restPath.IsValid) throw new NotSupportedException(string.Format( "RestPath '{0}' on Type '{1}' is not Valid", attr.Path, requestType.GetOperationName())); RegisterRestPath(restPath); } }
public void Can_include_StaticValuesForVariables_request() { using (JsConfig.With(includePublicFields: true)) { var variableBindings = new Dictionary<string, object> { {"Id", 5}, {"Name", "Is Alive"} }; var restPath = new RestPath(typeof (ComplexTypeWithFields), "/Complex/Unique/{UniqueId}", null, null, null, variableBindings); var request = restPath.CreateRequest( "/complex/unique/4583B364-BBDC-427F-A289-C2923DEBD547") as ComplexTypeWithFields; Assert.That(request, Is.Not.Null); Assert.That(request.Id, Is.EqualTo(5)); Assert.That(request.Name, Is.EqualTo("Is Alive")); Assert.That(request.UniqueId, Is.EqualTo(new Guid("4583B364-BBDC-427F-A289-C2923DEBD547"))); } }
public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary <string, string> requestParams) { var requestDto = CreateContentTypeRequest(httpReq, restPath.RequestType, httpReq.ContentType); return(CreateRequest(httpReq, restPath, requestParams, requestDto)); }
public void Can_match_lowercase_http_method() { var restPath = new RestPath(typeof(ComplexType), "/Complex/{Id}/{Name}/Unique/{UniqueId}", "PUT"); var withPathInfoParts = RestPath.GetPathPartsForMatching("/complex/5/Is Alive/unique/4583B364-BBDC-427F-A289-C2923DEBD547"); Assert.That(restPath.IsMatch("put", withPathInfoParts)); }
/// <summary> /// Get Best Matching Route. /// </summary> /// <param name="httpMethod"></param> /// <param name="pathInfo"></param> /// <param name="httpReq">If not null, ensures any Route matches any [Route(Matches)]</param> /// <returns></returns> public RestPath GetRestPathForRequest(string httpMethod, string pathInfo, IHttpRequest httpReq) { var matchUsingPathParts = RestPath.GetPathPartsForMatching(pathInfo); List <RestPath> firstMatches; RestPath bestMatch = null; var yieldedHashMatches = RestPath.GetFirstMatchHashKeys(matchUsingPathParts); foreach (var potentialHashMatch in yieldedHashMatches) { if (!this.RestPathMap.TryGetValue(potentialHashMatch, out firstMatches)) { continue; } var bestScore = -1; foreach (var restPath in firstMatches) { var matchScore = 0; //Handle [Route(Matches)] if (httpReq != null) { var matchFn = restPath.GetRequestRule(); if (matchFn != null) { var validRoute = matchFn(httpReq); if (!validRoute) { continue; } matchScore = 1; } } var score = restPath.MatchScore(httpMethod, matchUsingPathParts) + matchScore; if (score > bestScore) { bestScore = score; bestMatch = restPath; } } if (bestScore > 0) { return(bestMatch); } } var yieldedWildcardMatches = RestPath.GetFirstMatchWildCardHashKeys(matchUsingPathParts); foreach (var potentialHashMatch in yieldedWildcardMatches) { if (!this.RestPathMap.TryGetValue(potentialHashMatch, out firstMatches)) { continue; } var bestScore = -1; foreach (var restPath in firstMatches) { var score = restPath.MatchScore(httpMethod, matchUsingPathParts); if (score > bestScore) { bestScore = score; bestMatch = restPath; } } if (bestScore > 0) { return(bestMatch); } } return(null); }
public void Cannot_include_invalidStaticValuesForVariables_request() { using (JsConfig.With(includePublicFields: true)) { var variableBindings = new Dictionary<string, object> { {"IdX", 5}, {"Name", "Is Alive"} }; var restPath = new RestPath(typeof (ComplexTypeWithFields), "/Complex/Unique/{UniqueId}", null, null, null, variableBindings); } }
public static void SetRoute(this IRequest req, RestPath route) { req.Items["__route"] = route; }
private static void AssertMatch(string definitionPath, string requestPath, string firstMatchHashKey, BbcMusicRequest expectedRequest) { var restPath = new RestPath(typeof(BbcMusicRequest), definitionPath); var reqestTestPath = RestPath.GetPathPartsForMatching(requestPath); Assert.That(restPath.IsMatch("GET", reqestTestPath), Is.True); Assert.That(firstMatchHashKey, Is.EqualTo(restPath.FirstMatchHashKey)); var actualRequest = restPath.CreateRequest(requestPath) as BbcMusicRequest; Assert.That(actualRequest, Is.Not.Null); Assert.That(actualRequest.mbz_guid, Is.EqualTo(expectedRequest.mbz_guid)); Assert.That(actualRequest.release_type, Is.EqualTo(expectedRequest.release_type)); Assert.That(actualRequest.content_type, Is.EqualTo(expectedRequest.content_type)); }
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.GetOperationName())); 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.GetOperationName())); List<RestPath> pathsAtFirstMatch; if (!RestPathMap.TryGetValue(restPath.FirstMatchHashKey, out pathsAtFirstMatch)) { pathsAtFirstMatch = new List<RestPath>(); RestPathMap[restPath.FirstMatchHashKey] = pathsAtFirstMatch; } pathsAtFirstMatch.Add(restPath); }
private static void AssertMatch(string definitionPath, string requestPath, string firstMatchHashKey, RackSpaceRequest expectedRequest) { var restPath = new RestPath(typeof(RackSpaceRequest), definitionPath); var reqestTestPath = RestPath.GetPathPartsForMatching(requestPath); Assert.That(restPath.IsMatch("GET", reqestTestPath), Is.True); Assert.That(firstMatchHashKey, Is.EqualTo(restPath.FirstMatchHashKey)); var actualRequest = restPath.CreateRequest(requestPath) as RackSpaceRequest; Assert.That(actualRequest, Is.Not.Null); Assert.That(actualRequest.version, Is.EqualTo(expectedRequest.version)); Assert.That(actualRequest.id, Is.EqualTo(expectedRequest.id)); Assert.That(actualRequest.resource_type, Is.EqualTo(expectedRequest.resource_type)); Assert.That(actualRequest.action, Is.EqualTo(expectedRequest.action)); }
private MethodDescription FormateMethodDescription(RestPath restPath, Dictionary<string, SwaggerModel> models) { var verbs = new List<string>(); var summary = restPath.Summary; var notes = restPath.Notes; if (restPath.AllowsAllVerbs) { verbs.AddRange(new[] { "GET", "POST", "PUT", "DELETE" }); } else verbs.AddRange(restPath.AllowedVerbs.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)); var nickName = nicknameCleanerRegex.Replace(restPath.Path, ""); var md = new MethodDescription { Path = restPath.Path, Description = summary, Operations = verbs.Select(verb => new MethodOperation { HttpMethod = verb, Nickname = verb.ToLowerInvariant() + nickName, Summary = summary, Notes = notes, Parameters = ParseParameters(verb, restPath.RequestType, models, restPath.Path), ResponseClass = GetResponseClass(restPath, models), ErrorResponses = GetMethodResponseCodes(restPath.RequestType) }).ToList() }; return md; }
private static void AssertMatch(string definitionPath, string requestPath, string firstMatchHashKey, SlugRequest expectedRequest, int expectedScore) { var restPath = new RestPath(typeof(SlugRequest), definitionPath); var requestTestPath = RestPath.GetPathPartsForMatching(requestPath); Assert.That(restPath.IsMatch("GET", requestTestPath), Is.True); Assert.That(firstMatchHashKey, Is.EqualTo(restPath.FirstMatchHashKey)); var actualRequest = restPath.CreateRequest(requestPath) as SlugRequest; Assert.That(actualRequest, Is.Not.Null); Assert.That(actualRequest.Slug, Is.EqualTo(expectedRequest.Slug)); Assert.That(actualRequest.Version, Is.EqualTo(expectedRequest.Version)); Assert.That(actualRequest.Options, Is.EqualTo(expectedRequest.Options)); Assert.That(restPath.MatchScore("GET", requestTestPath), Is.EqualTo(expectedScore)); }
public IRestPath GetRestPathForRequest(string httpMethod, string pathInfo) { var matchUsingPathParts = RestPath.GetPathPartsForMatching(pathInfo); List <RestPath> firstMatches; var yieldedHashMatches = RestPath.GetFirstMatchHashKeys(matchUsingPathParts); foreach (var potentialHashMatch in yieldedHashMatches) { if (!this.RestPathMap.TryGetValue(potentialHashMatch, out firstMatches)) { continue; } var bestScore = -1; foreach (var restPath in firstMatches) { var score = restPath.MatchScore(httpMethod, matchUsingPathParts); if (score > bestScore) { bestScore = score; } } if (bestScore > 0) { foreach (var restPath in firstMatches) { if (bestScore == restPath.MatchScore(httpMethod, matchUsingPathParts)) { return(restPath); } } } } var yieldedWildcardMatches = RestPath.GetFirstMatchWildCardHashKeys(matchUsingPathParts); foreach (var potentialHashMatch in yieldedWildcardMatches) { if (!this.RestPathMap.TryGetValue(potentialHashMatch, out firstMatches)) { continue; } var bestScore = -1; foreach (var restPath in firstMatches) { var score = restPath.MatchScore(httpMethod, matchUsingPathParts); if (score > bestScore) { bestScore = score; } } if (bestScore > 0) { foreach (var restPath in firstMatches) { if (bestScore == restPath.MatchScore(httpMethod, matchUsingPathParts)) { return(restPath); } } } } return(null); }
private static void AssertNoMatch(string definitionPath, string requestPath) { var restPath = new RestPath(typeof(SlugRequest), definitionPath); var requestTestPath = RestPath.GetPathPartsForMatching(requestPath); Assert.That(restPath.IsMatch("GET", requestTestPath), Is.False); }