Skip to content

Commit

Permalink
Ensure that default media type is used when 'null' is passed-in to St…
Browse files Browse the repository at this point in the history
…ringContent #81506 (#81722)
  • Loading branch information
slovely authored Mar 14, 2023
1 parent 6ddb03f commit 86550d7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public StringContent(string content, Encoding? encoding)
/// <param name="encoding">The encoding to use for the content.</param>
/// <param name="mediaType">The media type to use for the content.</param>
public StringContent(string content, Encoding? encoding, string mediaType)
: this(content, encoding, new MediaTypeHeaderValue(mediaType, (encoding ?? DefaultStringEncoding).WebName))
: this(content, encoding, new MediaTypeHeaderValue(mediaType ?? DefaultMediaType, (encoding ?? DefaultStringEncoding).WebName))
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ public async Task Ctor_DefineNoEncoding_DefaultEncodingUsed()
Assert.Equal(sourceString, roundTrip);
}

[Fact]
public void Ctor_PassNullForMediaType_DefaultMediaTypeUsed()
{
string sourceString = "\u00C4\u00E4\u00FC\u00DC";
Encoding defaultStringEncoding = Encoding.GetEncoding("utf-8");
var content = new StringContent(sourceString, defaultStringEncoding, ((string)null)!);

// If no media is passed-in, the default is used
Assert.Equal("text/plain", content.Headers.ContentType.MediaType);
}

[Fact]
public async Task Ctor_UseCustomMediaTypeHeaderValue_SpecificEncoding()
{
Expand Down

0 comments on commit 86550d7

Please sign in to comment.