Exemplo n.º 1
0
        public async Task Existent_RouteDefinitionMatch_Test()
        {
            var httpContext = new DefaultHttpContext();

            A.CallTo(() => _routeDefinitionMatcher.MatchAsync(
                         httpContext.Request.Path,
                         httpContext.Request.Method))
            .Returns(new RouteDefinitionMatch());

            await _middleware.InvokeAsync(httpContext);

            Assert.IsNotNull(httpContext.GetRouteDefinitionMatch());
            A.CallTo(() => _next(httpContext))
            .MustHaveHappened();
        }
Exemplo n.º 2
0
        public async Task OptionsCall_WithNoRoute_Test()
        {
            var httpContext = new DefaultHttpContext();

            httpContext.Request.Method = HttpMethods.Options;

            A.CallTo(() => _routeDefinitionMatcher.MatchAsync(A <string> .Ignored, A <string> .Ignored))
            .Returns <RouteDefinitionMatch>(null);

            await _middleware.InvokeAsync(httpContext);

            A.CallTo(() => _next(httpContext))
            .MustNotHaveHappened();

            Assert.AreEqual(StatusCodes.Status204NoContent, httpContext.Response.StatusCode);
        }
Exemplo n.º 3
0
        public async Task InvokeAsync(HttpContext httpContext)
        {
            var routeDefinitionMatch = await _routeDefinitionMatcher.MatchAsync(
                httpContext.Request.Path.Value,
                httpContext.Request.Method);

            if (routeDefinitionMatch != null)
            {
                httpContext.SetRouteDefinitionMatch(routeDefinitionMatch);
            }
            await _next(httpContext);
        }
Exemplo n.º 4
0
        public async Task InvokeAsync(HttpContext httpContext)
        {
            if (!httpContext.Request.Headers.TryGetValue(HeaderNames.Origin, out var origin))
            {
                origin = default;
            }

            if (httpContext.Request.Method == HttpMethods.Options)
            {
                if (!httpContext.Request.Headers.TryGetValue(HeaderNames.AccessControlRequestMethod, out var requestMethod))
                {
                    requestMethod = default;
                }

                var optionsRouteDefinitionMatch = await _routeDefinitionMatcher.MatchAsync(httpContext.Request.Path.Value, requestMethod);

                if (AllowsOrigin(origin, optionsRouteDefinitionMatch?.RouteDefinition))
                {
                    httpContext.Response.Headers.Add(HeaderNames.AccessControlAllowOrigin, origin);
                    httpContext.Response.Headers.Add(HeaderNames.AccessControlAllowMethods,
                                                     new StringValues(optionsRouteDefinitionMatch.RouteDefinition.RouteMethods.ToArray()));
                }

                httpContext.Response.StatusCode = StatusCodes.Status204NoContent;
                return;
            }

            var routeDefinitionMatch = httpContext.GetRouteDefinitionMatch();

            if (AllowsOrigin(origin, routeDefinitionMatch?.RouteDefinition))
            {
                httpContext.Response.Headers.Add(HeaderNames.AccessControlAllowOrigin, origin);
            }

            await _next(httpContext);
        }