public void IgnoreRoute_GetVirtualPathReturnsNull()
        {
            // Arrange
            DomainHttpRoute           route      = new DomainHttpRoute("myDomain", "api/{controller}/{action}", new { controller = "SomeValue", action = "SomeAction" });
            HostedHttpRouteCollection collection = new HostedHttpRouteCollection(new RouteCollection());

            collection.IgnoreRoute("domainRoute", route.RouteTemplate);
            HttpRequestMessage       request     = CreateHttpRequestMessageWithContext();
            HttpRouteValueDictionary routeValues = new HttpRouteValueDictionary()
            {
                { "controller", "controllerName" },
                { "action", "actionName" },
                { "httproute", true }
            };

            request.SetRouteData(new HttpRouteData(route, routeValues));

            // Act
            IHttpVirtualPathData httpvPathData = collection.GetVirtualPath(request, "domainRoute", routeValues);

            // Assert
            // Altough it contains the ignore route, GetVirtualPath from the ignored route will always return null.
            Assert.Equal(collection.Count, 1);
            Assert.Null(httpvPathData);
        }
        public void CustomHttpRouteGetVitualPathRunsCustomHttpRoute()
        {
            // Arrange
            DomainHttpRoute route = new DomainHttpRoute(
                "myDomain",
                "api/{controller}/{action}",
                new { controller = "SomeValue", action = "SomeAction" }
                );
            HostedHttpRouteCollection collection = new HostedHttpRouteCollection(
                new RouteCollection()
                );

            collection.Add("domainRoute", route);
            HttpRequestMessage       request     = CreateHttpRequestMessageWithContext();
            HttpRouteValueDictionary routeValues = new HttpRouteValueDictionary()
            {
                { "controller", "controllerName" },
                { "action", "actionName" },
                { "httproute", true }
            };

            request.SetRouteData(new HttpRouteData(route, routeValues));

            // Act
            IHttpVirtualPathData httpvPathData = collection.GetVirtualPath(
                request,
                "domainRoute",
                routeValues
                );

            // Assert
            Assert.NotNull(httpvPathData);
            Assert.Equal("/api/controllerName/actionNameFromDomain", httpvPathData.VirtualPath);
        }
        public void ConvertHttpRouteDataToRouteDataRunsCustomHttpRoute()
        {
            // Arrange
            DomainHttpRoute route = new DomainHttpRoute(
                "myDomain",
                "api/{controller}/{action}",
                new { controller = "Values", action = "GetTenant" }
                );
            HostedHttpRouteCollection collection = new HostedHttpRouteCollection(
                new RouteCollection()
                );

            collection.Add("domainRoute", route);
            HttpRequestMessage request       = CreateHttpRequestMessageWithContext();
            IHttpRouteData     httpRouteData = collection.GetRouteData(request);

            // Act
            RouteData routeData = httpRouteData.ToRouteData();

            // Assert
            Assert.NotNull(routeData.Values);
            Assert.Equal(3, routeData.Values.Count);
            Assert.Equal("controllerName", routeData.Values["controller"]);
            Assert.Equal("actionName", routeData.Values["action"]);
            Assert.Equal("myDomain", routeData.Values["domain"]);
        }
