Added an http test method allowing any exception to be raised when a request is made. For the purpose of allowing tests of socket exception and other http exception that can be raised when making an http call

This commit is contained in:
Kyle Alexander 2022-02-17 13:57:39 +00:00
parent 9b9b2a696a
commit dc2b74ed2f
3 changed files with 26 additions and 1 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading.Tasks;
using Flurl.Http;
using Flurl.Http.Testing;
@ -361,7 +362,20 @@ namespace Flurl.Test.Http
}
}
[Test]
[Test]
public async Task can_simulate_excepion() {
var expectedException = new SocketException();
HttpTest.SimulateException(expectedException);
try {
await "http://www.api.com".GetAsync();
Assert.Fail("Exception was not thrown!");
}
catch (FlurlHttpException ex) {
Assert.AreEqual(expectedException, ex.InnerException);
}
}
[Test]
public async Task can_simulate_timeout_with_exception_handled() {
HttpTest.SimulateTimeout();
var result = await "http://www.api.com"

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
using Flurl.Http.Configuration;
namespace Flurl.Http.Testing

View File

@ -110,6 +110,16 @@ namespace Flurl.Http.Testing
return this;
}
/// <summary>
/// Add the throwing of an exception to the response queue
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public HttpTestSetup SimulateException(Exception exception) {
_responses.Add(() => throw exception);
return this;
}
/// <summary>
/// Do NOT fake requests for this setup. Typically called on a filtered setup, i.e. HttpTest.ForCallsTo(urlPattern).AllowRealHttp();
/// </summary>