Skip to content

Commit

Permalink
simplify SimulateTimeout by using SimulateException
Browse files Browse the repository at this point in the history
  • Loading branch information
tmenier committed Apr 25, 2022
1 parent 78bbcd4 commit ae633b4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
22 changes: 16 additions & 6 deletions Test/Flurl.Test/Http/TestingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,23 @@ public async Task can_simulate_exception() {
[Test]
public async Task can_simulate_timeout_with_exception_handled() {
HttpTest.SimulateTimeout();
var result = await "http://www.api.com"
.ConfigureRequest(c => c.OnError = call => call.ExceptionHandled = true)
.GetAsync();
Assert.IsNull(result);
}
var exceptionCaught = false;

var resp = await "http://api.com"
.ConfigureRequest(c => c.OnError = call => {
exceptionCaught = true;
var ex = call.Exception as TaskCanceledException;
Assert.NotNull(ex);
Assert.IsInstanceOf<TimeoutException>(ex.InnerException);
call.ExceptionHandled = true;
})
.GetAsync();

Assert.IsNull(resp);
Assert.IsTrue(exceptionCaught);
}

[Test]
[Test]
public async Task can_fake_headers() {
HttpTest.RespondWith(headers: new { h1 = "foo" });

Expand Down
7 changes: 1 addition & 6 deletions src/Flurl.Http/Testing/FakeHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
Content = new StringContent("")
};

var tcs = new TaskCompletionSource<HttpResponseMessage>();
if (resp is TimeoutResponseMessage)
tcs.SetCanceled();
else
tcs.SetResult(resp);
return tcs.Task;
return Task.FromResult(resp);
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/Flurl.Http/Testing/HttpTestSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Flurl.Http.Configuration;
using Flurl.Http.Content;
using Flurl.Util;
Expand Down Expand Up @@ -105,16 +106,13 @@ public HttpTestSetup RespondWith(Func<HttpContent> buildContent = null, int stat
/// <summary>
/// Adds a simulated timeout response to the response queue.
/// </summary>
public HttpTestSetup SimulateTimeout() {
_responses.Add(() => new TimeoutResponseMessage());
return this;
}
public HttpTestSetup SimulateTimeout() =>
SimulateException(new TaskCanceledException(null, new TimeoutException())); // inner exception covers the .net5+ case https://stackoverflow.com/a/65989456/62600

/// <summary>
/// Add the throwing of an exception to the response queue
/// Adds the throwing of an exception to the response queue.
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
/// <param name="exception">The exception to throw when the call is simulated.</param>
public HttpTestSetup SimulateException(Exception exception) {
_responses.Add(() => throw exception);
return this;
Expand Down
8 changes: 0 additions & 8 deletions src/Flurl.Http/Testing/TimeoutResponseMessage.cs

This file was deleted.

0 comments on commit ae633b4

Please sign in to comment.