2017-09-25 18:31:43 +02:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
2019-01-06 07:04:30 +01:00
|
|
|
|
using System.Threading.Channels;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Newtonsoft.Json;
|
2017-09-25 18:31:43 +02:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public class CustomServer : IDisposable
|
|
|
|
|
{
|
|
|
|
|
IWebHost _Host = null;
|
|
|
|
|
CancellationTokenSource _Closed = new CancellationTokenSource();
|
2019-01-06 07:04:30 +01:00
|
|
|
|
Channel<JObject> _Requests = Channel.CreateUnbounded<JObject>();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public CustomServer()
|
2019-01-06 07:04:30 +01:00
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var port = Utils.FreeTcpPort();
|
|
|
|
|
_Host = new WebHostBuilder()
|
|
|
|
|
.Configure(app =>
|
|
|
|
|
{
|
2019-10-09 10:06:00 +02:00
|
|
|
|
app.Run(async req =>
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2019-10-09 10:06:00 +02:00
|
|
|
|
await _Requests.Writer.WriteAsync(JsonConvert.DeserializeObject<JObject>(await new StreamReader(req.Request.Body).ReadToEndAsync()), _Closed.Token);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
req.Response.StatusCode = 200;
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.UseKestrel()
|
|
|
|
|
.UseUrls("http://127.0.0.1:" + port)
|
|
|
|
|
.Build();
|
|
|
|
|
_Host.Start();
|
|
|
|
|
}
|
2017-09-25 18:31:43 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public Uri GetUri()
|
|
|
|
|
{
|
|
|
|
|
return new Uri(_Host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
|
|
|
|
|
}
|
2017-09-25 18:31:43 +02:00
|
|
|
|
|
2019-01-06 07:04:30 +01:00
|
|
|
|
public async Task<JObject> GetNextRequest()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2019-01-06 07:04:30 +01:00
|
|
|
|
using (CancellationTokenSource cancellation = new CancellationTokenSource(2000000))
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2019-01-06 07:04:30 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
JObject req = null;
|
|
|
|
|
while(!await _Requests.Reader.WaitToReadAsync(cancellation.Token) ||
|
|
|
|
|
!_Requests.Reader.TryRead(out req))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return req;
|
|
|
|
|
}
|
|
|
|
|
catch (TaskCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw new Xunit.Sdk.XunitException("Callback to the webserver was expected, check if the callback url is accessible from internet");
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-25 18:31:43 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_Closed.Cancel();
|
|
|
|
|
_Host.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-25 18:31:43 +02:00
|
|
|
|
}
|