using System.Text; using FindAndDrive.Domain.Models.DTO.Request; using FindAndDrive.Domain.Models.DTO.Response; using Newtonsoft.Json; namespace FindAndDrive.Tests.Performance.Helpers; public static class AuthenticationHelper { public static async Task GetToken(HttpClient httpClient, TokenRequest payload) { var uriBuilder = new UriBuilder(httpClient.BaseAddress) { Path = $"api/Authentication/token" }; using var request = new HttpRequestMessage(HttpMethod.Post, uriBuilder.Uri); request.Content = new StringContent( JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json" ); var response = await httpClient.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); response.EnsureSuccessStatusCode(); var result = JsonConvert.DeserializeObject(body); if (result is null) throw new Exception("Authentication failed"); return result; } public static async Task RefreshToken(HttpClient httpClient, string token) { var uriBuilder = new UriBuilder(httpClient.BaseAddress) { Path = $"api/Authentication/token" }; using var request = new HttpRequestMessage(HttpMethod.Post, uriBuilder.Uri); request.Headers.Add("Authorization", $"Bearer {token}"); var response = await httpClient.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); response.EnsureSuccessStatusCode(); var result = JsonConvert.DeserializeObject(body); if (result is null) throw new Exception("Authentication failed"); return result; } }