I have some unit tests that use the very useful HttpResultsFilter
for mocking/testing results. However I’m switching some of the calls to be async and I can’t see how to do equivalent. I read the [wiki][1] and looked at the [HttpUtilsMockTests][2] but these are all for sync calls and I can’t see equivalent tests for async - is this something that’s possible?
E.g.
[Fact]
public void SyncTest()
{
HttpWebRequest webRequest = null;
using (new HttpResultsFilter
{
StringResultFn = (request, s) =>
{
webRequest = request;
return "result";
}
})
{
const string url = "http://127.0.0.1:8500/v1/kv/testKey";
url.SendStringToUrl("GET", accept: MimeTypes.Json);
webRequest.RequestUri.Should().Be(url);
}
}
works fine but
[Fact]
public async Task AsyncTest()
{
HttpWebRequest webRequest = null;
using (new HttpResultsFilter
{
StringResultFn = (request, s) =>
{
webRequest = request;
return "result";
}
})
{
const string url = "http://127.0.0.1:8500/v1/kv/testKey";
await url.SendStringToUrlAsync("GET", accept: MimeTypes.Json);
webRequest.RequestUri.Should().Be(url);
}
}
Tries to make the actual HTTP request and I can’t intercept it at all.
[1]: https://github.com/ServiceStack/ServiceStack/wiki/Http-Utils#user-content-http-utils-are-mockable
[2]: https://github.com/ServiceStack/ServiceStack.Text/blob/master/tests/ServiceStack.Text.Tests/HttpUtilsMockTests.cs