-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
AddJsonBody
overload to serialise top-level string (#2043)
* Added AddJsonBody overload for top-level strings
- Loading branch information
1 parent
2d42a33
commit bf24794
Showing
9 changed files
with
123 additions
and
48 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Text.Json; | ||
using RestSharp.Tests.Integrated.Fixtures; | ||
using RestSharp.Tests.Shared.Fixtures; | ||
|
||
namespace RestSharp.Tests.Integrated; | ||
|
||
public class JsonBodyTests : IClassFixture<RequestBodyFixture> { | ||
readonly SimpleServer _server; | ||
readonly ITestOutputHelper _output; | ||
readonly RestClient _client; | ||
|
||
public JsonBodyTests(RequestBodyFixture fixture, ITestOutputHelper output) { | ||
_output = output; | ||
_server = fixture.Server; | ||
_client = new RestClient(_server.Url); | ||
} | ||
|
||
[Fact] | ||
public async Task Query_Parameters_With_Json_Body() { | ||
var request = new RestRequest(RequestBodyCapturer.Resource, Method.Put) | ||
.AddJsonBody(new { displayName = "Display Name" }) | ||
.AddQueryParameter("key", "value"); | ||
|
||
await _client.ExecuteAsync(request); | ||
|
||
RequestBodyCapturer.CapturedUrl.ToString().Should().Be($"{_server.Url}Capture?key=value"); | ||
RequestBodyCapturer.CapturedContentType.Should().Be("application/json; charset=utf-8"); | ||
RequestBodyCapturer.CapturedEntityBody.Should().Be("{\"displayName\":\"Display Name\"}"); | ||
} | ||
|
||
[Fact] | ||
public async Task Add_JSON_body_JSON_string() { | ||
const string payload = "{\"displayName\":\"Display Name\"}"; | ||
|
||
var request = new RestRequest(RequestBodyCapturer.Resource, Method.Post).AddJsonBody(payload); | ||
|
||
await _client.ExecuteAsync(request); | ||
|
||
RequestBodyCapturer.CapturedContentType.Should().Be("application/json; charset=utf-8"); | ||
RequestBodyCapturer.CapturedEntityBody.Should().Be(payload); | ||
} | ||
|
||
[Fact] | ||
public async Task Add_JSON_body_string() { | ||
const string payload = @" | ||
""requestBody"": { | ||
""content"": { | ||
""application/json"": { | ||
""schema"": { | ||
""type"": ""string"" | ||
} | ||
} | ||
} | ||
},"; | ||
|
||
var expected = JsonSerializer.Serialize(payload); | ||
var request = new RestRequest(RequestBodyCapturer.Resource, Method.Post).AddJsonBody(payload, true); | ||
|
||
await _client.ExecuteAsync(request); | ||
|
||
RequestBodyCapturer.CapturedContentType.Should().Be("application/json; charset=utf-8"); | ||
RequestBodyCapturer.CapturedEntityBody.Should().Be(expected); | ||
} | ||
} |
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