public async Task For_Added(string startValue, string ip, int port, string formatString, bool append, string expected)
        {
            // NodeFormat is on an internal type so we can't put it in a test's public signature.
            var format        = Enum.Parse <RequestHeaderForwardedTransform.NodeFormat>(formatString, ignoreCase: true);
            var randomFactory = new TestRandomFactory();

            randomFactory.Instance = new TestRandom()
            {
                Sequence = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }
            };
            var httpContext = new DefaultHttpContext();

            httpContext.Connection.RemoteIpAddress = string.IsNullOrEmpty(ip) ? null : IPAddress.Parse(ip);
            httpContext.Connection.RemotePort      = port;
            var proxyRequest = new HttpRequestMessage();

            proxyRequest.Headers.Add("Forwarded", startValue.Split("|", StringSplitOptions.RemoveEmptyEntries));
            var transform = new RequestHeaderForwardedTransform(randomFactory, forFormat: format,
                                                                byFormat: RequestHeaderForwardedTransform.NodeFormat.None, host: false, proto: false, append);
            await transform.ApplyAsync(new RequestTransformContext()
            {
                HttpContext   = httpContext,
                ProxyRequest  = proxyRequest,
                HeadersCopied = true,
            });

            Assert.Equal(expected.Split("|", StringSplitOptions.RemoveEmptyEntries), proxyRequest.Headers.GetValues("Forwarded"));
        }
        public async Task AllValues_Added(string startValue, bool append, string expected)
        {
            var randomFactory = new TestRandomFactory();

            randomFactory.Instance = new TestRandom()
            {
                Sequence = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }
            };
            var httpContext = new DefaultHttpContext();

            httpContext.Request.Scheme             = "https";
            httpContext.Request.Host               = new HostString("myHost", 80);
            httpContext.Connection.RemoteIpAddress = IPAddress.IPv6Loopback;
            httpContext.Connection.RemotePort      = 10;
            var proxyRequest = new HttpRequestMessage();

            proxyRequest.Headers.Add("Forwarded", startValue.Split("|", StringSplitOptions.RemoveEmptyEntries));
            var transform = new RequestHeaderForwardedTransform(randomFactory,
                                                                forFormat: RequestHeaderForwardedTransform.NodeFormat.IpAndPort,
                                                                byFormat: RequestHeaderForwardedTransform.NodeFormat.Random,
                                                                host: true, proto: true, append);
            await transform.ApplyAsync(new RequestTransformContext()
            {
                HttpContext   = httpContext,
                ProxyRequest  = proxyRequest,
                HeadersCopied = true,
            });

            Assert.Equal(expected.Split("|", StringSplitOptions.RemoveEmptyEntries), proxyRequest.Headers.GetValues("Forwarded"));
        }
Exemple #3
0
        public async Task By_Added(string startValue, string ip, int port, NodeFormat format, bool append, string expected)
        {
            var randomFactory = new TestRandomFactory();

            randomFactory.Instance = new TestRandom()
            {
                Sequence = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }
            };
            var httpContext = new DefaultHttpContext();

            httpContext.Connection.LocalIpAddress = string.IsNullOrEmpty(ip) ? null : IPAddress.Parse(ip);
            httpContext.Connection.LocalPort      = port;
            var proxyRequest = new HttpRequestMessage();

            proxyRequest.Headers.Add("Forwarded", startValue.Split("|", StringSplitOptions.RemoveEmptyEntries));
            var transform = new RequestHeaderForwardedTransform(randomFactory, forFormat: NodeFormat.None,
                                                                byFormat: format, host: false, proto: false, append);
            await transform.ApplyAsync(new RequestTransformContext()
            {
                HttpContext   = httpContext,
                ProxyRequest  = proxyRequest,
                HeadersCopied = true,
            });

            Assert.Equal(expected.Split("|", StringSplitOptions.RemoveEmptyEntries), proxyRequest.Headers.GetValues("Forwarded"));
        }
        public async Task Host_Added(string startValue, string host, bool append, string expected)
        {
            var randomFactory = new TestRandomFactory();
            var httpContext   = new DefaultHttpContext();

            httpContext.Request.Host = new HostString(host);
            var proxyRequest = new HttpRequestMessage();

            proxyRequest.Headers.Add("Forwarded", startValue.Split("|", StringSplitOptions.RemoveEmptyEntries));
            var transform = new RequestHeaderForwardedTransform(randomFactory, forFormat: RequestHeaderForwardedTransform.NodeFormat.None,
                                                                byFormat: RequestHeaderForwardedTransform.NodeFormat.None, host: true, proto: false, append);
            await transform.ApplyAsync(new RequestTransformContext()
            {
                HttpContext   = httpContext,
                ProxyRequest  = proxyRequest,
                HeadersCopied = true,
            });

            Assert.Equal(expected.Split("|", StringSplitOptions.RemoveEmptyEntries), proxyRequest.Headers.GetValues("Forwarded"));
        }