Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 197 additions & 0 deletions RestSharp.IntegrationTests/AsyncRequestBodyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
using System.IO;
using System.Net;
using System.Threading;
using RestSharp.IntegrationTests.Helpers;
using Xunit;

namespace RestSharp.IntegrationTests
{
public class AsyncRequestBodyTests
{
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<RequestBodyCapturer>()))
{
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);

var resetEvent = new ManualResetEvent(false);
client.ExecuteAsync(request, response => resetEvent.Set());
resetEvent.WaitOne();

AssertHasNoRequestBody();
}
}

[Fact]
public void Can_Be_Added_To_POST_Request()
{
const Method httpMethod = Method.POST;
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
{
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);

var resetEvent = new ManualResetEvent(false);
client.ExecuteAsync(request, response => resetEvent.Set());
resetEvent.WaitOne();

AssertHasRequestBody(contentType, bodyData);
}
}

[Fact]
public void Can_Be_Added_To_PUT_Request()
{
const Method httpMethod = Method.PUT;
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
{
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);

var resetEvent = new ManualResetEvent(false);
client.ExecuteAsync(request, response => resetEvent.Set());
resetEvent.WaitOne();

AssertHasRequestBody(contentType, bodyData);
}
}

[Fact]
public void Can_Be_Added_To_DELETE_Request()
{
const Method httpMethod = Method.DELETE;
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
{
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);

var resetEvent = new ManualResetEvent(false);
client.ExecuteAsync(request, response => resetEvent.Set());
resetEvent.WaitOne();

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<RequestBodyCapturer>()))
{
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);

var resetEvent = new ManualResetEvent(false);
client.ExecuteAsync(request, response => resetEvent.Set());
resetEvent.WaitOne();

AssertHasNoRequestBody();
}
}

[Fact]
public void Can_Be_Added_To_OPTIONS_Request()
{
const Method httpMethod = Method.OPTIONS;
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
{
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);

var resetEvent = new ManualResetEvent(false);
client.ExecuteAsync(request, response => resetEvent.Set());
resetEvent.WaitOne();

AssertHasRequestBody(contentType, bodyData);
}
}

[Fact]
public void Can_Be_Added_To_PATCH_Request()
{
const Method httpMethod = Method.PATCH;
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
{
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);

var resetEvent = new ManualResetEvent(false);
client.ExecuteAsync(request, response => resetEvent.Set());
resetEvent.WaitOne();

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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<ItemGroup>
<Compile Include="AsyncTests.cs" />
<Compile Include="AuthenticationTests.cs" />
<Compile Include="AsyncRequestBodyTests.cs" />
<Compile Include="NonProtocolExceptionHandlingTests.cs" />
<Compile Include="Helpers\Extensions.cs" />
<Compile Include="FileTests.cs" />
Expand Down
14 changes: 11 additions & 3 deletions RestSharp/Http.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,17 @@ private HttpWebRequest GetStyleMethodInternalAsync(string method, Action<HttpRes
{
var url = Url;
webRequest = ConfigureAsyncWebRequest(method, url);
_timeoutState = new TimeOutState { Request = webRequest };
var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest);
SetTimeout(asyncResult, _timeoutState);
if (HasBody && (method == "DELETE" || method == "OPTIONS"))
{
webRequest.ContentType = RequestContentType;
WriteRequestBodyAsync(webRequest, callback);
}
else
{
_timeoutState = new TimeOutState { Request = webRequest };
var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest);
SetTimeout(asyncResult, _timeoutState);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, indentation is weird. RestSharp uses Tabs, not spaces (it wasn't my idea, I just inherited it).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, I changed to Keep tabs (Tab size: 4) but it doesn't seem to have been applied everywhere.

}
catch(Exception ex)
{
Expand Down