Skip to content

Commit a2e5bc8

Browse files
committed
Merge pull request restsharp#426 from restsharp/delete-options-request-body
Delete options request body
2 parents e8754e7 + 8ae6235 commit a2e5bc8

File tree

3 files changed

+190
-1
lines changed

3 files changed

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

RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<Compile Include="Properties\AssemblyInfo.cs" />
7474
<Compile Include="CompressionTests.cs" />
7575
<Compile Include="Helpers\SimpleServer.cs" />
76+
<Compile Include="RequestBodyTests.cs" />
7677
<Compile Include="StatusCodeTests.cs" />
7778
</ItemGroup>
7879
<ItemGroup>

RestSharp/Http.Sync.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,16 @@ public HttpResponse AsPost(string httpMethod)
107107
return PostPutInternal(httpMethod.ToUpperInvariant());
108108
}
109109

110-
private HttpResponse GetStyleMethodInternal(string method)
110+
private HttpResponse GetStyleMethodInternal(string method)
111111
{
112112
var webRequest = ConfigureWebRequest(method, Url);
113113

114+
if (HasBody && (method == "DELETE" || method == "OPTIONS"))
115+
{
116+
webRequest.ContentType = RequestContentType;
117+
WriteRequestBody(webRequest);
118+
}
119+
114120
return GetResponse(webRequest);
115121
}
116122

0 commit comments

Comments
 (0)