Skip to content

Instantly share code, notes, and snippets.

@erinnmclaughlin
Created March 9, 2025 21:29
Show Gist options
  • Select an option

  • Save erinnmclaughlin/bb35452105fe02fee68e9b8fe3901957 to your computer and use it in GitHub Desktop.

Select an option

Save erinnmclaughlin/bb35452105fe02fee68e9b8fe3901957 to your computer and use it in GitHub Desktop.
Xunit Helpers
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using AssessmentService.Common.Pagination;
using AssessmentService.Common.Serialization;
using MediatR;
namespace IntegrationTests;
public static class XunitJsonExtensions
{
public static readonly JsonSerializerOptions JsonOptions = CreateDefaultJsonSerializerOptions();
/// <summary>
/// Deserializes HTTP content into an object of type <typeparamref name="T"/>
/// </summary>
/// <param name="content">The content to read from.</param>
/// <typeparam name="T">The type to deserialize to.</typeparam>
/// <returns>The deserialized response.</returns>
public static Task<T> ReadFromJsonAsync<T>(this HttpContent content)
=> ReadFromJsonAsync<T>(content, JsonOptions);
/// <summary>
/// Deserializes HTTP content into an object of type <typeparamref name="T"/>
/// </summary>
/// <param name="content">The content to read from.</param>
/// <param name="options">Options to control the behavior during deserialization.</param>
/// <typeparam name="T">The type to deserialize to.</typeparam>
/// <returns>The deserialized response.</returns>
public static async Task<T> ReadFromJsonAsync<T>(this HttpContent content, JsonSerializerOptions options)
{
var response = await HttpContentJsonExtensions.ReadFromJsonAsync<T>(content, options);
Assert.NotNull(response);
return response;
}
/// <summary>
/// Sends a GET request to the specified URL and deserializes the response into an object of type <typeparamref name="T"/>.
/// </summary>
/// <param name="client">The client used to send the request.</param>
/// <param name="requestUri">The URI the request is sent to.</param>
/// <typeparam name="T">The type to deserialize to.</typeparam>
/// <returns>The deserialized response.</returns>
public static Task<T> GetFromJsonAsync<T>(this HttpClient client, [StringSyntax("Uri")] string requestUri)
=> GetFromJsonAsync<T>(client, requestUri, JsonOptions);
/// <summary>
/// Sends a GET request to the specified URL and deserializes the response into an object of type <typeparamref name="T"/>.
/// </summary>
/// <param name="client">The client used to send the request.</param>
/// <param name="requestUri">The URI the request is sent to.</param>
/// <param name="options">Options to control the behavior during deserialization.</param>
/// <typeparam name="T">The type to deserialize to.</typeparam>
/// <returns>The deserialized response.</returns>
public static async Task<T> GetFromJsonAsync<T>(this HttpClient client, [StringSyntax("Uri")] string requestUri, JsonSerializerOptions options)
{
var response = await HttpClientJsonExtensions.GetFromJsonAsync<T>(client, requestUri, options);
Assert.NotNull(response);
return response;
}
private static JsonSerializerOptions CreateDefaultJsonSerializerOptions()
{
var jsonOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
jsonOptions.Converters.Add(new JsonStringEnumConverter());
return jsonOptions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment