< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 22
Coverable lines: 22
Total lines: 59
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
ValidateCompressionLevel(...)0%440%
InitializeHeaders(...)100%110%
SerializeToStream(...)100%110%
SerializeToStreamAsync(...)0%440%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Http/src/System/Net/Http/CompressedContentCore.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.IO;
 5using System.IO.Compression;
 6using System.Net.Http.Headers;
 7using System.Threading;
 8using System.Threading.Tasks;
 9
 10namespace System.Net.Http
 11{
 12    /// <summary>
 13    /// Shared helpers for the per-algorithm compressed content types. Provides header initialization,
 14    /// argument validation, and serialization that copies the inner content through a compression stream.
 15    /// </summary>
 16    internal static class CompressedContentCore
 17    {
 18        public static void ValidateCompressionLevel(CompressionLevel compressionLevel, string paramName)
 019        {
 20            // Validate up front so an invalid enum value fails fast at construction time rather than
 21            // deferring the exception to the serialization path when the request is being sent.
 022            if (compressionLevel is not (CompressionLevel.Optimal or CompressionLevel.Fastest or CompressionLevel.NoComp
 023            {
 024                throw new ArgumentOutOfRangeException(paramName);
 25            }
 026        }
 27
 28        public static void InitializeHeaders(HttpContent target, HttpContent source, string encoding)
 029        {
 30            // Copy headers from the original content.
 031            target.Headers.AddHeaders(source.Headers);
 32
 33            // Append our encoding to the Content-Encoding header (supports stacking per HTTP spec).
 34            // Always use TryAddWithoutValidation so we append the new encoding without re-parsing or
 35            // validating any existing Content-Encoding values the inner content may have added.
 036            target.Headers.TryAddWithoutValidation(KnownHeaders.ContentEncoding.Descriptor, encoding);
 37
 38            // Remove Content-Length since we don't know the compressed size upfront.
 039            target.Headers.ContentLength = null;
 040        }
 41
 42        public static void SerializeToStream(HttpContent originalContent, Stream compressionStream, TransportContext? co
 043        {
 044            using (compressionStream)
 045            {
 046                originalContent.CopyTo(compressionStream, context, cancellationToken);
 047            }
 048        }
 49
 50        public static async Task SerializeToStreamAsync(HttpContent originalContent, Stream compressionStream, Transport
 051        {
 052            await using (compressionStream.ConfigureAwait(false))
 053            {
 054                await originalContent.CopyToAsync(compressionStream, context, cancellationToken).ConfigureAwait(false);
 055            }
 056        }
 57    }
 58}
 59