Skip to content

Commit

Permalink
Fix end-to-end tests
Browse files Browse the repository at this point in the history
Use JsonSerializerContext to (de)serialize JSON.
  • Loading branch information
martincostello committed Sep 11, 2024
1 parent 3edce8f commit f9644da
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tests/API.Tests/EndToEnd/ApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Xml.Linq;
using Microsoft.AspNetCore.WebUtilities;

namespace MartinCostello.Api.EndToEnd;

public class ApiTests(ApiFixture fixture) : EndToEndTest(fixture)
public partial class ApiTests(ApiFixture fixture) : EndToEndTest(fixture)
{
[SkippableFact]
public async Task Can_Get_Time()
Expand All @@ -20,7 +21,7 @@ public async Task Can_Get_Time()
using var client = Fixture.CreateClient();

// Act
using var response = await client.GetFromJsonAsync<JsonDocument>("/time");
using var response = await client.GetFromJsonAsync("/time", AppJsonSerializerContext.Default.JsonDocument);

// Assert
response.ShouldNotBeNull();
Expand All @@ -47,7 +48,7 @@ public async Task Can_Generate_Guid()
using var client = Fixture.CreateClient();

// Act
using var response = await client.GetFromJsonAsync<JsonDocument>("/tools/guid");
using var response = await client.GetFromJsonAsync("/tools/guid", AppJsonSerializerContext.Default.JsonDocument);

// Assert
response.ShouldNotBeNull();
Expand All @@ -69,7 +70,7 @@ public async Task Can_Generate_Machine_Key()
string requestUri = QueryHelpers.AddQueryString("/tools/machinekey", parameters);

// Act
using var response = await client.GetFromJsonAsync<JsonDocument>(requestUri);
using var response = await client.GetFromJsonAsync(requestUri, AppJsonSerializerContext.Default.JsonDocument);

// Assert
response.ShouldNotBeNull();
Expand Down Expand Up @@ -105,7 +106,7 @@ public async Task Can_Generate_Hash(string algorithm, string format, string plai
using var client = Fixture.CreateClient();

// Act
using var response = await client.PostAsJsonAsync("/tools/hash", request);
using var response = await client.PostAsJsonAsync("/tools/hash", request, AppJsonSerializerContext.Default.JsonObject);

// Assert
response.EnsureSuccessStatusCode();
Expand All @@ -114,4 +115,8 @@ public async Task Can_Generate_Hash(string algorithm, string format, string plai

result.RootElement.GetProperty("hash").GetString().ShouldBe(expected);
}

[JsonSerializable(typeof(JsonDocument))]
[JsonSerializable(typeof(JsonObject))]
private sealed partial class AppJsonSerializerContext : JsonSerializerContext;
}

0 comments on commit f9644da

Please sign in to comment.