// See https://github.com/microsoft/reverse-proxy/blob/51d797986b1fea03500a1ad173d13a1176fb5552/src/ReverseProxy/Forwarder/RequestUtilities.cs#L61-L83
    public async Task RestrictedHeaders_CopiedIfAllowed(string names, int expected)
    {
        var httpContext    = new DefaultHttpContext();
        var trailerFeature = new TestTrailersFeature();

        httpContext.Features.Set <IHttpResponseTrailersFeature>(trailerFeature);
        var proxyResponse = new HttpResponseMessage();

        proxyResponse.TrailingHeaders.TryAddWithoutValidation(HeaderNames.Connection, "value1");
        proxyResponse.TrailingHeaders.TryAddWithoutValidation(HeaderNames.TransferEncoding, "value2");
        proxyResponse.TrailingHeaders.TryAddWithoutValidation(HeaderNames.KeepAlive, "value3");

        var allowed          = names.Split(';');
        var transform        = new ResponseTrailersAllowedTransform(allowed);
        var transformContext = new ResponseTrailersTransformContext()
        {
            HttpContext   = httpContext,
            ProxyResponse = proxyResponse,
            HeadersCopied = false,
        };
        await transform.ApplyAsync(transformContext);

        Assert.True(transformContext.HeadersCopied);

        Assert.Equal(expected, trailerFeature.Trailers.Count());
        foreach (var header in trailerFeature.Trailers)
        {
            Assert.Contains(header.Key, allowed, StringComparer.OrdinalIgnoreCase);
        }
    }
        public async Task RemoveTrailerFromFeature_Success(string names, string values, int status, ResponseCondition condition, string removedHeader, string expected)
        {
            var httpContext = new DefaultHttpContext();

            httpContext.Response.StatusCode = status;
            var trailerFeature = new TestTrailersFeature();

            httpContext.Features.Set <IHttpResponseTrailersFeature>(trailerFeature);
            var proxyResponse = new HttpResponseMessage();

            foreach (var pair in TestResources.ParseNameAndValues(names, values))
            {
                trailerFeature.Trailers.Add(pair.Name, pair.Values);
            }

            var transform = new ResponseTrailerRemoveTransform(removedHeader, condition);
            await transform.ApplyAsync(new ResponseTrailersTransformContext()
            {
                HttpContext   = httpContext,
                ProxyResponse = proxyResponse,
                HeadersCopied = true,
            });

            var expectedHeaders = expected.Split("; ", StringSplitOptions.RemoveEmptyEntries);

            Assert.Equal(expectedHeaders, trailerFeature.Trailers.Select(h => h.Key));
        }
Exemplo n.º 3
0
        public async Task AddResponseTrailer_Success(string startValue, int status, string value, bool append, bool always, string expected)
        {
            var httpContext    = new DefaultHttpContext();
            var trailerFeature = new TestTrailersFeature();

            httpContext.Features.Set <IHttpResponseTrailersFeature>(trailerFeature);
            trailerFeature.Trailers["name"] = startValue.Split(";", System.StringSplitOptions.RemoveEmptyEntries);
            httpContext.Response.StatusCode = status;
            var transformContext = new ResponseTrailersTransformContext()
            {
                HttpContext   = httpContext,
                ProxyResponse = new HttpResponseMessage(),
                HeadersCopied = true,
            };
            var transform = new ResponseTrailerValueTransform("name", value, append, always);
            await transform.ApplyAsync(transformContext);

            Assert.Equal(expected.Split(";", System.StringSplitOptions.RemoveEmptyEntries), trailerFeature.Trailers["name"]);
        }
    public void TakeHeader_HeadersCopied_ReturnsNothing()
    {
        var httpContext    = new DefaultHttpContext();
        var trailerFeature = new TestTrailersFeature();

        httpContext.Features.Set <IHttpResponseTrailersFeature>(trailerFeature);
        var proxyResponse = new HttpResponseMessage();

        proxyResponse.TrailingHeaders.Add("Name", "value1");
        var result = ResponseTrailersTransform.TakeHeader(new ResponseTrailersTransformContext()
        {
            HttpContext   = httpContext,
            ProxyResponse = proxyResponse,
            HeadersCopied = true,
        }, "name");

        Assert.Equal(StringValues.Empty, result);
        Assert.False(trailerFeature.Trailers.TryGetValue("name", out var _));
        Assert.Equal(new[] { "value1" }, proxyResponse.TrailingHeaders.GetValues("name"));
    }
Exemplo n.º 5
0
        public async Task TransformResponseTrailersAsync_RemovesRestrictedHeaders()
        {
            var transformer     = HttpTransformer.Default;
            var httpContext     = new DefaultHttpContext();
            var trailersFeature = new TestTrailersFeature();

            httpContext.Features.Set <IHttpResponseTrailersFeature>(trailersFeature);
            var proxyResponse = new HttpResponseMessage();

            foreach (var header in RestrictedHeaders)
            {
                Assert.True(proxyResponse.TrailingHeaders.TryAddWithoutValidation(header, "value"));
            }

            await transformer.TransformResponseTrailersAsync(httpContext, proxyResponse);

            foreach (var header in RestrictedHeaders)
            {
                Assert.False(trailersFeature.Trailers.ContainsKey(header));
            }
        }