using System; using System.Net; using System.Net.Http; using System.Net.Http.Json; using System.Threading.Tasks; using FluentAssertions; using Timeline.Tests.Helpers; namespace Timeline.Tests.IntegratedTests2 { public delegate Task RequestSetupAsync(HttpRequestMessage httpRequest); public static class HttpClientTestExtensions { public static async Task TestSendAsync(this HttpClient client, HttpMethod method, string url, HttpContent? body = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, RequestSetupAsync? requestSetup = null) { using var req = new HttpRequestMessage { Method = method, RequestUri = new Uri(url, UriKind.Relative), Content = body }; var task = requestSetup?.Invoke(req); if (task is not null) await task; var res = await client.SendAsync(req); res.StatusCode.Should().Be(expectedStatusCode); return res; } public static async Task AssertJsonBodyAsync(HttpResponseMessage response) { var body = await response.Content.ReadFromJsonAsync(CommonJsonSerializeOptions.Options); body.Should().NotBeNull($"Body is not json format of type {typeof(T).FullName}"); return body!; } public static async Task TestJsonSendAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, RequestSetupAsync? requestSetup = null) { using JsonContent? reqContent = jsonBody is null ? null : JsonContent.Create(jsonBody, options: CommonJsonSerializeOptions.Options); await client.TestSendAsync(method, url, reqContent, expectedStatusCode, requestSetup); } public static async Task TestJsonSendAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, RequestSetupAsync? requestSetup = null) { using JsonContent? reqContent = jsonBody == null ? null : JsonContent.Create(jsonBody, options: CommonJsonSerializeOptions.Options); var res = await client.TestSendAsync(method, url, reqContent, expectedStatusCode, requestSetup); var resBody = await AssertJsonBodyAsync(res); return resBody; } } }