Skip to content

Commit d13c9d5

Browse files
committed
add method for setting up HttpClient with raw json content
Remove dependency on Newtonsoft.Json
1 parent 47743bf commit d13c9d5

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/HttpTestUtils/HttpTestUtils/HttpClientMock.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Newtonsoft.Json;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Net;
54
using System.Net.Http;
@@ -19,7 +18,7 @@ public static HttpClient SetupHttpClientWithJsonResponse<TResponseContent>(HttpS
1918
public static HttpClient SetupHttpClientWithJsonResponse<TResponseContent>(HttpResponseContent<TResponseContent> response)
2019
{
2120
var messageHandler =
22-
new TestHttpMessageHandler(_ => Task.FromResult(new HttpResponseMessage(response.StatusCode) { Content = new StringContent(JsonConvert.SerializeObject(response.Content), Encoding.UTF8, "application/json") }));
21+
new TestHttpMessageHandler(_ => Task.FromResult(new HttpResponseMessage(response.StatusCode) { Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(response.Content), Encoding.UTF8, "application/json") }));
2322

2423
return new HttpClient(messageHandler);
2524
}
@@ -33,7 +32,7 @@ public static HttpClient SetupHttpClientWithJsonResponse<TResponseContent>(Func<
3332

3433
var response = new HttpResponseMessage(responseContent.StatusCode)
3534
{
36-
Content = new StringContent(JsonConvert.SerializeObject(responseContent.Content),
35+
Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(responseContent.Content),
3736
Encoding.UTF8,
3837
"application/json")
3938
};
@@ -42,7 +41,15 @@ public static HttpClient SetupHttpClientWithJsonResponse<TResponseContent>(Func<
4241
});
4342

4443
return new HttpClient(messageHandler);
45-
}
44+
}
45+
46+
public static HttpClient SetupHttpClientWithRawJsonResponse(HttpStatusCode statusCode, string rawJson)
47+
{
48+
var messageHandler =
49+
new TestHttpMessageHandler(_ => Task.FromResult(new HttpResponseMessage(statusCode) { Content = new StringContent(rawJson, Encoding.UTF8, "application/json") }));
50+
51+
return new HttpClient(messageHandler);
52+
}
4653

4754
/// <summary>
4855
/// Sets up a HttpClientMock that will return another response on each invocation.
@@ -65,7 +72,7 @@ public static HttpClient SetupHttpClientWithMultipleJsonResponses<TResponseConte
6572

6673
var response = new HttpResponseMessage(responseContent.StatusCode)
6774
{
68-
Content = new StringContent(JsonConvert.SerializeObject(responseContent.Content),
75+
Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(responseContent.Content),
6976
Encoding.UTF8,
7077
"application/json")
7178
};

src/HttpTestUtils/HttpTestUtils/HttpTestUtils.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
16+
<PackageReference Include="System.Text.Json" Version="[8.0.5,)" />
1717
</ItemGroup>
1818
</Project>

0 commit comments

Comments
 (0)