< Summary

Information
Class: System.Net.Http.StringContent
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\StringContent.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 41
Coverable lines: 41
Total lines: 115
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
.ctor(...)100%110%
.ctor(...)100%110%
.ctor(...)0%12120%
.ctor(...)100%110%
GetContentByteArray(...)0%220%
SerializeToStreamAsync(...)0%220%
TryCreateContentReadStream()0%220%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\StringContent.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.Diagnostics;
 5using System.IO;
 6using System.Net.Http.Headers;
 7using System.Text;
 8using System.Threading;
 9using System.Threading.Tasks;
 10
 11namespace System.Net.Http
 12{
 13    /// <summary>Provides HTTP content based on a string.</summary>
 14    public class StringContent : ByteArrayContent
 15    {
 16        /// <summary>The media type to use when none is specified.</summary>
 17        private const string DefaultMediaType = "text/plain";
 18
 19        /// <summary>Creates a new instance of the <see cref="StringContent"/> class.</summary>
 20        /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param>
 21        /// <remarks>The media type for the <see cref="StringContent"/> created defaults to text/plain.</remarks>
 22        public StringContent(string content)
 023            : this(content, DefaultStringEncoding, DefaultMediaType)
 024        {
 025        }
 26
 27        /// <summary>Creates a new instance of the <see cref="StringContent"/> class.</summary>
 28        /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param>
 29        /// <param name="mediaType">The media type to use for the content.</param>
 30        public StringContent(string content, MediaTypeHeaderValue? mediaType)
 031            : this(content, DefaultStringEncoding, mediaType)
 032        {
 033        }
 34
 35        /// <summary>Creates a new instance of the <see cref="StringContent"/> class.</summary>
 36        /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param>
 37        /// <param name="encoding">The encoding to use for the content.</param>
 38        /// <remarks>The media type for the <see cref="StringContent"/> created defaults to text/plain.</remarks>
 39        public StringContent(string content, Encoding? encoding)
 040            : this(content, encoding, DefaultMediaType)
 041        {
 042        }
 43
 44        /// <summary>Creates a new instance of the <see cref="StringContent"/> class.</summary>
 45        /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param>
 46        /// <param name="encoding">The encoding to use for the content.</param>
 47        /// <param name="mediaType">The media type to use for the content.</param>
 48        public StringContent(string content, Encoding? encoding, string? mediaType)
 049            : base(GetContentByteArray(content, encoding))
 050        {
 051            Debug.Assert(DefaultStringEncoding.WebName == "utf-8");
 52
 053            encoding ??= DefaultStringEncoding;
 054            mediaType ??= DefaultMediaType;
 55
 56            // Avoid allocating MediaTypeHeaderValue and related objects for common media types.
 057            if (ReferenceEquals(encoding, DefaultStringEncoding))
 058            {
 059                string? knownValue = mediaType switch
 060                {
 061                    "text/plain" => "text/plain; charset=utf-8",
 062                    "application/json" => "application/json; charset=utf-8",
 063                    _ => null
 064                };
 65
 066                if (knownValue is not null)
 067                {
 068                    Headers.TryAddWithoutValidation(KnownHeaders.ContentType.Descriptor, knownValue);
 069                    return;
 70                }
 071            }
 72
 073            Headers.ContentType = new MediaTypeHeaderValue(mediaType, encoding.WebName);
 074        }
 75
 76        /// <summary>Creates a new instance of the <see cref="StringContent"/> class.</summary>
 77        /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param>
 78        /// <param name="encoding">The encoding to use for the content.</param>
 79        /// <param name="mediaType">The media type to use for the content.</param>
 80        public StringContent(string content, Encoding? encoding, MediaTypeHeaderValue? mediaType)
 081            : base(GetContentByteArray(content, encoding))
 082        {
 083            Headers.ContentType = mediaType;
 084        }
 85
 86        /// <summary>Serialize the string into a byte-array using encoding information provided by the caller (if any).<
 87        /// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param>
 88        /// <param name="encoding">The encoding to use for the content.</param>
 89        /// <returns>The serialized byte array.</returns>
 90        private static byte[] GetContentByteArray(string content, Encoding? encoding)
 091        {
 092            ArgumentNullException.ThrowIfNull(content);
 93
 94            // In this case we treat 'null' strings differently from string.Empty in order to be consistent with our
 95            // other *Content constructors: 'null' throws, empty values are allowed.
 96
 097            return (encoding ?? DefaultStringEncoding).GetBytes(content);
 098        }
 99
 100        /// <summary>Serialize and write the byte array provided in the constructor to an HTTP content stream as an asyn
 101        /// <param name="stream">The target stream.</param>
 102        /// <param name="context">Information about the transport, like channel binding token. This parameter may be <se
 103        /// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
 104        /// <returns>The task object representing the asynchronous operation.</returns>
 105        protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance
 106            // Only skip the original protected virtual SerializeToStreamAsync if this
 107            // isn't a derived type that may have overridden the behavior.
 0108            GetType() == typeof(StringContent) ? SerializeToStreamAsyncCore(stream, cancellationToken) :
 0109            base.SerializeToStreamAsync(stream, context, cancellationToken);
 110
 111        internal override Stream? TryCreateContentReadStream() =>
 0112            GetType() == typeof(StringContent) ? CreateMemoryStreamForByteArray() : // type check ensures we use possibl
 0113            null;
 114    }
 115}