Exemplo n.º 1
0
        public void CreateRoute_CreatesHostedHttpRoute()
        {
            var defaults = new HttpRouteValueDictionary {
                { "Foo", "Bar" }
            };
            var constraints = new HttpRouteValueDictionary {
                { "Bar", "Baz" }
            };
            var dataTokens = new HttpRouteValueDictionary {
                { "Baz", "Biff" }
            };
            var handler = new Mock <HttpMessageHandler>().Object;

            IHttpRoute result = _webApiRoutes.CreateRoute("uri", defaults, constraints, dataTokens, handler);

            Assert.IsType <HostedHttpRoute>(result);
            Assert.Equal("Bar", result.Defaults["Foo"]);
            Assert.Equal("Baz", result.Constraints["Bar"]);
            Assert.Equal("Biff", result.DataTokens["Baz"]);
            Assert.Same(handler, result.Handler);
        }
        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);
        }
Exemplo n.º 3
-1
        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);
        }