Repository: dotnet/aspnetcore
File: src/Middleware/ResponseCompression/test/ResponseCompressionMiddlewareTest.cs (C#)

Code before:
                    {
                        context.Response.Headers.ContentMD5 = "MD5";
                        context.Response.ContentType = responseType;

                        if (HttpMethods.IsHead(context.Request.Method))
                        {
                            context.Response.ContentLength = uncompressedBodyLength;
                            return Task.CompletedTask;
                        }

                        addResponseAction?.Invoke(context.Response);
                        return context.Response.WriteAsync(new string('a', uncompressedBodyLength));
                    });
                });
            }).Build();

        await host.StartAsync();

        var server = host.GetTestServer();
        var client = server.CreateClient();

        var request = new HttpRequestMessage(new HttpMethod(httpMethod), "");
        for (var i = 0; i < requestAcceptEncodings?.Length; i++)
        {
            request.Headers.AcceptEncoding.Add(System.Net.Http.Headers.StringWithQualityHeaderValue.Parse(requestAcceptEncodings[i]));
        }

        var response = await client.SendAsync(request);

        return (response, sink.Writes.ToList());
    }

    private static void CheckResponseCompressed(HttpResponseMessage response, long? expectedBodyLength, string expectedEncoding)
    {
        var containsVaryAcceptEncoding = false;
        foreach (var value in response.Headers.GetValues(HeaderNames.Vary))
        {
            if (value.Contains(HeaderNames.AcceptEncoding))
            {
                containsVaryAcceptEncoding = true;
                break;
            }
        }
        Assert.True(containsVaryAcceptEncoding);
        Assert.False(response.Content.Headers.TryGetValues(HeaderNames.ContentMD5, out _));
        Assert.Single(response.Content.Headers.ContentEncoding, expectedEncoding);

        // Test functionality instead of exact byte counts
        CheckCompressionFunctionality(response, expectedEncoding).GetAwaiter().GetResult();
    }


Review comment (suggestion):
`expectedBodyLength` no longer used, should probably remove it.

Code after:
                    {
                        context.Response.Headers.ContentMD5 = "MD5";
                        context.Response.ContentType = responseType;

                        if (HttpMethods.IsHead(context.Request.Method))
                        {
                            context.Response.ContentLength = uncompressedBodyLength;
                            return Task.CompletedTask;
                        }

                        addResponseAction?.Invoke(context.Response);
                        return context.Response.WriteAsync(new string('a', uncompressedBodyLength));
                    });
                });
            }).Build();

        await host.StartAsync();

        var server = host.GetTestServer();
        var client = server.CreateClient();

        var request = new HttpRequestMessage(new HttpMethod(httpMethod), "");
        for (var i = 0; i < requestAcceptEncodings?.Length; i++)
        {
            request.Headers.AcceptEncoding.Add(System.Net.Http.Headers.StringWithQualityHeaderValue.Parse(requestAcceptEncodings[i]));
        }

        var response = await client.SendAsync(request);

        return (response, sink.Writes.ToList());
    }

    private static async Task CheckResponseCompressed(HttpResponseMessage response, string expectedEncoding)
    {
        var containsVaryAcceptEncoding = false;
        foreach (var value in response.Headers.GetValues(HeaderNames.Vary))
        {
            if (value.Contains(HeaderNames.AcceptEncoding))
            {
                containsVaryAcceptEncoding = true;
                break;
            }
        }
        Assert.True(containsVaryAcceptEncoding);
        Assert.False(response.Content.Headers.TryGetValues(HeaderNames.ContentMD5, out _));
        Assert.Single(response.Content.Headers.ContentEncoding, expectedEncoding);

        // Test functionality instead of exact byte counts
        await CheckCompressionFunctionality(response, expectedEncoding, new string('a', 100));
    }