From ac24d7be4ac3f8f2258c61785c83636592a304e8 Mon Sep 17 00:00:00 2001 From: issafram Date: Mon, 13 Apr 2015 21:12:32 -0400 Subject: [PATCH 1/3] Add unit tests for Sync and Async MultpartFormData_WithParameter. --- .../MultipartFormDataTests.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/RestSharp.IntegrationTests/MultipartFormDataTests.cs b/RestSharp.IntegrationTests/MultipartFormDataTests.cs index 611fb11ff..14518c660 100644 --- a/RestSharp.IntegrationTests/MultipartFormDataTests.cs +++ b/RestSharp.IntegrationTests/MultipartFormDataTests.cs @@ -1,8 +1,11 @@ namespace RestSharp.IntegrationTests { using System; + using System.Diagnostics; using System.IO; using System.Net; + using System.Threading; + using System.Threading.Tasks; using RestSharp.IntegrationTests.Helpers; @@ -58,6 +61,59 @@ public void MultipartFormData() } } + [Fact] + public void AlwaysMultipartFormData_WithParameter() + { + const string baseUrl = "http://localhost:8888/"; + + using (SimpleServer.Create(baseUrl, EchoHandler)) + { + var client = new RestClient(baseUrl); + var request = new RestRequest("?json_route=/posts") + { + AlwaysMultipartFormData = true, + Method = Method.POST, + }; + request.AddParameter("title", "test", ParameterType.RequestBody); + + var response = client.Execute(request); + Assert.Null(response.ErrorException); + } + } + + [Fact] + public void AlwaysMultipartFormData_WithParameter_Async() + { + const string baseUrl = "http://localhost:8888/"; + + using (SimpleServer.Create(baseUrl, EchoHandler)) + { + var client = new RestClient(baseUrl); + var request = new RestRequest("?json_route=/posts") + { + AlwaysMultipartFormData = true, + Method = Method.POST, + }; + request.AddParameter("title", "test", ParameterType.RequestBody); + IRestResponse syncResponse = null; + + using (var eventWaitHandle = new AutoResetEvent(false)) + { + client.ExecuteAsync( + request, + response => + { + syncResponse = response; + eventWaitHandle.Set(); + }); + + eventWaitHandle.WaitOne(); + } + + Assert.Null(syncResponse.ErrorException); + } + } + private static void EchoHandler(HttpListenerContext obj) { obj.Response.StatusCode = 200; From 245ef57a994997f1d3cb7c50b7366a6827ff99ff Mon Sep 17 00:00:00 2001 From: issafram Date: Fri, 17 Apr 2015 23:33:12 -0400 Subject: [PATCH 2/3] Added unit test for AlwaysMultipartFormData_WithParameter_ExecuteTaskAsync. --- .../MultipartFormDataTests.cs | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/RestSharp.IntegrationTests/MultipartFormDataTests.cs b/RestSharp.IntegrationTests/MultipartFormDataTests.cs index 14518c660..f13448b41 100644 --- a/RestSharp.IntegrationTests/MultipartFormDataTests.cs +++ b/RestSharp.IntegrationTests/MultipartFormDataTests.cs @@ -1,11 +1,9 @@ namespace RestSharp.IntegrationTests { using System; - using System.Diagnostics; using System.IO; using System.Net; using System.Threading; - using System.Threading.Tasks; using RestSharp.IntegrationTests.Helpers; @@ -62,7 +60,7 @@ public void MultipartFormData() } [Fact] - public void AlwaysMultipartFormData_WithParameter() + public void AlwaysMultipartFormData_WithParameter_Execute() { const string baseUrl = "http://localhost:8888/"; @@ -82,7 +80,7 @@ public void AlwaysMultipartFormData_WithParameter() } [Fact] - public void AlwaysMultipartFormData_WithParameter_Async() + public void AlwaysMultipartFormData_WithParameter_ExecuteTaskAsync() { const string baseUrl = "http://localhost:8888/"; @@ -95,6 +93,31 @@ public void AlwaysMultipartFormData_WithParameter_Async() Method = Method.POST, }; request.AddParameter("title", "test", ParameterType.RequestBody); + + var task = client.ExecuteTaskAsync(request).ContinueWith( + x => + { + Assert.Null(x.Result.ErrorException); + }); + + task.Wait(); + } + } + + [Fact] + public void AlwaysMultipartFormData_WithParameter_ExecuteAsync() + { + const string baseUrl = "http://localhost:8888/"; + + using (SimpleServer.Create(baseUrl, EchoHandler)) + { + var client = new RestClient(baseUrl); + var request = new RestRequest("?json_route=/posts") + { + AlwaysMultipartFormData = true, + Method = Method.POST, + }; + request.AddParameter("title", "test", ParameterType.RequestBody); IRestResponse syncResponse = null; using (var eventWaitHandle = new AutoResetEvent(false)) @@ -102,10 +125,10 @@ public void AlwaysMultipartFormData_WithParameter_Async() client.ExecuteAsync( request, response => - { - syncResponse = response; - eventWaitHandle.Set(); - }); + { + syncResponse = response; + eventWaitHandle.Set(); + }); eventWaitHandle.WaitOne(); } From ede74573f5d6ae1d9eceeeb268149c540d02a860 Mon Sep 17 00:00:00 2001 From: issafram Date: Sat, 18 Apr 2015 01:00:40 -0400 Subject: [PATCH 3/3] Copy Async logic to Http.Sync class. Remove PreparePostData method. --- RestSharp/Http.Sync.cs | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/RestSharp/Http.Sync.cs b/RestSharp/Http.Sync.cs index c11e46163..a13965008 100644 --- a/RestSharp/Http.Sync.cs +++ b/RestSharp/Http.Sync.cs @@ -140,7 +140,7 @@ private HttpResponse PostPutInternal(string method) { var webRequest = ConfigureWebRequest(method, Url); - PreparePostData(webRequest); + PreparePostBody(webRequest); WriteRequestBody(webRequest); return GetResponse(webRequest); @@ -214,33 +214,29 @@ private static HttpWebResponse GetRawResponse(HttpWebRequest request) } } - private void PreparePostData(HttpWebRequest webRequest) + private void WriteRequestBody(HttpWebRequest webRequest) { - if (HasFiles || AlwaysMultipartFormData) + if (HasBody || HasFiles || AlwaysMultipartFormData) { - webRequest.ContentType = GetMultipartFormContentType(); - - using (var requestStream = webRequest.GetRequestStream()) - { - WriteMultipartFormData(requestStream); - } +#if !WINDOWS_PHONE && !PocketPC + webRequest.ContentLength = CalculateContentLength(); +#endif } - PreparePostBody(webRequest); - } - - private void WriteRequestBody(HttpWebRequest webRequest) - { - if (!HasBody) - return; - - var bytes = this.RequestBodyBytes ?? this.Encoding.GetBytes(this.RequestBody); - - webRequest.ContentLength = bytes.Length; - using (var requestStream = webRequest.GetRequestStream()) { - requestStream.Write(bytes, 0, bytes.Length); + if (HasFiles || AlwaysMultipartFormData) + { + WriteMultipartFormData(requestStream); + } + else if (RequestBodyBytes != null) + { + requestStream.Write(RequestBodyBytes, 0, RequestBodyBytes.Length); + } + else + { + WriteStringTo(requestStream, RequestBody); + } } }