This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
91 deletions.
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
82 changes: 82 additions & 0 deletions
82
src/System.Net.Http.Json/tests/FunctionalTests/JsonContentLoopbackTests.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,82 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Net.Http.Headers; | ||
using System.Net.Test.Common; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace System.Net.Http.Json.Functional.Tests | ||
{ | ||
public partial class JsonContentTests | ||
{ | ||
[Fact] | ||
public async Task JsonContentMediaTypeValidateOnServerAsync() | ||
{ | ||
await LoopbackServer.CreateClientAndServerAsync( | ||
async uri => | ||
{ | ||
using (HttpClient client = new HttpClient()) | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Post, uri); | ||
MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse("foo/bar; charset=utf-8"); | ||
request.Content = JsonContent.Create(Person.Create(), mediaType: mediaType); | ||
await client.SendAsync(request); | ||
} | ||
}, | ||
async server => { | ||
HttpRequestData req = await server.HandleRequestAsync(); | ||
Assert.Equal("foo/bar; charset=utf-8", req.GetSingleHeaderValue("Content-Type")); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public static async Task ValidateUtf16IsTranscodedAsync() | ||
{ | ||
await LoopbackServer.CreateClientAndServerAsync( | ||
async uri => | ||
{ | ||
using (HttpClient client = new HttpClient()) | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Post, uri); | ||
MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse("application/json; charset=utf-16"); | ||
// Pass new options to avoid using the Default Web Options that use camelCase. | ||
request.Content = JsonContent.Create(Person.Create(), mediaType: mediaType, options: new JsonSerializerOptions()); | ||
await client.SendAsync(request); | ||
} | ||
}, | ||
async server => { | ||
HttpRequestData req = await server.HandleRequestAsync(); | ||
Assert.Equal("application/json; charset=utf-16", req.GetSingleHeaderValue("Content-Type")); | ||
Person per = JsonSerializer.Deserialize<Person>(Encoding.Unicode.GetString(req.Body)); | ||
per.Validate(); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public async Task TestJsonContentNullContentTypeAsync() | ||
{ | ||
await LoopbackServer.CreateClientAndServerAsync( | ||
async uri => | ||
{ | ||
using (HttpClient client = new HttpClient()) | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Post, uri); | ||
MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse("application/json; charset=utf-16"); | ||
JsonContent content = JsonContent.Create(Person.Create(), mediaType: mediaType); | ||
content.Headers.ContentType = null; | ||
request.Content = content; | ||
await client.SendAsync(request); | ||
} | ||
}, | ||
async server => { | ||
HttpRequestData req = await server.HandleRequestAsync(); | ||
Assert.Equal(0, req.GetHeaderValueCount("Content-Type")); | ||
}); | ||
} | ||
} | ||
} |
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
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