Improve URI handling in `MockTorHttpClient`.
MODIFY
WalletWasabi.Tests/UnitTests/Clients/MockTorHttpClient.cs
WalletWasabi.Tests/UnitTests/Clients/MockTorHttpClient.cs
using System;
+using System.Collections.Specialized;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
+using System.Web;
using NBitcoin;
using WalletWasabi.TorSocks5;
@@ -20,30 +22,32 @@ namespace WalletWasabi.Tests.UnitTests.Clients
public bool IsTorUsed => true;
- public Func<HttpMethod, string, string[], string, Task<HttpResponseMessage>> OnSendAsync { get; set; }
+ public Func<HttpMethod, string, NameValueCollection, string, Task<HttpResponseMessage>> OnSendAsync { get; set; }
public async Task<HttpResponseMessage> SendAsync(HttpMethod method, string relativeUri, HttpContent content = null, CancellationToken cancel = default)
{
- var body = (content is { })
+ string body = (content is { })
? await content.ReadAsStringAsync()
: "";
- var sepPos = relativeUri.IndexOf('?');
- var action = relativeUri[..sepPos];
- var parameters = relativeUri[(sepPos + 1)..].Split('&', StringSplitOptions.RemoveEmptyEntries);
- return await OnSendAsync(method, action, parameters, body);
+
+ // It does not matter which URI is actually used here, we just need to construct absolute URI to be able to access `uri.Query`.
+ Uri baseUri = new Uri("http://127.0.0.1");
+ Uri uri = new Uri(baseUri, relativeUri);
+ NameValueCollection parameters = HttpUtility.ParseQueryString(uri.Query);
+
+ return await OnSendAsync(method, uri.AbsolutePath, parameters, body);
}
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancel = default)
{
- var relativeUri = request.RequestUri?.ToString()?.Replace(TorSocks5EndPoint.ToEndpointString(), "");
- var body = (request.Content is { })
+ string body = (request.Content is { })
? await request.Content.ReadAsStringAsync()
: "";
- var sepPos = relativeUri.IndexOf('?');
- var action = relativeUri[..sepPos];
- var parameters = relativeUri[(sepPos + 1)..].Split('&', StringSplitOptions.RemoveEmptyEntries);
- return await OnSendAsync(request.Method, action, parameters, body);
+ Uri uri = request.RequestUri;
+ NameValueCollection parameters = HttpUtility.ParseQueryString(uri.Query);
+
+ return await OnSendAsync(request.Method, uri.AbsolutePath, parameters, body);
}
#region IDisposable Support

MODIFY
WalletWasabi.Tests/UnitTests/Clients/WasabiClientTests.cs
WalletWasabi.Tests/UnitTests/Clients/WasabiClientTests.cs
using NBitcoin;
using Newtonsoft.Json;
using System;
+using System.Collections.Generic;
+using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Net.Http;
@@ -20,11 +22,12 @@ namespace WalletWasabi.Tests.UnitTests.Clients
{
var mempool = Enumerable.Range(0, 1_100).Select(_ => CreateTransaction()).ToArray();
- Task<HttpResponseMessage> FakeServerCode(HttpMethod method, string action, string[] parameters, string body)
+ Task<HttpResponseMessage> FakeServerCode(HttpMethod method, string action, NameValueCollection parameters, string body)
{
- Assert.True(parameters.Length <= 10);
- var requestedTxId = parameters.Select(p => uint256.Parse(p[(p.IndexOf('=') + 1)..]));
- var result = mempool.Where(tx => requestedTxId.Contains(tx.GetHash())).Select(tx => tx.ToHex());
+ Assert.True(parameters.Count <= 10);
+
+ IEnumerable<uint256> requestedTxIds = parameters["transactionIds"].Split(",").Select(x => uint256.Parse(x));
+ IEnumerable<string> result = mempool.Where(tx => requestedTxIds.Contains(tx.GetHash())).Select(tx => tx.ToHex());
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(JsonConvert.SerializeObject(result));

MODIFY
WalletWasabi.Tests/UnitTests/Transactions/PayjoinTests.cs
WalletWasabi.Tests/UnitTests/Transactions/PayjoinTests.cs
@@ -20,6 +20,7 @@ using WalletWasabi.Helpers;
using WalletWasabi.Stores;
using WalletWasabi.Blockchain.Blocks;
using WalletWasabi.Blockchain.Mempool;
+using System.Collections.Specialized;
namespace WalletWasabi.Tests.UnitTests.Transactions
{
@@ -360,11 +361,11 @@ namespace WalletWasabi.Tests.UnitTests.Transactions
Assert.Single(tx.Transaction.Transaction.Inputs);
}
- private static Func<HttpMethod, string, string[], string, Task<HttpResponseMessage>> PayjoinServer(Func<PSBT, PSBT> transformPsbt = null, HttpStatusCode statusCode = HttpStatusCode.OK)
+ private static Func<HttpMethod, string, NameValueCollection, string, Task<HttpResponseMessage>> PayjoinServer(Func<PSBT, PSBT> transformPsbt = null, HttpStatusCode statusCode = HttpStatusCode.OK)
{
if (transformPsbt is { })
{
- return new Func<HttpMethod, string, string[], string, Task<HttpResponseMessage>>((method, path, parameters, requestBody) =>
+ return new Func<HttpMethod, string, NameValueCollection, string, Task<HttpResponseMessage>>((method, path, parameters, requestBody) =>
{
var psbt = PSBT.Parse(requestBody, Network.Main);
var newPsbt = transformPsbt(psbt);
@@ -375,7 +376,7 @@ namespace WalletWasabi.Tests.UnitTests.Transactions
}
else
{
- return new Func<HttpMethod, string, string[], string, Task<HttpResponseMessage>>((method, path, parameters, requestBody) =>
+ return new Func<HttpMethod, string, NameValueCollection, string, Task<HttpResponseMessage>>((method, path, parameters, requestBody) =>
{
var message = new HttpResponseMessage(statusCode);
message.ReasonPhrase = "";

Improve URI handling in `MockTorHttpClient`.