Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson 9.1.1-preview.5

This is a prerelease version of Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson.
dotnet add package Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson --version 9.1.1-preview.5
                    
NuGet\Install-Package Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson -Version 9.1.1-preview.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson" Version="9.1.1-preview.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson" Version="9.1.1-preview.5" />
                    
Directory.Packages.props
<PackageReference Include="Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson --version 9.1.1-preview.5
                    
#r "nuget: Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson, 9.1.1-preview.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson@9.1.1-preview.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson&version=9.1.1-preview.5&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson&version=9.1.1-preview.5&prerelease
                    
Install as a Cake Tool

ASP.NET Core Tests Newtonsoft.Json

A library that provides testing utilities for ASP.NET Core applications using Newtonsoft.Json as the JSON serialization provider.

Intended Usage

Use this library when testing ASP.NET Core applications that use Newtonsoft.Json for JSON serialization. It includes helper methods for serializing and deserializing test data, as well as utilities for HTTP request and response handling.

Installation

Add the package to your project:

dotnet add package Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson

Usage Examples

Deserializing HTTP Response Content

using Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson.Http;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using NUnit.Framework;

[TestFixture]
public class ApiClientTests
{
    private readonly HttpClient _httpClient;

    public ApiClientTests()
    {
        _httpClient = new HttpClient();
        _httpClient.BaseAddress = new Uri("https://api.example.com/");
    }    [Test]
    public async Task GetProducts_ReturnsProducts_UsingDeserializeAsync()
    {
        // Arrange & Act
        var response = await _httpClient.GetAsync("api/products");
        
        // Assert
        // DeserializeAsync doesn't check status code, just deserializes content
        var products = await response.DeserializeAsync<List<Product>>();        Assert.That(products, Is.Not.Null);
        Assert.That(products, Is.Not.Empty);
    }    [Test]
    public async Task GetProduct_WithStatusCheck_ThrowsOnNonSuccessStatus()
    {
        // Arrange & Act
        var response = new HttpResponseMessage(System.Net.HttpStatusCode.NotFound);
        response.Content = new StringContent("Product not found");
        response.RequestMessage = new HttpRequestMessage(HttpMethod.Get, "api/products/999");
        
        // Assert
        // DeserializeWithStatusCodeCheckAsync will throw if status code is not success        Assert.ThrowsAsync<HttpRequestException>(async () => 
            await response.DeserializeWithStatusCodeCheckAsync<Product>());
    }    [Test]
    public async Task EnsureSuccessStatusCode_ThrowsWithContent_WhenNotSuccess()
    {
        // Arrange
        var response = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
        response.Content = new StringContent("Invalid request parameters");
        response.RequestMessage = new HttpRequestMessage(HttpMethod.Get, "api/products");
        
        // Act & Assert
        // EnsureSuccessStatusCodeAsync enhances exception with content details        var exception = Assert.ThrowsAsync<HttpRequestException>(async () => 
            await response.EnsureSuccessStatusCodeAsync());
            
        Assert.That(exception.Message, Does.Contain("StatusCode: BadRequest"));
        Assert.That(exception.Message, Does.Contain("Invalid request parameters"));
    }
      [Test]
    public async Task TestResponseAssertions_UsingExtensionMethods()
    {
        // Arrange
        var badRequestResponse = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
        var notFoundResponse = new HttpResponseMessage(System.Net.HttpStatusCode.NotFound);
        
        var validationResponse = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
        validationResponse.Content = new StringContent(@"{
            ""type"": ""https://tools.ietf.org/html/rfc7231#section-6.5.1"",
            ""title"": ""One or more validation errors occurred."",
            ""status"": 400,
            ""errors"": {
                ""Name"": [""The Name field is required.""]
            }
        }");
        
        // Act & Assert
        // Use the assertion extension methods
        badRequestResponse.BeBadRequest();
        notFoundResponse.BeNotFound();
        validationResponse.ContainValidationError("Name", "required");
    }
}

Using HttpClient Extensions

using Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson.Http;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using NUnit.Framework;

[TestFixture]
public class HttpClientExtensionsTests
{
    private readonly HttpClient _httpClient;

