Permalink
Browse files

Merge pull request #500 from rggardner/timeout-put-async

It appears possible for a PUT request to enter a state where a timeout d...
  • Loading branch information...
2 parents b990048 + 721d9dc commit 821153d8425c3d65123a016c939ade23720c46eb @Haacked Haacked committed Feb 3, 2014
Showing with 50 additions and 1 deletion.
  1. +48 −0 RestSharp.IntegrationTests/AsyncTests.cs
  2. +2 −1 RestSharp/Http.Async.cs
@@ -168,6 +168,54 @@ public void Handles_GET_Request_Errors_TaskAsync_With_Response_Type()
}
}
+ [Fact]
+ public void Can_Timeout_GET_TaskAsync()
+ {
+ const string baseUrl = "http://localhost:8080/";
+ using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
+ {
+ var client = new RestClient(baseUrl);
+ var request = new RestRequest("timeout", Method.GET).AddBody("Body_Content");
+
+ //Half the value of ResponseHandler.Timeout
+ request.Timeout = 500;
+
+ System.AggregateException agg = Assert.Throws<System.AggregateException>(
+ delegate
+ {
+ var task = client.ExecuteTaskAsync(request);
+ task.Wait();
+ });
+
+ Assert.IsType(typeof(WebException), agg.InnerException);
+ Assert.Equal("The request timed-out.", agg.InnerException.Message);
+ }
+ }
+
+ [Fact]
+ public void Can_Timeout_PUT_TaskAsync()
+ {
+ const string baseUrl = "http://localhost:8080/";
+ using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
+ {
+ var client = new RestClient(baseUrl);
+ var request = new RestRequest("timeout", Method.PUT).AddBody("Body_Content");
+
+ //Half the value of ResponseHandler.Timeout
+ request.Timeout = 500;
+
+ System.AggregateException agg = Assert.Throws<System.AggregateException>(
+ delegate
+ {
+ var task = client.ExecuteTaskAsync(request);
+ task.Wait();
+ });
+
+ Assert.IsType(typeof(WebException), agg.InnerException);
+ Assert.Equal("The request timed-out.", agg.InnerException.Message);
+ }
+ }
+
void UrlToStatusCodeHandler(HttpListenerContext obj)
{
obj.Response.StatusCode = int.Parse(obj.Request.Url.Segments.Last());
View
@@ -249,7 +249,8 @@ private void RequestStreamCallback(IAsyncResult result, Action<HttpResponse> cal
return;
}
- webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
+ IAsyncResult asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
+ SetTimeout(asyncResult, _timeoutState);
}
private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState)

0 comments on commit 821153d

Please sign in to comment.