Skip to content

Commit 9985f6c

Browse files
committed
Merge pull request #461 from chribben/delete-request-with-body
Sending a DELETE reqeust asynchronously with a request body from the cli...
2 parents f547b91 + c39e547 commit 9985f6c

File tree

3 files changed

+209
-3
lines changed

3 files changed

+209
-3
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
using System.IO;
2+
using System.Net;
3+
using System.Threading;
4+
using RestSharp.IntegrationTests.Helpers;
5+
using Xunit;
6+
7+
namespace RestSharp.IntegrationTests
8+
{
9+
public class AsyncRequestBodyTests
10+
{
11+
private const string BASE_URL = "http://localhost:8080/";
12+
13+
[Fact]
14+
public void Can_Not_Be_Added_To_GET_Request()
15+
{
16+
const Method httpMethod = Method.GET;
17+
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
18+
{
19+
var client = new RestClient(BASE_URL);
20+
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
21+
22+
const string contentType = "text/plain";
23+
const string bodyData = "abc123 foo bar baz BING!";
24+
request.AddParameter(contentType, bodyData, ParameterType.RequestBody);
25+
26+
var resetEvent = new ManualResetEvent(false);
27+
client.ExecuteAsync(request, response => resetEvent.Set());
28+
resetEvent.WaitOne();
29+
30+
AssertHasNoRequestBody();
31+
}
32+
}
33+
34+
[Fact]
35+
public void Can_Be_Added_To_POST_Request()
36+
{
37+
const Method httpMethod = Method.POST;
38+
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
39+
{
40+
var client = new RestClient(BASE_URL);
41+
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
42+
43+
const string contentType = "text/plain";
44+
const string bodyData = "abc123 foo bar baz BING!";
45+
request.AddParameter(contentType, bodyData, ParameterType.RequestBody);
46+
47+
var resetEvent = new ManualResetEvent(false);
48+
client.ExecuteAsync(request, response => resetEvent.Set());
49+
resetEvent.WaitOne();
50+
51+
AssertHasRequestBody(contentType, bodyData);
52+
}
53+
}
54+
55+
[Fact]
56+
public void Can_Be_Added_To_PUT_Request()
57+
{
58+
const Method httpMethod = Method.PUT;
59+
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
60+
{
61+
var client = new RestClient(BASE_URL);
62+
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
63+
64+
const string contentType = "text/plain";
65+
const string bodyData = "abc123 foo bar baz BING!";
66+
request.AddParameter(contentType, bodyData, ParameterType.RequestBody);
67+
68+
var resetEvent = new ManualResetEvent(false);
69+
client.ExecuteAsync(request, response => resetEvent.Set());
70+
resetEvent.WaitOne();
71+
72+
AssertHasRequestBody(contentType, bodyData);
73+
}
74+
}
75+
76+
[Fact]
77+
public void Can_Be_Added_To_DELETE_Request()
78+
{
79+
const Method httpMethod = Method.DELETE;
80+
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
81+
{
82+
var client = new RestClient(BASE_URL);
83+
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
84+
85+
const string contentType = "text/plain";
86+
const string bodyData = "abc123 foo bar baz BING!";
87+
request.AddParameter(contentType, bodyData, ParameterType.RequestBody);
88+
89+
var resetEvent = new ManualResetEvent(false);
90+
client.ExecuteAsync(request, response => resetEvent.Set());
91+
resetEvent.WaitOne();
92+
93+
AssertHasRequestBody(contentType, bodyData);
94+
}
95+
}
96+
97+
[Fact]
98+
public void Can_Not_Be_Added_To_HEAD_Request()
99+
{
100+
const Method httpMethod = Method.HEAD;
101+
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
102+
{
103+
var client = new RestClient(BASE_URL);
104+
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
105+
106+
const string contentType = "text/plain";
107+
const string bodyData = "abc123 foo bar baz BING!";
108+
request.AddParameter(contentType, bodyData, ParameterType.RequestBody);
109+
110+
var resetEvent = new ManualResetEvent(false);
111+
client.ExecuteAsync(request, response => resetEvent.Set());
112+
resetEvent.WaitOne();
113+
114+
AssertHasNoRequestBody();
115+
}
116+
}
117+
118+
[Fact]
119+
public void Can_Be_Added_To_OPTIONS_Request()
120+
{
121+
const Method httpMethod = Method.OPTIONS;
122+
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
123+
{
124+
var client = new RestClient(BASE_URL);
125+
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
126+
127+
const string contentType = "text/plain";
128+
const string bodyData = "abc123 foo bar baz BING!";
129+
request.AddParameter(contentType, bodyData, ParameterType.RequestBody);
130+
131+
var resetEvent = new ManualResetEvent(false);
132+
client.ExecuteAsync(request, response => resetEvent.Set());
133+
resetEvent.WaitOne();
134+
135+
AssertHasRequestBody(contentType, bodyData);
136+
}
137+
}
138+
139+
[Fact]
140+
public void Can_Be_Added_To_PATCH_Request()
141+
{
142+
const Method httpMethod = Method.PATCH;
143+
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
144+
{
145+
var client = new RestClient(BASE_URL);
146+
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
147+
148+
const string contentType = "text/plain";
149+
const string bodyData = "abc123 foo bar baz BING!";
150+
request.AddParameter(contentType, bodyData, ParameterType.RequestBody);
151+
152+
var resetEvent = new ManualResetEvent(false);
153+
client.ExecuteAsync(request, response => resetEvent.Set());
154+
resetEvent.WaitOne();
155+
156+
AssertHasRequestBody(contentType, bodyData);
157+
}
158+
}
159+
160+
private static void AssertHasNoRequestBody()
161+
{
162+
Assert.Null(RequestBodyCapturer.CapturedContentType);
163+
Assert.Equal(false, RequestBodyCapturer.CapturedHasEntityBody);
164+
Assert.Equal(string.Empty, RequestBodyCapturer.CapturedEntityBody);
165+
}
166+
167+
private static void AssertHasRequestBody(string contentType, string bodyData)
168+
{
169+
Assert.Equal(contentType, RequestBodyCapturer.CapturedContentType);
170+
Assert.Equal(true, RequestBodyCapturer.CapturedHasEntityBody);
171+
Assert.Equal(bodyData, RequestBodyCapturer.CapturedEntityBody);
172+
}
173+
174+
private class RequestBodyCapturer
175+
{
176+
public const string RESOURCE = "Capture";
177+
178+
public static string CapturedContentType { get; set; }
179+
public static bool CapturedHasEntityBody { get; set; }
180+
public static string CapturedEntityBody { get; set; }
181+
182+
public static void Capture(HttpListenerContext context)
183+
{
184+
var request = context.Request;
185+
CapturedContentType = request.ContentType;
186+
CapturedHasEntityBody = request.HasEntityBody;
187+
CapturedEntityBody = StreamToString(request.InputStream);
188+
}
189+
190+
private static string StreamToString(Stream stream)
191+
{
192+
var streamReader = new StreamReader(stream);
193+
return streamReader.ReadToEnd();
194+
}
195+
}
196+
}
197+
}

RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<ItemGroup>
6565
<Compile Include="AsyncTests.cs" />
6666
<Compile Include="AuthenticationTests.cs" />
67+
<Compile Include="AsyncRequestBodyTests.cs" />
6768
<Compile Include="NonProtocolExceptionHandlingTests.cs" />
6869
<Compile Include="Helpers\Extensions.cs" />
6970
<Compile Include="FileTests.cs" />

RestSharp/Http.Async.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,17 @@ private HttpWebRequest GetStyleMethodInternalAsync(string method, Action<HttpRes
104104
{
105105
var url = Url;
106106
webRequest = ConfigureAsyncWebRequest(method, url);
107-
_timeoutState = new TimeOutState { Request = webRequest };
108-
var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest);
109-
SetTimeout(asyncResult, _timeoutState);
107+
if (HasBody && (method == "DELETE" || method == "OPTIONS"))
108+
{
109+
webRequest.ContentType = RequestContentType;
110+
WriteRequestBodyAsync(webRequest, callback);
111+
}
112+
else
113+
{
114+
_timeoutState = new TimeOutState { Request = webRequest };
115+
var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest);
116+
SetTimeout(asyncResult, _timeoutState);
117+
}
110118
}
111119
catch(Exception ex)
112120
{

0 commit comments

Comments
 (0)