Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OFFI-135: DRYing and using system json #413

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 51 additions & 25 deletions Lombiq.Tests.UI/Extensions/HttpClientUITestContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Lombiq.Tests.UI.Services;
using Newtonsoft.Json.Linq;
using Shouldly;
using System;
using System.Collections.Generic;
Expand All @@ -25,6 +24,8 @@ namespace Lombiq.Tests.UI.Extensions;
Justification = "Disposed by the HttpClient.")]
public static class HttpClientUITestContextExtensions
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new(JsonSerializerDefaults.Web);

public static HttpClient CreateClient(this UITestContext context)
{
var handler = new HttpClientHandler
Expand Down Expand Up @@ -102,8 +103,7 @@ public static async Task<TObject> GetAndReadResponseContentAsync<TObject>(
where TObject : class
{
var content = await GetAndReadResponseContentAsync(context, client, requestUri);
var parsed = JToken.Parse(content);
return parsed.ToObject<TObject>();
return Deserialize<TObject>(content);
}

/// <summary>
Expand All @@ -117,7 +117,7 @@ public static async Task<string> PostAndReadResponseContentAsync(
string requestUri,
string json)
{
using var response = await PostAndGetResponseAsync(client, requestUri, json);
using var response = await PostAndGetResponseAsync(client, json, requestUri);
return await response.Content.ReadAsStringAsync();
}

Expand All @@ -129,16 +129,29 @@ public static async Task<string> PostAndReadResponseContentAsync(
public static async Task<TObject> PostAndReadResponseContentAsync<TObject>(
this UITestContext context,
HttpClient client,
string requestUri,
string json)
where TObject : class
{
using var response = await PostAndGetResponseAsync(client, requestUri, json);
var content = await response.Content.ReadAsStringAsync();
var parsed = JToken.Parse(content);
string json,
string requestUri)
Comment on lines +132 to +133
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this breaking change necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To have the same structure for all extension methods, where the last parameter is the request URI and the previous parameter is either the json or the object that needs to be serialized.

where TObject : class =>
Deserialize<TObject>(await context.PostAndReadResponseContentAsync(
client,
requestUri,
json));

return parsed.ToObject<TObject>();
}
/// <summary>
/// Issues a POST request to the given <paramref name="requestUri"/> using the provided
/// <paramref name="objectToSerialize"/>, that will be serialized as JSON, then the response content is deserialized
/// to the given <typeparamref name="TObject"/> and returned.
/// </summary>
public static async Task<TObject> PostAndReadResponseContentAsync<TObject>(
this UITestContext context,
HttpClient client,
object objectToSerialize,
string requestUri)
where TObject : class =>
Deserialize<TObject>(await context.PostAndReadResponseContentAsync(
client,
requestUri,
Serialize(objectToSerialize)));

/// <summary>
/// Issues a POST request to the given <paramref name="requestUri"/> using the provided
Expand All @@ -149,11 +162,8 @@ public static Task<string> PostAndReadResponseContentAsync(
this UITestContext context,
HttpClient client,
object objectToSerialize,
string requestUri)
{
var json = JsonSerializer.Serialize(objectToSerialize);
return PostAndReadResponseContentAsync(context, client, requestUri, json);
}
string requestUri) =>
PostAndReadResponseContentAsync(context, client, requestUri, Serialize(objectToSerialize));

/// <summary>
/// Issues a POST request to the given <paramref name="requestUri"/> using the provided
Expand All @@ -164,27 +174,29 @@ public static Task<HttpResponseMessage> PostAndGetResponseAsync(
this UITestContext context,
HttpClient client,
object objectToSerialize,
string requestUri)
{
var json = JsonSerializer.Serialize(objectToSerialize);
return PostAndGetResponseAsync(client, requestUri, json);
}
string requestUri) =>
PostAndGetResponseAsync(client, Serialize(objectToSerialize), requestUri);

/// <summary>
/// Issues a POST request to the given <paramref name="requestUri"/> using the provided <paramref name="json"/>.
/// </summary>
/// <returns>The <see cref="HttpResponseMessage"/>.</returns>
public static async Task<HttpResponseMessage> PostAndGetResponseAsync(
HttpClient client,
string requestUri,
string json)
string json,
string requestUri)
{
var stringContent = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json);
var response = await client.PostAsync(requestUri, stringContent);

return response;
}

/// <summary>
/// Issues a POST request to the given <paramref name="requestUri"/> using the provided
/// <paramref name="objectToSerialize"/>, that will be serialized as JSON, then the response is checked if it's the
/// <paramref name="expected"/> status code.
/// </summary>
public static async Task PostAndResponseStatusCodeShouldBeAsync(
this UITestContext context,
HttpClient client,
Expand All @@ -195,4 +207,18 @@ public static async Task PostAndResponseStatusCodeShouldBeAsync(
using var response = await context.PostAndGetResponseAsync(client, objectToSerialize, requestUri);
response.StatusCode.ShouldBe(expected);
}

/// <summary>
/// Returns the serialized object as JSON using the default <see cref="JOptions"/> settings.
/// </summary>
public static string Serialize(object objectToSerialize) =>
JsonSerializer.Serialize(objectToSerialize, JsonSerializerOptions);

/// <summary>
/// Deserializes the provided <paramref name="content"/> to the given <typeparamref name="TObject"/> using the
/// default <see cref="JOptions"/> settings.
/// </summary>
public static TObject Deserialize<TObject>(string content)
where TObject : class =>
JsonSerializer.Deserialize<TObject>(content, JsonSerializerOptions);
}