Esempio n. 1
0
        public void GetRouteServices_ReturnsNull()
        {
            // Arrange
            ODataOptions options = new ODataOptions();

            // Act & Assert
            Assert.Null(options.GetRouteServices(null));
        }
Esempio n. 2
0
        public void GetRouteServices_ReturnsCorrectServiceProvider_When_Leading_Or_Trailing_Slashes(string routePrefix)
        {
            // Arrange
            ODataOptions options  = new ODataOptions();
            IEdmModel    edmModel = EdmCoreModel.Instance;

            // Act
            options.AddRouteComponents(routePrefix, edmModel);

            // & Assert
            // can retrieve service provider using original routePrefix
            IServiceProvider sp = options.GetRouteServices(routePrefix);

            Assert.NotNull(sp);

            // can retrieve service provider using sanitized routePrefix
            string           sanitizedRoutePrefix = "odata";
            IServiceProvider sp2 = options.GetRouteServices(sanitizedRoutePrefix);

            Assert.NotNull(sp2);
        }
Esempio n. 3
0
        public void GetRouteServices_ReturnsCorrectServiceProvider()
        {
            // Arrange
            ODataOptions options  = new ODataOptions();
            IEdmModel    edmModel = EdmCoreModel.Instance;

            // Act
            options.AddRouteComponents("odata", edmModel);

            // & Assert
            IServiceProvider sp = options.GetRouteServices("odata");

            Assert.NotNull(sp);
        }
        /// <summary>
        /// Create a scoped request.
        /// </summary>
        /// <param name="request">The <see cref="HttpRequest"/> instance to extend.</param>
        /// <param name="routePrefix">The route prefix for this request. Should match an entry in <see cref="ODataOptions.RouteComponents"/>.</param>
        /// <returns></returns>
        private static IServiceScope CreateRequestScope(this HttpRequest request, string routePrefix)
        {
            ODataOptions options = request.ODataOptions();

            IServiceProvider rootContainer = options.GetRouteServices(routePrefix);
            IServiceScope    scope         = rootContainer.GetRequiredService <IServiceScopeFactory>().CreateScope();

            // Bind scoping request into the OData container.
            if (!string.IsNullOrEmpty(routePrefix))
            {
                scope.ServiceProvider.GetRequiredService <HttpRequestScope>().HttpRequest = request;
            }

            return(scope);
        }
Esempio n. 5
0
        public Task ApplyAsync(HttpContext httpContext, CandidateSet candidates)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            IODataFeature odataFeature = httpContext.ODataFeature();

            if (odataFeature.Path != null)
            {
                // If we have the OData path setting, it means there's some Policy working.
                // Let's skip this default OData matcher policy.
                return(Task.CompletedTask);
            }

            for (var i = 0; i < candidates.Count; i++)
            {
                ref CandidateState candidate = ref candidates[i];
                if (!candidates.IsValidCandidate(i))
                {
                    continue;
                }

                IODataRoutingMetadata metadata = candidate.Endpoint.Metadata.OfType <IODataRoutingMetadata>().FirstOrDefault();
                if (metadata == null)
                {
                    continue;
                }

                // Get api-version query from HttpRequest?
                QueryStringApiVersionReader reader = new QueryStringApiVersionReader("api-version");
                string apiVersionStr = reader.Read(httpContext.Request);
                if (apiVersionStr == null)
                {
                    candidates.SetValidity(i, false);
                    continue;
                }
                ApiVersion apiVersion = ApiVersion.Parse(apiVersionStr);

                IEdmModel model = GetEdmModel(apiVersion);
                if (model == null)
                {
                    candidates.SetValidity(i, false);
                    continue;
                }

                if (!IsApiVersionMatch(candidate.Endpoint.Metadata, apiVersion))
                {
                    candidates.SetValidity(i, false);
                    continue;
                }

                ODataTemplateTranslateContext translatorContext
                    = new ODataTemplateTranslateContext(httpContext, candidate.Endpoint, candidate.Values, model);

                try
                {
                    ODataPath odataPath = _translator.Translate(metadata.Template, translatorContext);
                    if (odataPath != null)
                    {
                        odataFeature.RoutePrefix = metadata.Prefix;
                        odataFeature.Model       = model;
                        odataFeature.Path        = odataPath;

                        ODataOptions options = new ODataOptions();
                        UpdateQuerySetting(options);
                        options.AddRouteComponents(model);
                        odataFeature.Services = options.GetRouteServices(string.Empty);

                        MergeRouteValues(translatorContext.UpdatedValues, candidate.Values);
                    }
                    else
                    {
                        candidates.SetValidity(i, false);
                    }
                }
                catch
                {
                    candidates.SetValidity(i, false);
                }
            }