Beispiel #4
0
        private static UrlHelper GetUrlHelperForMixedApp(WhichRoute whichRoute, out RouteCollection routes, out RequestContext requestContext)
        {
            routes = new RouteCollection();

            HttpControllerContext cc = new HttpControllerContext();

            cc.Request = new HttpRequestMessage();
            var mockHttpContext = new Mock <HttpContextBase>();
            var mockHttpRequest = new Mock <HttpRequestBase>();

            mockHttpRequest.SetupGet <string>(x => x.ApplicationPath).Returns("/SOMEAPP/");
            var mockHttpResponse = new Mock <HttpResponseBase>();

            mockHttpResponse.Setup <string>(x => x.ApplyAppPathModifier(It.IsAny <string>())).Returns <string>(x => "$APP$" + x);
            mockHttpContext.SetupGet <HttpRequestBase>(x => x.Request).Returns(mockHttpRequest.Object);
            mockHttpContext.SetupGet <HttpResponseBase>(x => x.Response).Returns(mockHttpResponse.Object);
            cc.Request.Properties["MS_HttpContext"] = mockHttpContext.Object;

            // Set up routes
            var   hostedRoutes = new HostedHttpRouteCollection(routes);
            Route apiRoute1    = routes.MapHttpRoute("apiroute1", "api/{controller}/{id}", new { action = "someaction" });
            Route apiRoute2    = routes.MapHttpRoute("apiroute2", "api/{controller}/{action}", new { id = 789 });
            Route webRoute1    = routes.MapRoute("webroute1", "{controller}/{action}/{id}");

            cc.Configuration = new HttpConfiguration(hostedRoutes);

            RouteData routeData = new RouteData();

            routeData.Values.Add("controller", "people");
            routeData.Values.Add("id", "123");

            // Specify which route we came in on (e.g. what request matching the incoming URL) because
            // it can affect the generated URL due to the ambient route data.
            switch (whichRoute)
            {
            case WhichRoute.ApiRoute1:
                routeData.Route = apiRoute1;
                break;

            case WhichRoute.ApiRoute2:
                routeData.Route = apiRoute2;
                break;

            case WhichRoute.WebRoute1:
                routeData.Route = webRoute1;
                break;

            default:
                throw new ArgumentException("Invalid route specified.", "whichRoute");
            }
            cc.RouteData = new HostedHttpRouteData(routeData);

            requestContext = new RequestContext(mockHttpContext.Object, routeData);

            return(cc.Url);
        }
        public void CreateRoute_ValidatesConstraintType_InvalidType()
        {
            // Arrange
            var routes = new HostedHttpRouteCollection(new RouteCollection());

            var constraint  = new Uri("http://localhost/");
            var constraints = new HttpRouteValueDictionary();

            constraints.Add("custom", constraint);

            string expectedMessage =
                "The constraint entry 'custom' on the route with route template '{controller}/{id}' " +
                "must have a string value or be of a type which implements 'System.Web.Http.Routing.IHttpRouteConstraint' or 'System.Web.Routing.IRouteConstraint'.";

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => routes.CreateRoute("{controller}/{id}", null, constraints), expectedMessage);
        }
        private static UrlHelper GetUrlHelperForMixedApp(WhichRoute whichRoute, out RouteCollection routes, out RequestContext requestContext)
        {
            routes = new RouteCollection();

            HttpRequestMessage request = new HttpRequestMessage();
            var mockHttpContext = new Mock<HttpContextBase>();
            var mockHttpRequest = new Mock<HttpRequestBase>();
            mockHttpRequest.SetupGet<string>(x => x.ApplicationPath).Returns("/SOMEAPP/");
            var mockHttpResponse = new Mock<HttpResponseBase>();
            mockHttpResponse.Setup<string>(x => x.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(x => "$APP$" + x);
            mockHttpContext.SetupGet<HttpRequestBase>(x => x.Request).Returns(mockHttpRequest.Object);
            mockHttpContext.SetupGet<HttpResponseBase>(x => x.Response).Returns(mockHttpResponse.Object);
            request.Properties["MS_HttpContext"] = mockHttpContext.Object;

            // Set up routes
            var hostedRoutes = new HostedHttpRouteCollection(routes);
            Route apiRoute1 = routes.MapHttpRoute("apiroute1", "api/{controller}/{id}", new { action = "someaction" });
            Route apiRoute2 = routes.MapHttpRoute("apiroute2", "api/{controller}/{action}", new { id = 789 });
            Route webRoute1 = routes.MapRoute("webroute1", "{controller}/{action}/{id}");
            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = new HttpConfiguration(hostedRoutes);

            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "people");
            routeData.Values.Add("id", "123");

            // Specify which route we came in on (e.g. what request matching the incoming URL) because
            // it can affect the generated URL due to the ambient route data.
            switch (whichRoute)
            {
                case WhichRoute.ApiRoute1:
                    routeData.Route = apiRoute1;
                    break;
                case WhichRoute.ApiRoute2:
                    routeData.Route = apiRoute2;
                    break;
                case WhichRoute.WebRoute1:
                    routeData.Route = webRoute1;
                    break;
                default:
                    throw new ArgumentException("Invalid route specified.", "whichRoute");
            }
            request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HostedHttpRouteData(routeData);

            requestContext = new RequestContext(mockHttpContext.Object, routeData);

            return request.GetUrlHelper();
        }
        public void CustomHttpRouteGetVitualPathRunsCustomHttpRoute()
        {
            // Arrange
            DomainHttpRoute route = new DomainHttpRoute("myDomain", "api/{controller}/{action}", new { controller = "SomeValue", action = "SomeAction" });
            HostedHttpRouteCollection collection = new HostedHttpRouteCollection(new RouteCollection());
            collection.Add("domainRoute", route);
            HttpRequestMessage request = CreateHttpRequestMessageWithContext();
            HttpRouteValueDictionary routeValues = new HttpRouteValueDictionary()
                {
                    {"controller", "controllerName"},
                    {"action", "actionName"},
                    {"httproute", true}
                };

            request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HttpRouteData(route, routeValues);
            
            // Act
            IHttpVirtualPathData httpvPathData = collection.GetVirtualPath(request, "domainRoute", routeValues);

            // Assert
            Assert.NotNull(httpvPathData);
            Assert.Equal("/api/controllerName/actionNameFromDomain", httpvPathData.VirtualPath);
        }
        public void ConvertHttpRouteDataToRouteDataRunsCustomHttpRoute()
        {
            // Arrange
            DomainHttpRoute route = new DomainHttpRoute("myDomain", "api/{controller}/{action}", new { controller = "Values", action = "GetTenant" });
            HostedHttpRouteCollection collection = new HostedHttpRouteCollection(new RouteCollection());
            collection.Add("domainRoute", route);
            HttpRequestMessage request = CreateHttpRequestMessageWithContext();
            IHttpRouteData httpRouteData = collection.GetRouteData(request);
            
            // Act
            RouteData routeData = httpRouteData.ToRouteData();

            // Assert
            Assert.NotNull(routeData.Values);
            Assert.Equal(3, routeData.Values.Count);
            Assert.Equal("controllerName", routeData.Values["controller"]);
            Assert.Equal("actionName", routeData.Values["action"]);
            Assert.Equal("myDomain", routeData.Values["domain"]);
        }
 public HostedHttpRouteCollectionTest()
 {
     _aspNetRoutes = new RouteCollection();
     _webApiRoutes = new HostedHttpRouteCollection(_aspNetRoutes);
 }
Beispiel #10
0
 public HostedHttpRouteCollectionTest()
 {
     _aspNetRoutes = new RouteCollection();
     _webApiRoutes = new HostedHttpRouteCollection(_aspNetRoutes);
 }
        public async Task WebHost_Batching_WithSpecialCharactersInUrl()
        {
            // Arrange
            var handler = new SuccessMessageHandler();

            var routeCollection = new HostedHttpRouteCollection(new RouteCollection(), "/");
            routeCollection.Add("default", routeCollection.CreateRoute(
                "values/  space",
                defaults: null,
                constraints: null,
                dataTokens: null,
                handler: handler));

            var configuration = new HttpConfiguration(routeCollection);

            var server = new HttpServer(configuration);

            var batchHandler = new DefaultHttpBatchHandler(server);
            var request = new HttpRequestMessage
            {
                Content = new MultipartContent("mixed")
                {
                    new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "http://contoso.com/values/  space"))
                }
            };

            // Arrange
            var response = await batchHandler.ProcessBatchAsync(request, CancellationToken.None);

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.True(handler.IsCalled);
        }