Пример #1
0
 public override void Apply(RequestParametersTransformContext context)
 {
     if (FromMethod.Equals(context.ProxyRequest.Method))
     {
         context.ProxyRequest.Method = ToMethod;
     }
 }
        /// <inheritdoc/>
        public override void Apply(RequestParametersTransformContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            switch (Mode)
            {
            case PathTransformMode.Set:
                context.Path = Value;
                break;

            case PathTransformMode.Prefix:
                context.Path = Value + context.Path;
                break;

            case PathTransformMode.RemovePrefix:
                context.Path = context.Path.StartsWithSegments(Value, out var remainder) ? remainder : context.Path;
                break;

            default:
                throw new NotImplementedException(Mode.ToString());
            }
        }
 public override void Apply(RequestParametersTransformContext context)
 {
     if (HttpMethodEquals(FromMethod, context.Method))
     {
         context.Method = ToMethod;
     }
 }
Пример #4
0
        public override void Apply(RequestParametersTransformContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var value = GetValue(context);

            if (!string.IsNullOrEmpty(value))
            {
                switch (_mode)
                {
                case QueryStringTransformMode.Append:
                    StringValues newValue = value;
                    if (context.Query.Collection.TryGetValue(_key, out var currentValue))
                    {
                        newValue = StringValues.Concat(currentValue, value);
                    }
                    context.Query.Collection[_key] = newValue;
                    break;

                case QueryStringTransformMode.Set:
                    context.Query.Collection[_key] = value;
                    break;

                default:
                    throw new NotImplementedException(_mode.ToString());
                }
            }
        }
        public void Set_PathPattern_ReplacesPathWithRouteValues(string transformValue, string expected)
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddOptions();
            serviceCollection.AddRouting();
            using var services = serviceCollection.BuildServiceProvider();

            var httpContext = new DefaultHttpContext();

            httpContext.Request.RouteValues = new AspNetCore.Routing.RouteValueDictionary()
            {
                { "a", "6" },
                { "b", "7" },
                { "c", "8" },
            };
            var context = new RequestParametersTransformContext()
            {
                Path        = "/",
                HttpContext = httpContext
            };
            var transform = new PathRouteValuesTransform(transformValue, services.GetRequiredService <TemplateBinderFactory>());

            transform.Apply(context);
            Assert.Equal(expected, context.Path);
        }
Пример #6
0
        public override void Apply(RequestParametersTransformContext context)
        {
            if (context is null)
            {
                throw new System.ArgumentNullException(nameof(context));
            }

            context.Path = _binder.BindValues(context.HttpContext.Request.RouteValues);
        }
Пример #7
0
        public void Set_AddsNewQueryStringParameter()
        {
            var httpContext = new DefaultHttpContext();
            var context     = new RequestParametersTransformContext()
            {
                Query       = new QueryTransformContext(httpContext.Request),
                HttpContext = httpContext
            };
            var transform = new QueryParameterFromStaticTransform(QueryStringTransformMode.Set, "z", "foo");

            transform.Apply(context);
            Assert.Equal("?z=foo", context.Query.QueryString.Value);
        }
Пример #8
0
        public void HttpMethod_Works(string fromMethod, string toMethod, string requestMethod, string expected)
        {
            var httpContext = new DefaultHttpContext();
            var context     = new RequestParametersTransformContext()
            {
                Method      = requestMethod,
                HttpContext = httpContext
            };
            var transform = new HttpMethodTransform(fromMethod, toMethod);

            transform.Apply(context);
            Assert.Equal(expected, context.Method);
        }
        public void Set_Path_Success(string initialValue, string modeString, string transformValue, string expected)
        {
            // We can't put an internal type in a public test API parameter.
            var mode    = Enum.Parse <PathStringTransform.PathTransformMode>(modeString);
            var context = new RequestParametersTransformContext()
            {
                Path = initialValue
            };
            var transform = new PathStringTransform(mode, transformValue);

            transform.Apply(context);
            Assert.Equal(expected, context.Path);
        }
