Add BackendHttpClient.
MODIFY
WalletWasabi.Tests/RegressionTests/BackendTests.cs
WalletWasabi.Tests/RegressionTests/BackendTests.cs
@@ -29,20 +29,20 @@ namespace WalletWasabi.Tests.RegressionTests
public BackendTests(RegTestFixture regTestFixture)
{
RegTestFixture = regTestFixture;
- HttpClient = ClearnetHttpClient.Instance;
+ BackendHttpClient = new BackendHttpClient(ClearnetHttpClient.Instance, RegTestFixture.BackendEndPointApiUri);
}
private RegTestFixture RegTestFixture { get; }
/// <summary>Clearnet HTTP client.</summary>
- private IHttpClient HttpClient { get; }
+ private BackendHttpClient BackendHttpClient { get; }
#region BackendTests
[Fact]
public async Task GetExchangeRatesAsync()
{
- using var response = await HttpClient.SendAsync(HttpMethod.Get, new Uri(RegTestFixture.BackendEndPointApiUri, "btc/offchain/exchange-rates"));
+ using var response = await BackendHttpClient.SendAsync(HttpMethod.Get, "btc/offchain/exchange-rates");
Assert.True(response.StatusCode == HttpStatusCode.OK);
var exchangeRates = await response.Content.ReadAsJsonAsync<List<ExchangeRate>>();
@@ -74,7 +74,7 @@ namespace WalletWasabi.Tests.RegressionTests
Logger.TurnOff();
- using var response = await HttpClient.SendAsync(HttpMethod.Post, new Uri(RegTestFixture.BackendEndPointApiUri, "btc/blockchain/broadcast"), content);
+ using var response = await BackendHttpClient.SendAsync(HttpMethod.Post, "btc/blockchain/broadcast", content);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("Transaction is already in the blockchain.", await response.Content.ReadAsJsonAsync<string>());
@@ -90,7 +90,7 @@ namespace WalletWasabi.Tests.RegressionTests
Logger.TurnOff();
- var response = await HttpClient.SendAsync(HttpMethod.Post, new Uri(RegTestFixture.BackendEndPointApiUri, "btc/blockchain/broadcast"), content);
+ var response = await BackendHttpClient.SendAsync(HttpMethod.Post, "btc/blockchain/broadcast", content);
Assert.NotEqual(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
@@ -170,7 +170,7 @@ namespace WalletWasabi.Tests.RegressionTests
[Fact]
public async Task StatusRequestTestAsync()
{
- var requestUri = new Uri(RegTestFixture.BackendEndPointApiUri, "btc/Blockchain/status");
+ var requestUri = "btc/Blockchain/status";
(_, IRPCClient rpc, _, _, _, _, Backend.Global global) = await Common.InitializeTestEnvironmentAsync(RegTestFixture, 1);
@@ -191,7 +191,7 @@ namespace WalletWasabi.Tests.RegressionTests
times++;
}
- HttpResponseMessage? response = await HttpClient.SendAsync(HttpMethod.Get, requestUri);
+ HttpResponseMessage? response = await BackendHttpClient.SendAsync(HttpMethod.Get, requestUri);
Assert.NotNull(response);
using (HttpContent content = response!.Content)
@@ -206,7 +206,7 @@ namespace WalletWasabi.Tests.RegressionTests
await rpc.GenerateAsync(1);
- response = await HttpClient.SendAsync(HttpMethod.Get, requestUri);
+ response = await BackendHttpClient.SendAsync(HttpMethod.Get, requestUri);
Assert.NotNull(response);
using (HttpContent content = response!.Content)
@@ -223,7 +223,7 @@ namespace WalletWasabi.Tests.RegressionTests
// Set back the time to trigger timeout in BlockchainController.GetStatusAsync.
global.IndexBuilderService.LastFilterBuildTime = DateTimeOffset.UtcNow - BlockchainController.FilterTimeout;
- response = await HttpClient.SendAsync(HttpMethod.Get, requestUri);
+ response = await BackendHttpClient.SendAsync(HttpMethod.Get, requestUri);
Assert.NotNull(response);
using (HttpContent content = response!.Content)

ADD
WalletWasabi/Tor/Http/BackendHttpClient.cs
+using System;
+using System.Net.Http;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace WalletWasabi.Tor.Http
+{
+ /// <summary>
+ /// Convenience class that allows sending HTTP request to Wasabi Backend using relative URIs.
+ /// </summary>
+ public class BackendHttpClient
+ {
+ /// <summary>
+ /// Creates a new instance of the object.
+ /// </summary>
+ /// <param name="httpClient">Use <see cref="ClearnetHttpClient"/> or <see cref="TorHttpClient"/> to initialize.</param>
+ public BackendHttpClient(IHttpClient httpClient, Uri backendApiUri)
+ {
+ HttpClient = httpClient;
+ BackendApiUri = backendApiUri;
+ }
+
+ private IHttpClient HttpClient { get; }
+ private Uri BackendApiUri { get; }
+
+ /// <summary>
+ /// Sends an HTTP request to Wasabi Backend.
+ /// </summary>
+ /// <param name="method">HTTP method</param>
+ /// <param name="apiQuery">API query part to append to <see cref="BackendApiUri"/>.</param>
+ /// <param name="content">HTTP request content. Only for <see cref="HttpMethod.Post"/> method.</param>
+ /// <param name="token">Cancellation token to cancel the asynchronous operation.</param>
+ public Task<HttpResponseMessage> SendAsync(HttpMethod method, string apiQuery, HttpContent? content = null, CancellationToken token = default)
+ {
+ if (apiQuery.StartsWith("/"))
+ {
+ throw new ArgumentException("Forward slash would lead to http://backend:port/<relativeUri> URI instead of http://backend:port/api/v<version>/<relativeUri>.", nameof(apiQuery));
+ }
+
+ return HttpClient.SendAsync(method, new Uri(BackendApiUri, apiQuery), content, token);
+ }
+ }
+}

Add BackendHttpClient.