Ejemplo n.º 1
0
        public void ODataBatchPathMappingWorksForSimpleTemplate(string name)
        {
            // Arrange
            string routeName     = "odata";
            string routeTemplate = "hello/{name}/$batch";
            string uri           = "http://localhost/hello/" + name + "/$batch";
            var    request       = RequestFactory.Create(HttpMethod.Get, uri);
            var    mapping       = new ODataBatchPathMapping();

            // Act
            mapping.AddRoute(routeName, routeTemplate);

            bool result = mapping.TryGetRouteName(request.HttpContext, out string outputName);

            // Assert
            Assert.True(result);
            Assert.Equal(outputName, routeName);
            var routeData = request.ODataFeature().BatchRouteData;

            Assert.NotNull(routeData);
            var actual = Assert.Single(routeData);

            Assert.Equal("name", actual.Key);
            Assert.Equal(name, actual.Value);
        }
Ejemplo n.º 2
0
        public void ODataBatchPathMappingWorksForComplexTemplate(string version, string spec)
        {
            // Arrange
            string            routeName     = "odata";
            string            routeTemplate = "/v{api-version:apiVersion}/odata{spec}/$batch";
            string            uri           = "http://localhost/v" + version + "/odata" + spec + "/$batch";
            var               request       = RequestFactory.Create(HttpMethods.Get, uri);
            var               mapping       = new ODataBatchPathMapping();
            ODataBatchHandler handler       = new Mock <ODataBatchHandler>().Object;

            // Act
            mapping.AddRoute(routeName, routeTemplate, handler);

            bool result = mapping.TryGetPrefixName(request.HttpContext, out string outputName, out ODataBatchHandler outHandler);

            // Assert
            Assert.True(result);
            Assert.Equal(outputName, routeName);
            var routeData = request.ODataFeature().BatchRouteData;

            Assert.NotNull(routeData);
            Assert.Equal(new[] { "api-version", "spec" }, routeData.Keys);
            Assert.Equal(version, routeData["api-version"]);
            Assert.Equal(spec, routeData["spec"]);
        }
Ejemplo n.º 3
0
        private HttpRequest CreateRequest(HttpMethod method)
        {
            HttpRequest           request      = RequestFactory.Create(method, URI, this._routeBuilder, "odata");
            ODataBatchPathMapping batchMapping =
                request.HttpContext.RequestServices.GetRequiredService <ODataBatchPathMapping>();

            batchMapping.AddRoute("odata", "/$batch");
            return(request);
        }
Ejemplo n.º 4
0
        public void ODataBatchPathMappingWorksForNormalTemplate()
        {
            // Arrange
            var mapping = new ODataBatchPathMapping();
            var request = RequestFactory.Create(HttpMethod.Get, "http://localhost/$batch");

            mapping.AddRoute("odata", "$batch");

            // Act & Assert
            Assert.True(mapping.TryGetRouteName(request.HttpContext, out string outputName));
            Assert.Equal("odata", outputName);
        }
Ejemplo n.º 5
0
        public void ODataBatchPathMappingWorksForNormalTemplate()
        {
            // Arrange
            ODataBatchHandler handler = new Mock <ODataBatchHandler>().Object;
            var mapping = new ODataBatchPathMapping();
            var request = RequestFactory.Create(HttpMethods.Get, "http://localhost/$batch");

            mapping.AddRoute("odata", "$batch", handler);

            // Act & Assert
            Assert.True(mapping.TryGetPrefixName(request.HttpContext, out string outputName, out ODataBatchHandler outHandler));
            Assert.Equal("odata", outputName);
            Assert.Same(outHandler, handler);
        }
        /// <summary>
        /// Maps the specified OData route and the OData route attributes.
        /// </summary>
        /// <param name="builder">The <see cref="IEndpointRouteBuilder"/> to add the route to.</param>
        /// <param name="routeName">The name of the route to map.</param>
        /// <param name="routePrefix">The prefix to add to the OData route's path template.</param>
        /// <param name="configureAction">The configuring action to add the services to the root container.</param>
        /// <returns>The input <see cref="IEndpointRouteBuilder"/>.</returns>
        public static IEndpointRouteBuilder MapODataRoute(this IEndpointRouteBuilder builder,
                                                          string routeName,
                                                          string routePrefix,
                                                          Action <IContainerBuilder> configureAction)
        {
            if (builder == null)
            {
                throw Error.ArgumentNull("builder");
            }

            if (routeName == null)
            {
                throw Error.ArgumentNull("routeName");
            }

            // Build and configure the root container.
            IServiceProvider serviceProvider = builder.ServiceProvider;

            IPerRouteContainer perRouteContainer = serviceProvider.GetRequiredService <IPerRouteContainer>();

            if (perRouteContainer == null)
            {
                throw Error.InvalidOperation(SRResources.MissingODataServices, nameof(IPerRouteContainer));
            }

            // Make sure the MetadataController is registered with the ApplicationPartManager.
            ApplicationPartManager applicationPartManager = serviceProvider.GetRequiredService <ApplicationPartManager>();

            applicationPartManager.ApplicationParts.Add(new AssemblyPart(typeof(MetadataController).Assembly));

            // Create an service provider for this route. Add the default services to the custom configuration actions.
            Action <IContainerBuilder> builderAction = ConfigureDefaultServices(builder, configureAction);

            IServiceProvider subServiceProvider = perRouteContainer.CreateODataRootContainer(routeName, builderAction);

            // Resolve the path handler and set URI resolver to it.
            IODataPathHandler pathHandler = subServiceProvider.GetRequiredService <IODataPathHandler>();

            // If settings is not on local, use the global configuration settings.
            ODataOptions options = serviceProvider.GetRequiredService <ODataOptions>();

            if (pathHandler != null && pathHandler.UrlKeyDelimiter == null)
            {
                pathHandler.UrlKeyDelimiter = options.UrlKeyDelimiter;
            }

            // Resolve HTTP handler, create the OData route and register it.
            routePrefix = RemoveTrailingSlash(routePrefix);

            // If a batch handler is present, register the route with the batch path mapper. This will be used
            // by the batching middleware to handle the batch request. Batching still requires the injection
            // of the batching middleware via UseODataBatching().
            ODataBatchHandler batchHandler = subServiceProvider.GetService <ODataBatchHandler>();

            if (batchHandler != null)
            {
                // TODO: for the $batch, i need more time to refactor/test it.
                // batchHandler.ODataRoute = route;
                batchHandler.ODataRouteName = routeName;

                string batchPath = String.IsNullOrEmpty(routePrefix)
                    ? '/' + ODataRouteConstants.Batch
                    : '/' + routePrefix + '/' + ODataRouteConstants.Batch;

                ODataBatchPathMapping batchMapping = builder.ServiceProvider.GetRequiredService <ODataBatchPathMapping>();
                batchMapping.AddRoute(routeName, batchPath);
            }

            builder.MapDynamicControllerRoute <ODataEndpointRouteValueTransformer>(
                ODataEndpointPattern.CreateODataEndpointPattern(routeName, routePrefix));

            perRouteContainer.AddRoute(routeName, routePrefix);

            return(builder);
        }