Skip to content

Commit

Permalink
Merge pull request #150 from bubblecup-12/bugfix/stream-handling-in-j…
Browse files Browse the repository at this point in the history
…soncontentmatcher

Seek stream to beginning before json deserialize
  • Loading branch information
richardszalay authored Nov 30, 2024
2 parents 3517eca + 118ca82 commit 7b0d72f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
46 changes: 46 additions & 0 deletions RichardSzalay.MockHttp.Tests/Issues/Issue149Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Xunit;

namespace RichardSzalay.MockHttp.Tests.Issues;

public class Issue149Tests
{
private HttpClient client;

Check warning on line 12 in RichardSzalay.MockHttp.Tests/Issues/Issue149Tests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Non-nullable field 'client' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 12 in RichardSzalay.MockHttp.Tests/Issues/Issue149Tests.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Non-nullable field 'client' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 12 in RichardSzalay.MockHttp.Tests/Issues/Issue149Tests.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Non-nullable field 'client' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 12 in RichardSzalay.MockHttp.Tests/Issues/Issue149Tests.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Non-nullable field 'client' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 12 in RichardSzalay.MockHttp.Tests/Issues/Issue149Tests.cs

View workflow job for this annotation

GitHub Actions / Build (macOS-latest)

Non-nullable field 'client' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 12 in RichardSzalay.MockHttp.Tests/Issues/Issue149Tests.cs

View workflow job for this annotation

GitHub Actions / Build (macOS-latest)

Non-nullable field 'client' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

//https://github.com/richardszalay/mockhttp/issues/149
[Fact]
public async Task MultipleJsonContentMatchersToSameUri()
{
var mockHttp = new MockHttpMessageHandler();
mockHttp
.When("https://someUrl.com")
.WithJsonContent<SomeJsonParameters>(a => a.Key == "SomeApiKey")
.Respond("application/json", "{\"some_result\" : \"Error\"}");
mockHttp
.When("https://someUrl.com")
.WithJsonContent<SomeJsonParameters>(a => a.Content == "SomeContent")
.Respond("application/json", "{\"some_result\" : \"Result\"}");

client = mockHttp.ToHttpClient();

var parameters = JsonSerializer.Serialize(new SomeJsonParameters("SomeApiKey", "OtherContent"));
var response = await client.PostAsync(
"https://someUrl.com",
new StringContent(parameters, Encoding.UTF8, "application/json"));
var content = await response.Content.ReadAsStringAsync();
Assert.True(!string.IsNullOrEmpty(content));

var parameters1 = JsonSerializer.Serialize(new SomeJsonParameters("OtherApiKey", "SomeContent"));
var action1 = async ()=> await client.PostAsync(
"https://someUrl.com",
new StringContent(parameters1, Encoding.UTF8, "application/json"));
var exception = await Record.ExceptionAsync(action1);
Assert.Null(exception);
}

private record SomeJsonParameters(string Key, string Content);
}
4 changes: 4 additions & 0 deletions RichardSzalay.MockHttp/Matchers/JsonContentMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public bool Matches(HttpRequestMessage message)

private T? Deserialize(Stream stream)
{
if (stream.CanSeek)
{
stream.Seek(0, SeekOrigin.Begin);
}
#if NET5_0
return JsonSerializer.DeserializeAsync<T>(stream, serializerOptions)
.GetAwaiter().GetResult();
Expand Down

0 comments on commit 7b0d72f

Please sign in to comment.