Merge pull request #162 from thiagolunardi/master

Managing timeout with exception handled
This commit is contained in:
Todd Menier 2017-02-03 14:07:26 -06:00 committed by GitHub
commit 1b4358a5aa
4 changed files with 32 additions and 7 deletions

View File

@ -128,7 +128,16 @@ namespace Flurl.Test.Http
}
}
private class TestData
[Test]
public async Task can_get_null_json_when_timeout_and_exception_handled() {
HttpTest.SimulateTimeout();
var data = await "http://api.com"
.ConfigureClient(c => c.OnError = call => call.ExceptionHandled = true)
.GetJsonAsync<TestData>();
Assert.IsNull(data);
}
private class TestData
{
public int id { get; set; }
public string name { get; set; }

View File

@ -136,7 +136,16 @@ namespace Flurl.Test.Http
}
}
[Test]
[Test]
public async Task can_simulate_timeout_with_exception_handled() {
HttpTest.SimulateTimeout();
var result = await "http://www.api.com"
.ConfigureClient(c => c.OnError = call => call.ExceptionHandled = true)
.GetAsync();
Assert.IsNull(result);
}
[Test]
public async Task can_fake_headers() {
HttpTest.RespondWith(headers: new { h1 = "foo" });

View File

@ -193,8 +193,9 @@ namespace Flurl.Http
/// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param>
/// <returns>A Task whose result is the received HttpResponseMessage.</returns>
public async Task<HttpResponseMessage> SendAsync(HttpMethod verb, HttpContent content = null, CancellationToken? cancellationToken = null, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) {
HttpRequestMessage request = null;
try {
var request = new HttpRequestMessage(verb, Url) { Content = content };
request = new HttpRequestMessage(verb, Url) { Content = content };
if (Settings.CookiesEnabled)
WriteRequestCookies(request);
request.SetFlurlHttpCall(Settings);
@ -202,8 +203,12 @@ namespace Flurl.Http
if (Settings.CookiesEnabled)
ReadResponseCookies(resp);
return resp;
}
finally {
}
catch(InvalidOperationException) {
if (request.GetFlurlHttpCall().ExceptionHandled) return null;
throw;
}
finally {
if (Settings.AutoDispose) Dispose();
}
}

View File

@ -22,9 +22,11 @@ namespace Flurl.Http
/// <returns>A Task whose result is an object containing data in the response body.</returns>
/// <example>x = await url.PostAsync(data).ReceiveJson&lt;T&gt;()</example>
/// <exception cref="FlurlHttpException">Condition.</exception>
public static async Task<T> ReceiveJson<T>(this Task<HttpResponseMessage> response) {
public static async Task<T> ReceiveJson<T>(this Task<HttpResponseMessage> response)
{
var resp = await response.ConfigureAwait(false);
var call = resp.RequestMessage.GetFlurlHttpCall();
if (resp == null) return default(T);
var call = resp.RequestMessage.GetFlurlHttpCall();
try {
using (var stream = await resp.Content.ReadAsStreamAsync().ConfigureAwait(false))
return call.Settings.JsonSerializer.Deserialize<T>(stream);