From 7d4674da9485d4e6e6d8b9086aad180d22176155 Mon Sep 17 00:00:00 2001 From: Jesse Webb Date: Mon, 16 Sep 2013 02:29:01 -0600 Subject: [PATCH 1/3] RequestBody now supported by DELETE and OPTIONS --- .../RequestBodyTests.cs | 182 ++++++++++++++++++ .../RestSharp.IntegrationTests.csproj | 1 + RestSharp/Http.Sync.cs | 10 + 3 files changed, 193 insertions(+) create mode 100644 RestSharp.IntegrationTests/RequestBodyTests.cs diff --git a/RestSharp.IntegrationTests/RequestBodyTests.cs b/RestSharp.IntegrationTests/RequestBodyTests.cs new file mode 100644 index 000000000..46e73bd2e --- /dev/null +++ b/RestSharp.IntegrationTests/RequestBodyTests.cs @@ -0,0 +1,182 @@ +using System.IO; +using System.Net; +using RestSharp.IntegrationTests.Helpers; +using Xunit; + +namespace RestSharp.IntegrationTests +{ + public class RequestBodyTests + { + private const string BASE_URL = "http://localhost:8080/"; + + [Fact] + public void Can_Not_Be_Added_To_GET_Request() + { + const Method httpMethod = Method.GET; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasNoRequestBody(); + } + } + + [Fact] + public void Can_Be_Added_To_POST_Request() + { + const Method httpMethod = Method.POST; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasRequestBody(contentType, bodyData); + } + } + + [Fact] + public void Can_Be_Added_To_PUT_Request() + { + const Method httpMethod = Method.PUT; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasRequestBody(contentType, bodyData); + } + } + + [Fact] + public void Can_Be_Added_To_DELETE_Request() + { + const Method httpMethod = Method.DELETE; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasRequestBody(contentType, bodyData); + } + } + + [Fact] + public void Can_Not_Be_Added_To_HEAD_Request() + { + const Method httpMethod = Method.HEAD; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasNoRequestBody(); + } + } + + [Fact] + public void Can_Be_Added_To_OPTIONS_Request() + { + const Method httpMethod = Method.OPTIONS; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasRequestBody(contentType, bodyData); + } + } + + [Fact] + public void Can_Be_Added_To_PATCH_Request() + { + const Method httpMethod = Method.PATCH; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasRequestBody(contentType, bodyData); + } + } + + private static void AssertHasNoRequestBody() + { + Assert.Null(RequestBodyCapturer.CapturedContentType); + Assert.Equal(false, RequestBodyCapturer.CapturedHasEntityBody); + Assert.Equal(string.Empty, RequestBodyCapturer.CapturedEntityBody); + } + + private static void AssertHasRequestBody(string contentType, string bodyData) + { + Assert.Equal(contentType, RequestBodyCapturer.CapturedContentType); + Assert.Equal(true, RequestBodyCapturer.CapturedHasEntityBody); + Assert.Equal(bodyData, RequestBodyCapturer.CapturedEntityBody); + } + + private class RequestBodyCapturer + { + public const string RESOURCE = "Capture"; + + public static string CapturedContentType { get; set; } + public static bool CapturedHasEntityBody { get; set; } + public static string CapturedEntityBody { get; set; } + + public static void Capture(HttpListenerContext context) + { + var request = context.Request; + CapturedContentType = request.ContentType; + CapturedHasEntityBody = request.HasEntityBody; + CapturedEntityBody = StreamToString(request.InputStream); + } + + private static string StreamToString(Stream stream) + { + var streamReader = new StreamReader(stream); + return streamReader.ReadToEnd(); + } + } + } +} diff --git a/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj b/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj index 83c7da106..b5f3b79b7 100644 --- a/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj +++ b/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj @@ -71,6 +71,7 @@ + diff --git a/RestSharp/Http.Sync.cs b/RestSharp/Http.Sync.cs index de3f608a9..9e4d095b9 100644 --- a/RestSharp/Http.Sync.cs +++ b/RestSharp/Http.Sync.cs @@ -111,6 +111,16 @@ private HttpResponse GetStyleMethodInternal(string method) { var webRequest = ConfigureWebRequest(method, Url); + + if (method == "DELETE" || method == "OPTIONS") + { + if (HasBody) + { + webRequest.ContentType = RequestContentType; + WriteRequestBody(webRequest); + } + } + return GetResponse(webRequest); } From a8a31edd4aa65dbb8737f5ce08db3dae8a785606 Mon Sep 17 00:00:00 2001 From: Jesse Webb Date: Mon, 16 Sep 2013 02:39:22 -0600 Subject: [PATCH 2/3] Convert spaces to Tabs --- .../RequestBodyTests.cs | 348 +++++++++--------- RestSharp/Http.Sync.cs | 18 +- 2 files changed, 183 insertions(+), 183 deletions(-) diff --git a/RestSharp.IntegrationTests/RequestBodyTests.cs b/RestSharp.IntegrationTests/RequestBodyTests.cs index 46e73bd2e..3b61d6327 100644 --- a/RestSharp.IntegrationTests/RequestBodyTests.cs +++ b/RestSharp.IntegrationTests/RequestBodyTests.cs @@ -5,178 +5,178 @@ namespace RestSharp.IntegrationTests { - public class RequestBodyTests - { - private const string BASE_URL = "http://localhost:8080/"; - - [Fact] - public void Can_Not_Be_Added_To_GET_Request() - { - const Method httpMethod = Method.GET; - using (SimpleServer.Create(BASE_URL, Handlers.Generic())) - { - var client = new RestClient(BASE_URL); - var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); - - const string contentType = "text/plain"; - const string bodyData = "abc123 foo bar baz BING!"; - request.AddParameter(contentType, bodyData, ParameterType.RequestBody); - - client.Execute(request); - - AssertHasNoRequestBody(); - } - } - - [Fact] - public void Can_Be_Added_To_POST_Request() - { - const Method httpMethod = Method.POST; - using (SimpleServer.Create(BASE_URL, Handlers.Generic())) - { - var client = new RestClient(BASE_URL); - var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); - - const string contentType = "text/plain"; - const string bodyData = "abc123 foo bar baz BING!"; - request.AddParameter(contentType, bodyData, ParameterType.RequestBody); - - client.Execute(request); - - AssertHasRequestBody(contentType, bodyData); - } - } - - [Fact] - public void Can_Be_Added_To_PUT_Request() - { - const Method httpMethod = Method.PUT; - using (SimpleServer.Create(BASE_URL, Handlers.Generic())) - { - var client = new RestClient(BASE_URL); - var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); - - const string contentType = "text/plain"; - const string bodyData = "abc123 foo bar baz BING!"; - request.AddParameter(contentType, bodyData, ParameterType.RequestBody); - - client.Execute(request); - - AssertHasRequestBody(contentType, bodyData); - } - } - - [Fact] - public void Can_Be_Added_To_DELETE_Request() - { - const Method httpMethod = Method.DELETE; - using (SimpleServer.Create(BASE_URL, Handlers.Generic())) - { - var client = new RestClient(BASE_URL); - var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); - - const string contentType = "text/plain"; - const string bodyData = "abc123 foo bar baz BING!"; - request.AddParameter(contentType, bodyData, ParameterType.RequestBody); - - client.Execute(request); - - AssertHasRequestBody(contentType, bodyData); - } - } - - [Fact] - public void Can_Not_Be_Added_To_HEAD_Request() - { - const Method httpMethod = Method.HEAD; - using (SimpleServer.Create(BASE_URL, Handlers.Generic())) - { - var client = new RestClient(BASE_URL); - var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); - - const string contentType = "text/plain"; - const string bodyData = "abc123 foo bar baz BING!"; - request.AddParameter(contentType, bodyData, ParameterType.RequestBody); - - client.Execute(request); - - AssertHasNoRequestBody(); - } - } - - [Fact] - public void Can_Be_Added_To_OPTIONS_Request() - { - const Method httpMethod = Method.OPTIONS; - using (SimpleServer.Create(BASE_URL, Handlers.Generic())) - { - var client = new RestClient(BASE_URL); - var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); - - const string contentType = "text/plain"; - const string bodyData = "abc123 foo bar baz BING!"; - request.AddParameter(contentType, bodyData, ParameterType.RequestBody); - - client.Execute(request); - - AssertHasRequestBody(contentType, bodyData); - } - } - - [Fact] - public void Can_Be_Added_To_PATCH_Request() - { - const Method httpMethod = Method.PATCH; - using (SimpleServer.Create(BASE_URL, Handlers.Generic())) - { - var client = new RestClient(BASE_URL); - var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); - - const string contentType = "text/plain"; - const string bodyData = "abc123 foo bar baz BING!"; - request.AddParameter(contentType, bodyData, ParameterType.RequestBody); - - client.Execute(request); - - AssertHasRequestBody(contentType, bodyData); - } - } - - private static void AssertHasNoRequestBody() - { - Assert.Null(RequestBodyCapturer.CapturedContentType); - Assert.Equal(false, RequestBodyCapturer.CapturedHasEntityBody); - Assert.Equal(string.Empty, RequestBodyCapturer.CapturedEntityBody); - } - - private static void AssertHasRequestBody(string contentType, string bodyData) - { - Assert.Equal(contentType, RequestBodyCapturer.CapturedContentType); - Assert.Equal(true, RequestBodyCapturer.CapturedHasEntityBody); - Assert.Equal(bodyData, RequestBodyCapturer.CapturedEntityBody); - } - - private class RequestBodyCapturer - { - public const string RESOURCE = "Capture"; - - public static string CapturedContentType { get; set; } - public static bool CapturedHasEntityBody { get; set; } - public static string CapturedEntityBody { get; set; } - - public static void Capture(HttpListenerContext context) - { - var request = context.Request; - CapturedContentType = request.ContentType; - CapturedHasEntityBody = request.HasEntityBody; - CapturedEntityBody = StreamToString(request.InputStream); - } - - private static string StreamToString(Stream stream) - { - var streamReader = new StreamReader(stream); - return streamReader.ReadToEnd(); - } - } - } + public class RequestBodyTests + { + private const string BASE_URL = "http://localhost:8080/"; + + [Fact] + public void Can_Not_Be_Added_To_GET_Request() + { + const Method httpMethod = Method.GET; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasNoRequestBody(); + } + } + + [Fact] + public void Can_Be_Added_To_POST_Request() + { + const Method httpMethod = Method.POST; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasRequestBody(contentType, bodyData); + } + } + + [Fact] + public void Can_Be_Added_To_PUT_Request() + { + const Method httpMethod = Method.PUT; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasRequestBody(contentType, bodyData); + } + } + + [Fact] + public void Can_Be_Added_To_DELETE_Request() + { + const Method httpMethod = Method.DELETE; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasRequestBody(contentType, bodyData); + } + } + + [Fact] + public void Can_Not_Be_Added_To_HEAD_Request() + { + const Method httpMethod = Method.HEAD; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasNoRequestBody(); + } + } + + [Fact] + public void Can_Be_Added_To_OPTIONS_Request() + { + const Method httpMethod = Method.OPTIONS; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasRequestBody(contentType, bodyData); + } + } + + [Fact] + public void Can_Be_Added_To_PATCH_Request() + { + const Method httpMethod = Method.PATCH; + using (SimpleServer.Create(BASE_URL, Handlers.Generic())) + { + var client = new RestClient(BASE_URL); + var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod); + + const string contentType = "text/plain"; + const string bodyData = "abc123 foo bar baz BING!"; + request.AddParameter(contentType, bodyData, ParameterType.RequestBody); + + client.Execute(request); + + AssertHasRequestBody(contentType, bodyData); + } + } + + private static void AssertHasNoRequestBody() + { + Assert.Null(RequestBodyCapturer.CapturedContentType); + Assert.Equal(false, RequestBodyCapturer.CapturedHasEntityBody); + Assert.Equal(string.Empty, RequestBodyCapturer.CapturedEntityBody); + } + + private static void AssertHasRequestBody(string contentType, string bodyData) + { + Assert.Equal(contentType, RequestBodyCapturer.CapturedContentType); + Assert.Equal(true, RequestBodyCapturer.CapturedHasEntityBody); + Assert.Equal(bodyData, RequestBodyCapturer.CapturedEntityBody); + } + + private class RequestBodyCapturer + { + public const string RESOURCE = "Capture"; + + public static string CapturedContentType { get; set; } + public static bool CapturedHasEntityBody { get; set; } + public static string CapturedEntityBody { get; set; } + + public static void Capture(HttpListenerContext context) + { + var request = context.Request; + CapturedContentType = request.ContentType; + CapturedHasEntityBody = request.HasEntityBody; + CapturedEntityBody = StreamToString(request.InputStream); + } + + private static string StreamToString(Stream stream) + { + var streamReader = new StreamReader(stream); + return streamReader.ReadToEnd(); + } + } + } } diff --git a/RestSharp/Http.Sync.cs b/RestSharp/Http.Sync.cs index 9e4d095b9..7bf31243b 100644 --- a/RestSharp/Http.Sync.cs +++ b/RestSharp/Http.Sync.cs @@ -107,19 +107,19 @@ public HttpResponse AsPost(string httpMethod) return PostPutInternal(httpMethod.ToUpperInvariant()); } - private HttpResponse GetStyleMethodInternal(string method) + private HttpResponse GetStyleMethodInternal(string method) { var webRequest = ConfigureWebRequest(method, Url); - if (method == "DELETE" || method == "OPTIONS") - { - if (HasBody) - { - webRequest.ContentType = RequestContentType; - WriteRequestBody(webRequest); - } - } + if (method == "DELETE" || method == "OPTIONS") + { + if (HasBody) + { + webRequest.ContentType = RequestContentType; + WriteRequestBody(webRequest); + } + } return GetResponse(webRequest); } From 8ae623515b61c153a77d46888314d1ae3d02d2fa Mon Sep 17 00:00:00 2001 From: Haacked Date: Mon, 16 Sep 2013 17:16:31 -0700 Subject: [PATCH 3/3] Merge if statements --- RestSharp/Http.Sync.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/RestSharp/Http.Sync.cs b/RestSharp/Http.Sync.cs index 7bf31243b..6ed9c65bc 100644 --- a/RestSharp/Http.Sync.cs +++ b/RestSharp/Http.Sync.cs @@ -111,14 +111,10 @@ private HttpResponse GetStyleMethodInternal(string method) { var webRequest = ConfigureWebRequest(method, Url); - - if (method == "DELETE" || method == "OPTIONS") + if (HasBody && (method == "DELETE" || method == "OPTIONS")) { - if (HasBody) - { - webRequest.ContentType = RequestContentType; - WriteRequestBody(webRequest); - } + webRequest.ContentType = RequestContentType; + WriteRequestBody(webRequest); } return GetResponse(webRequest);