Пример #10
0
        public override void Apply(RequestParametersTransformContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var routeValues = context.HttpContext.Request.RouteValues;
            // Route values that are not considered defaults will be appended as query parameters. Make them all defaults.
            var binder = _binderFactory.Create(Template, defaults: routeValues);

            context.Path = binder.BindValues(acceptedValues: routeValues);
        }
        public void DoesNotFailOnNonExistingQueryParameter()
        {
            var httpContext = new DefaultHttpContext();

            httpContext.Request.QueryString = new QueryString("?z=1");
            var context = new RequestParametersTransformContext()
            {
                Query = new QueryTransformContext(httpContext.Request),
            };
            var transform = new RemoveQueryParameterTransform("a");

            transform.Apply(context);
            Assert.Equal("?z=1", context.Query.QueryString.Value);
        }
        public void LeavesOtherQueryParameters()
        {
            var httpContext = new DefaultHttpContext();

            httpContext.Request.QueryString = new QueryString("?z=1&a=2");
            var context = new RequestParametersTransformContext()
            {
                Query = new QueryTransformContext(httpContext.Request),
            };
            var transform = new RemoveQueryParameterTransform("z");

            transform.Apply(context);
            Assert.Equal("?a=2", context.Query.QueryString.Value);
        }
        public void RemovesExistingQueryParameter()
        {
            var httpContext = new DefaultHttpContext();

            httpContext.Request.QueryString = new QueryString("?z=1");
            var context = new RequestParametersTransformContext()
            {
                Query = new QueryTransformContext(httpContext.Request)
            };
            var transform = new RemoveQueryParameterTransform("z");

            transform.Apply(context);
            Assert.False(context.Query.QueryString.HasValue);
        }
Пример #14
0
        public void Append_IgnoresExistingQueryStringParameter()
        {
            var httpContext = new DefaultHttpContext();

            httpContext.Request.QueryString = new QueryString("?z=1");
            var context = new RequestParametersTransformContext()
            {
                Query       = new QueryTransformContext(httpContext.Request),
                HttpContext = httpContext
            };
            var transform = new QueryParameterFromStaticTransform(QueryStringTransformMode.Append, "z", "foo");

            transform.Apply(context);
            Assert.Equal("?z=1&z=foo", context.Query.QueryString.Value);
        }
        public void HttpMethod_Works(string fromMethod, string toMethod, string requestMethod, string expected)
        {
            var httpContext = new DefaultHttpContext();
            var request     = new HttpRequestMessage()
            {
                Method = new HttpMethod(requestMethod)
            };
            var context = new RequestParametersTransformContext()
            {
                HttpContext  = httpContext,
                ProxyRequest = request,
            };
            var transform = new HttpMethodTransform(fromMethod, toMethod);

            transform.Apply(context);
            Assert.Equal(expected, request.Method.Method);
        }
Пример #16
0
        public void Append_AddsQueryParameterWithRouteValue(string pattern, string routeValueKey, string expected)
        {
            const string path = "/6/7/8";

            var routeValues     = new AspNetCore.Routing.RouteValueDictionary();
            var templateMatcher = new TemplateMatcher(TemplateParser.Parse(pattern), new AspNetCore.Routing.RouteValueDictionary());

            templateMatcher.TryMatch(path, routeValues);

            var httpContext = new DefaultHttpContext();

            httpContext.Request.RouteValues = routeValues;
            var context = new RequestParametersTransformContext()
            {
                Path        = path,
                Query       = new QueryTransformContext(httpContext.Request),
                HttpContext = httpContext
            };
            var transform = new QueryParameterRouteTransform(QueryStringTransformMode.Append, "z", routeValueKey);

            transform.Apply(context);
            Assert.Equal(expected, context.Query.QueryString.Value);
        }
Пример #17
0
        public void Set_AddsNewQueryParameter()
        {
            const string path = "/6/7/8";

            var routeValues     = new AspNetCore.Routing.RouteValueDictionary();
            var templateMatcher = new TemplateMatcher(TemplateParser.Parse("/{a}/{b}/{c}"), new AspNetCore.Routing.RouteValueDictionary());

            templateMatcher.TryMatch(path, routeValues);

            var httpContext = new DefaultHttpContext();

            httpContext.Request.RouteValues = routeValues;
            var context = new RequestParametersTransformContext()
            {
                Path        = path,
                Query       = new QueryTransformContext(httpContext.Request),
                HttpContext = httpContext
            };
            var transform = new QueryParameterRouteTransform(QueryStringTransformMode.Set, "z", "a");

            transform.Apply(context);
            Assert.Equal("?z=6", context.Query.QueryString.Value);
        }
Пример #18
0
        // These intentionally do not call base because the logic here overlaps with the default header copy logic.
        public override Task TransformRequestAsync(HttpContext context, HttpRequestMessage proxyRequest, string destinationPrefix)
        {
            var transformContext = new RequestParametersTransformContext()
            {
                DestinationPrefix = destinationPrefix,
                HttpContext       = context,
                ProxyRequest      = proxyRequest,
                Path  = context.Request.Path,
                Query = new QueryTransformContext(context.Request),
            };

            foreach (var requestTransform in RequestTransforms)
            {
                requestTransform.Apply(transformContext);
            }

            // Allow a transform to directly set a custom RequestUri.
            proxyRequest.RequestUri ??= RequestUtilities.MakeDestinationAddress(
                transformContext.DestinationPrefix, transformContext.Path, transformContext.Query.QueryString);

            CopyRequestHeaders(context, proxyRequest);

            return(Task.CompletedTask);
        }
Пример #19
0
 protected abstract string GetValue(RequestParametersTransformContext context);