public void Match_ReturnsTrue_IfODataPathCanBeParsed() { // Arrange var routeRequest = new TestRouteRequest(HttpMethod.Get, "http://any/odata/$metadata"); string expectedRoute = "$metadata"; var values = new Dictionary <string, object>() { { "odataPath", expectedRoute } }; var constraint = CreatePathRouteConstraint(); // Act & Assert Assert.True(ConstraintMatch(constraint, routeRequest, values, RouteDirection.UriResolution)); #if NETCORE Assert.Equal(_routeName, routeRequest.InnerRequest.ODataFeature().RouteName); Assert.NotNull(routeRequest.InnerRequest.ODataFeature().Path); Assert.Equal(expectedRoute, routeRequest.InnerRequest.ODataFeature().Path.ToString()); #else Assert.Equal("Metadata", values["controller"]); Assert.Same(_model, routeRequest.InnerRequest.GetModel()); Assert.Equal(_conventions, routeRequest.InnerRequest.GetRoutingConventions()); Assert.Same(_pathHandler, routeRequest.InnerRequest.GetPathHandler()); Assert.Equal(_routeName, routeRequest.InnerRequest.ODataProperties().RouteName); Assert.NotNull(routeRequest.InnerRequest.ODataProperties().Path); Assert.Equal(expectedRoute, routeRequest.InnerRequest.ODataProperties().Path.ToString()); #endif }
public void Match_DeterminesExpectedServiceRoot_ForMetadataWithEscapedSeparator(string prefixString) { // Arrange var originalRoot = "http://any/" + prefixString; var expectedRoot = originalRoot; if (!String.IsNullOrEmpty(prefixString)) { originalRoot += "%2F"; // Escaped '/' } var pathHandler = new TestPathHandler(); var routeRequest = new TestRouteRequest(HttpMethod.Get, originalRoot + "$metadata") { PathHandler = pathHandler, }; var constraint = CreatePathRouteConstraint(); var values = new Dictionary <string, object> { { ODataRouteConstants.ODataPath, "$metadata" }, }; // Act var matched = ConstraintMatch(constraint, routeRequest, values, RouteDirection.UriResolution); // Assert Assert.True(matched); Assert.NotNull(pathHandler.ServiceRoot); Assert.Equal(expectedRoot, pathHandler.ServiceRoot); Assert.NotNull(pathHandler.ODataPath); Assert.Equal("$metadata", pathHandler.ODataPath); }
public void Match_ReturnsFalse_IfODataPathCannotBeParsed() { // Arrange var routeRequest = new TestRouteRequest(HttpMethod.Get, "http://any/NotAnODataPath"); var values = new Dictionary <string, object>() { { "odataPath", "NotAnODataPath" } }; var constraint = CreatePathRouteConstraint(); // Act & Assert Assert.False(ConstraintMatch(constraint, routeRequest, values, RouteDirection.UriResolution)); }
public void Match_DeterminesExpectedServiceRoot_ForFunctionCallWithEscapedSeparator( string prefixString, string oDataString) { // Arrange var originalRoot = "http://any/" + prefixString; var expectedRoot = originalRoot; if (!String.IsNullOrEmpty(prefixString)) { originalRoot += "%2F"; // Escaped '/' } var builder = new ODataModelBuilder(); builder.Function("Unbound").Returns <string>().Parameter <string>("p0"); var model = builder.GetEdmModel(); var pathHandler = new TestPathHandler(); var oDataPath = String.Format("Unbound(p0='{0}')", oDataString); var routeRequest = new TestRouteRequest(HttpMethod.Get, originalRoot + oDataPath) { PathHandler = pathHandler, Model = model, }; var constraint = CreatePathRouteConstraint(); var values = new Dictionary <string, object> { { ODataRouteConstants.ODataPath, Uri.UnescapeDataString(oDataPath) }, }; // Act var matched = ConstraintMatch(constraint, routeRequest, values, RouteDirection.UriResolution); // Assert Assert.True(matched); Assert.NotNull(pathHandler.ServiceRoot); Assert.Equal(expectedRoot, pathHandler.ServiceRoot); Assert.NotNull(pathHandler.ODataPath); Assert.Equal(oDataPath, pathHandler.ODataPath); }
/// <summary> /// Test method to call constraint.Match using the proper arguments for each platform. /// </summary> /// <param name="constraint">The constraint object.</param> /// <param name="routeRequest">The abstracted request.</param> /// <param name="direction">The abstracted route direction.</param> /// <returns>Result from constraint.Match,</returns> private bool ConstraintMatch(ODataPathRouteConstraint constraint, TestRouteRequest routeRequest, Dictionary <string, object> values, RouteDirection direction) { #if NETCORE IRouteBuilder config = RoutingConfigurationFactory.Create(); if (routeRequest?.PathHandler != null && routeRequest?.Model != null) { config.MapODataServiceRoute(_routeName, "", builder => builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.PathHandler) .AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.Model)); } else if (routeRequest?.PathHandler != null) { config.MapODataServiceRoute(_routeName, "", builder => builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.PathHandler)); } else if (routeRequest?.Model != null) { config.MapODataServiceRoute(_routeName, "", builder => builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.Model)); } else { config.MapODataServiceRoute(_routeName, "", builder => { }); } HttpRequest request = (routeRequest != null) ? RequestFactory.Create(routeRequest.Method, routeRequest.Uri, config, _routeName) : RequestFactory.Create(); // The RequestFactory will create a request container which most tests want but for checking the constraint, // we don't want a request container before the test runs since Match() creates one. request.DeleteRequestContainer(true); if (routeRequest != null) { routeRequest.InnerRequest = request; } AspNetCore.Routing.RouteDirection routeDirection = (direction == RouteDirection.UriResolution) ? AspNetCore.Routing.RouteDirection.IncomingRequest : AspNetCore.Routing.RouteDirection.UrlGeneration; RouteValueDictionary routeValues = new RouteValueDictionary(values); return(constraint.Match(request.HttpContext, null, null, routeValues, routeDirection)); #else HttpRequestMessage request = (routeRequest != null) ? new HttpRequestMessage(routeRequest.Method, routeRequest.Uri) : new HttpRequestMessage(); var httpRouteCollection = new HttpRouteCollection { { _routeName, new HttpRoute() }, }; var configuration = new HttpConfiguration(httpRouteCollection); if (routeRequest != null && routeRequest.PathHandler != null && routeRequest.Model != null) { configuration.CreateODataRootContainer(_routeName, builder => builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.PathHandler) .AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.Model)); } else if (routeRequest != null && routeRequest.PathHandler != null) { configuration.CreateODataRootContainer(_routeName, builder => builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.PathHandler)); } else if (routeRequest != null && routeRequest.Model != null) { configuration.CreateODataRootContainer(_routeName, builder => builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.Model)); } else { PerRouteContainer perRouteContainer = configuration.GetPerRouteContainer() as PerRouteContainer; perRouteContainer.SetODataRootContainer(_routeName, _rootContainer); } request.SetConfiguration(configuration); if (routeRequest != null) { routeRequest.InnerRequest = request; } HttpRouteDirection routeDirection = (direction == RouteDirection.UriResolution) ? HttpRouteDirection.UriResolution : HttpRouteDirection.UriGeneration; return(constraint.Match(request, null, null, values, routeDirection)); #endif }