    public HttpClientExtensionsTests()
    {
        _httpClient = new HttpClient();
        _httpClient.BaseAddress = new Uri("https://api.example.com/");
    }    [Test]
    public async Task GetAsync_SimplifiesHttpGetWithDeserialization()
    {
        // The GetAsync extension method combines request and deserialization
        var product = await _httpClient.GetAsync<Product>("api/products/1");
        
        // Result is automatically deserialized and status checked        Assert.That(product, Is.Not.Null);
        Assert.That(product.Id, Is.EqualTo(1));
    }
      [Test]
    public async Task PostAsync_SimplifiesHttpPostWithSerialization()
    {
        // Create a product to send
        var newProduct = new Product { Name = "New Product", Price = 19.99m };
        
        // The PostAsync extension method handles serialization
        await _httpClient.PostAsync("api/products", newProduct);
        
        // There's also a version that returns a typed response
        var createdProduct = await _httpClient.PostAsync<Product, Product>("api/products", newProduct);
          Assert.That(createdProduct, Is.Not.Null);
        Assert.That(createdProduct.Id, Is.Not.EqualTo(0));
    }
      [Test]
    public async Task PutAsync_SimplifiesHttpPutWithSerialization()
    {
        // Create a product to update
        var updatedProduct = new Product { Id = 1, Name = "Updated Product", Price = 29.99m };
        
        // The PutAsync extension methods handle serialization
        await _httpClient.PutAsync("api/products/1", updatedProduct);
        
        // There's also a version that returns a typed response
        var result = await _httpClient.PutAsync<Product, Product>("api/products/1", updatedProduct);
        
        Assert.That(result, Is.Not.Null);
    }
}

Configuring JSON Serialization Settings

The library uses a central HttpSerializationSettings class for controlling JSON serialization behavior:

using Enigmatry.Entry.AspNetCore.Tests.NewtonsoftJson.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

// You can customize the JSON serialization settings used by all methods
HttpSerializationSettings.Settings = new JsonSerializerSettings 
{
    Formatting = Formatting.Indented,
    NullValueHandling = NullValueHandling.Ignore,
    DateTimeZoneHandling = DateTimeZoneHandling.Utc,
    Converters = new List<JsonConverter> 
    { 
        new StringEnumConverter() 
    }
};

// After configuration, all deserialization calls will use these settings

Additional Information

This library is designed to work with the following dependencies:

  • Newtonsoft.Json - For JSON serialization
  • Microsoft.AspNetCore.Mvc - For ValidationProblemDetails
  • System.Net.Http.Formatting - For JsonMediaTypeFormatter

It works well with XUnit, NUnit, or MSTest for testing ASP.NET Core applications that use Newtonsoft.Json as their JSON serializer.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.1.1-preview.5 144 8/8/2025
9.1.1-preview.4 91 6/27/2025
9.1.1-preview.3 119 6/4/2025
9.1.0 395 6/3/2025
9.0.1-preview.8 123 5/26/2025
9.0.1-preview.7 204 5/13/2025
9.0.1-preview.6 99 5/9/2025
9.0.1-preview.5 123 5/7/2025
9.0.1-preview.4 119 4/30/2025
9.0.1-preview.2 133 4/1/2025
9.0.0 824 2/26/2025
8.1.1-preview.3 128 5/7/2025
8.1.1-preview.1 126 4/1/2025
8.1.0 264 2/19/2025
8.0.1-preview.4 75 2/7/2025
8.0.1-preview.2 56 1/15/2025
8.0.0 256 11/27/2024
3.4.6-preview.10 70 11/27/2024
3.4.3 1,338 10/22/2024
3.4.2 216 10/11/2024
3.4.1 138 10/9/2024
3.4.0 130 10/9/2024
3.3.2 142 8/28/2024
3.3.2-preview.7 69 8/27/2024
3.3.1 254 7/16/2024
3.3.1-preview.4 68 7/12/2024
3.3.0 146 6/20/2024
3.2.1-preview.4 73 6/17/2024
3.2.1-preview.1 77 5/23/2024
3.2.0 3,453 4/3/2024
3.1.1-preview.1 78 3/13/2024
3.1.0 175 3/8/2024
3.1.0-preview.2 78 2/19/2024
3.0.1-preview.2 91 2/9/2024
3.0.1-preview.1 87 1/24/2024
3.0.0 844 1/15/2024
3.0.0-preview.14 87 1/9/2024
3.0.0-preview.12 80 1/9/2024
3.0.0-preview.5 82 1/10/2024
3.0.0-preview.2 109 12/28/2023
3.0.0-preview 148 12/20/2023
2.1.0 183 12/28/2023
2.0.1-preview.3 110 12/1/2023
2.0.1-preview.2 89 11/29/2023
2.0.1-preview.1 89 11/28/2023
2.0.0 262 11/8/2023
2.0.0-preview.3 105 10/27/2023
2.0.0-preview.2 95 10/27/2023
2.0.0-preview.1 88 10/27/2023
2.0.0-preview 126 10/27/2023
1.1.500 166 10/27/2023
1.1.495 199 9/24/2023
1.1.486 192 9/13/2023
1.1.484 203 9/7/2023
1.1.482 193 9/6/2023
1.1.480 296 8/24/2023
1.1.477 229 8/2/2023
1.1.464 276 7/5/2023
1.1.447 237 5/26/2023
1.1.396 344 4/11/2023
1.1.383 294 4/3/2023
1.1.377 284 3/13/2023
1.1.376 256 3/13/2023
1.1.365 473 2/15/2023