-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid a few allocations in StringContent
- Loading branch information
Showing
3 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/libraries/System.Net.Http/tests/UnitTests/StringContentTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace System.Net.Http.Tests | ||
{ | ||
public class StringContentTests | ||
{ | ||
[Fact] | ||
public void StringContent_SetsDefaultContentTypeHeader() | ||
{ | ||
var content = new StringContent("Foo"); | ||
Assert.Equal("text/plain; charset=utf-8", content.Headers.ContentType.ToString()); | ||
|
||
content = new StringContent("Foo", (Encoding)null); | ||
Assert.Equal("text/plain; charset=utf-8", content.Headers.ContentType.ToString()); | ||
|
||
content = new StringContent("Foo", Encoding.UTF8); | ||
Assert.Equal("text/plain; charset=utf-8", content.Headers.ContentType.ToString()); | ||
|
||
content = new StringContent("Foo", Encoding.UTF8, "text/plain"); | ||
Assert.Equal("text/plain; charset=utf-8", content.Headers.ContentType.ToString()); | ||
|
||
content = new StringContent("Foo", null, "text/plain"); | ||
Assert.Equal("text/plain; charset=utf-8", content.Headers.ContentType.ToString()); | ||
|
||
content = new StringContent("Foo", Encoding.UTF8, (string)null); | ||
Assert.Equal("text/plain; charset=utf-8", content.Headers.ContentType.ToString()); | ||
|
||
content = new StringContent("Foo", null, (string)null); | ||
Assert.Equal("text/plain; charset=utf-8", content.Headers.ContentType.ToString()); | ||
|
||
content = new StringContent("Foo", (MediaTypeHeaderValue)null); | ||
Assert.Null(content.Headers.ContentType); | ||
|
||
content = new StringContent("Foo", null, (MediaTypeHeaderValue)null); | ||
Assert.Null(content.Headers.ContentType); | ||
} | ||
|
||
[Theory] | ||
[InlineData("text/plain")] | ||
[InlineData("application/json")] | ||
[InlineData("application/xml")] | ||
[InlineData("foo/bar")] | ||
public void StringContent_SetsContentTypeHeader(string mediaType) | ||
{ | ||
var content = new StringContent("foo", Encoding.UTF8, mediaType); | ||
Assert.Equal($"{mediaType}; charset=utf-8", content.Headers.ContentType.ToString()); | ||
|
||
content = new StringContent("foo", null, mediaType); | ||
Assert.Equal($"{mediaType}; charset=utf-8", content.Headers.ContentType.ToString()); | ||
|
||
content = new StringContent("foo", Encoding.ASCII, mediaType); | ||
Assert.Equal($"{mediaType}; charset=us-ascii", content.Headers.ContentType.ToString()); | ||
|
||
content = new StringContent("foo", new MediaTypeHeaderValue(mediaType)); | ||
Assert.Equal(mediaType, content.Headers.ContentType.ToString()); | ||
|
||
content = new StringContent("foo", Encoding.UTF8, new MediaTypeHeaderValue(mediaType, "ascii")); | ||
Assert.Equal($"{mediaType}; charset=ascii", content.Headers.ContentType.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters