private async Task <Endpoint> GenerateCorsEndPoint(bool isOptionsRequest, bool includeMetadata)
        {
            // Arrange
            IEndpointRouteBuilder builder = EndpointRouteBuilderFactory.Create("odata",
                                                                               c => c.AddService(ServiceLifetime.Singleton, b => GetEdmModel()));

            HttpContext httpContext = new DefaultHttpContext
            {
                RequestServices = builder.ServiceProvider
            };

            var httpMethod = isOptionsRequest ? HttpMethod.Options : HttpMethod.Get;

            HttpRequest          request = SetHttpRequest(httpContext, httpMethod, "http://localhost:123/Customers(1)");
            RouteValueDictionary values  = new RouteValueDictionary();

            values.Add("ODataEndpointPath_odata", "Customers(1)");

            ActionDescriptor actionDescriptor = new ControllerActionDescriptor
            {
                ControllerName   = "Customers",
                ActionName       = "Get",
                EndpointMetadata = GenerateMetadata(includeMetadata)
            };

            IActionSelector actionSelector = new MockActionSelector(actionDescriptor);

            ODataEndpointRouteValueTransformer transformer = new ODataEndpointRouteValueTransformer(actionSelector);
            await transformer.TransformAsync(httpContext, values);

            return(httpContext.GetEndpoint());
        }
Exemplo n.º 2
0
        public void ODataEndpointRouteValueTransformerrowsIfRouteNameIsNull()
        {
            IEdmModel model = GetEdmModel();

            IEndpointRouteBuilder builder = EndpointRouteBuilderFactory.CreateWithRootContainer("odata",
                                                                                                c => c.AddService(ServiceLifetime.Singleton, b => model));

            // Arrange & Act
            HttpContext httpContext = new DefaultHttpContext
            {
                RequestServices = builder.ServiceProvider
            };

            RouteContext     routeContext               = new RouteContext(httpContext);
            ActionDescriptor actionDescriptor           = new ControllerActionDescriptor();
            IReadOnlyList <ActionDescriptor> candidates = new List <ActionDescriptor>();

            Mock <IActionSelector> selector = new Mock <IActionSelector>();

            selector.Setup(e => e.SelectCandidates(routeContext)).Returns(candidates);
            selector.Setup(e => e.SelectBestCandidate(routeContext, candidates)).Returns(actionDescriptor);

            ODataEndpointRouteValueTransformer transformer = new ODataEndpointRouteValueTransformer(selector.Object);

            // Assert
            RouteValueDictionary values = new RouteValueDictionary();

            values.Add("ODataEndpointPath_odata", "Customers(1)");

            var results = transformer.TransformAsync(httpContext, values);
        }
        public void TransformAsyncReturnsCorrectRouteValues()
        {
            // Arrange
            IEndpointRouteBuilder builder = EndpointRouteBuilderFactory.Create("odata",
                                                                               c => c.AddService(ServiceLifetime.Singleton, b => GetEdmModel()));

            HttpContext httpContext = new DefaultHttpContext
            {
                RequestServices = builder.ServiceProvider
            };

            HttpRequest          request = SetHttpRequest(httpContext, HttpMethod.Get, "http://localhost:123/Customers(1)");
            RouteValueDictionary values  = new RouteValueDictionary();

            values.Add("ODataEndpointPath_odata", "Customers(1)");

            ActionDescriptor actionDescriptor = new ControllerActionDescriptor
            {
                ControllerName = "Customers",
                ActionName     = "Get"
            };
            IActionSelector actionSelector = new MockActionSelector(actionDescriptor);

            // Act
            ODataEndpointRouteValueTransformer transformer = new ODataEndpointRouteValueTransformer(actionSelector);
            ValueTask <RouteValueDictionary>   actual      = transformer.TransformAsync(httpContext, values);

            // Assert
            Assert.NotNull(actual.Result);
            RouteValueDictionary routeValues = actual.Result;

            Assert.Equal(4, routeValues.Count);
            Assert.Equal("Customers(1)", routeValues["ODataEndpointPath_odata"]);
            Assert.Equal("Customers", routeValues["controller"]);
            Assert.Equal("Get", routeValues["action"]);
            Assert.Equal("Customers(1)", routeValues["odataPath"]);

            Assert.NotNull(httpContext.ODataFeature().Path);
            Assert.Same(actionDescriptor, httpContext.ODataFeature().ActionDescriptor);
        }