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
79 changes: 79 additions & 0 deletions RestSharp.IntegrationTests/MultipartFormDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.IO;
using System.Net;
using System.Threading;

using RestSharp.IntegrationTests.Helpers;

Expand Down Expand Up @@ -58,6 +59,84 @@ public void MultipartFormData()
}
}

[Fact]
public void AlwaysMultipartFormData_WithParameter_Execute()
{
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_ExecuteTaskAsync()
{
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 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))
{
client.ExecuteAsync(
request,
response =>
{
syncResponse = response;
eventWaitHandle.Set();
});

eventWaitHandle.WaitOne();
}

Assert.Null(syncResponse.ErrorException);
}
}

private static void EchoHandler(HttpListenerContext obj)
{
obj.Response.StatusCode = 200;
Expand Down
40 changes: 18 additions & 22 deletions RestSharp/Http.Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private HttpResponse PostPutInternal(string method)
{
var webRequest = ConfigureWebRequest(method, Url);

PreparePostData(webRequest);
PreparePostBody(webRequest);

WriteRequestBody(webRequest);
return GetResponse(webRequest);
Expand Down Expand Up @@ -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);
}
}
}

Expand Down