Skip to content

Commit 710d55a

Browse files
committed
Fixing more tests
1 parent 92b9e6f commit 710d55a

File tree

10 files changed

+56
-105
lines changed

10 files changed

+56
-105
lines changed

src/RestSharp/Http.Sync.cs

+29-49
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
using RestSharp.Authenticators.OAuth.Extensions;
1919
using RestSharp.Extensions;
2020

21-
namespace RestSharp
22-
{
21+
namespace RestSharp {
2322
/// <summary>
2423
/// HttpWebRequest wrapper (sync methods)
2524
/// </summary>
26-
public partial class Http
27-
{
25+
public partial class Http {
2826
/// <summary>
2927
/// Execute a POST request
3028
/// </summary>
@@ -81,75 +79,66 @@ public partial class Http
8179

8280
HttpResponse GetStyleMethodInternal(string method)
8381
=> ExecuteRequest(
84-
method, r =>
85-
{
82+
method,
83+
r => {
8684
if (!HasBody) return;
87-
85+
8886
if (!CanGetWithBody())
89-
throw new NotSupportedException($"Http verb {method} does not support body");
87+
throw new NotSupportedException($"HTTP verb {method} does not support body");
9088

9189
r.ContentType = RequestContentType;
9290
WriteRequestBody(r);
9391

94-
bool CanGetWithBody() => method == "DELETE" || method == "OPTIONS";
92+
bool CanGetWithBody() => method is "DELETE" or "OPTIONS";
9593
}
9694
);
9795

9896
HttpResponse PostPutInternal(string method)
9997
=> ExecuteRequest(
100-
method, r =>
101-
{
98+
method,
99+
r => {
102100
PreparePostBody(r);
103101
WriteRequestBody(r);
104102
}
105103
);
106104

107-
HttpResponse ExecuteRequest(string httpMethod, Action<HttpWebRequest> prepareRequest)
108-
{
105+
HttpResponse ExecuteRequest(string httpMethod, Action<HttpWebRequest> prepareRequest) {
109106
var webRequest = ConfigureWebRequest(httpMethod, Url);
110107

111108
prepareRequest(webRequest);
112109

113-
try
114-
{
110+
try {
115111
using var webResponse = GetRawResponse(webRequest);
116112

117113
return ExtractResponseData(webResponse);
118114
}
119-
catch (Exception ex)
120-
{
115+
catch (Exception ex) {
121116
if (ThrowOnAnyError)
122117
throw;
123118

124119
return ExtractErrorResponse(ex);
125120
}
126121

127-
static HttpResponse ExtractErrorResponse(Exception ex)
128-
{
129-
var response = new HttpResponse {ErrorMessage = ex.Message};
122+
static HttpResponse ExtractErrorResponse(Exception ex) {
123+
var response = new HttpResponse { ErrorMessage = ex.Message };
130124

131-
if (ex is WebException webException && webException.Status == WebExceptionStatus.Timeout)
132-
{
125+
if (ex is WebException webException && webException.Status == WebExceptionStatus.Timeout) {
133126
response.ResponseStatus = ResponseStatus.TimedOut;
134127
response.ErrorException = webException;
135128
}
136-
else
137-
{
129+
else {
138130
response.ErrorException = ex;
139131
response.ResponseStatus = ResponseStatus.Error;
140132
}
141133

142134
return response;
143135
}
144136

145-
static HttpWebResponse GetRawResponse(WebRequest request)
146-
{
147-
try
148-
{
149-
return (HttpWebResponse) request.GetResponse();
137+
static HttpWebResponse GetRawResponse(WebRequest request) {
138+
try {
139+
return (HttpWebResponse)request.GetResponse();
150140
}
151-
catch (WebException ex)
152-
{
141+
catch (WebException ex) {
153142
// Check to see if this is an HTTP error or a transport error.
154143
// In cases where an HTTP error occurs ( status code >= 400 )
155144
// return the underlying HTTP response, otherwise assume a
@@ -164,8 +153,7 @@ static HttpWebResponse GetRawResponse(WebRequest request)
164153
}
165154
}
166155

167-
void WriteRequestBody(WebRequest webRequest)
168-
{
156+
void WriteRequestBody(WebRequest webRequest) {
169157
if (HasBody || HasFiles || AlwaysMultipartFormData)
170158
webRequest.ContentLength = CalculateContentLength();
171159

@@ -180,8 +168,7 @@ void WriteRequestBody(WebRequest webRequest)
180168
}
181169

182170
[Obsolete("Use the WebRequestConfigurator delegate instead of overriding this method")]
183-
protected virtual HttpWebRequest ConfigureWebRequest(string method, Uri url)
184-
{
171+
protected virtual HttpWebRequest ConfigureWebRequest(string method, Uri url) {
185172
var webRequest = CreateWebRequest(url) ?? CreateRequest(url);
186173

187174
webRequest.UseDefaultCredentials = UseDefaultCredentials;
@@ -192,12 +179,10 @@ protected virtual HttpWebRequest ConfigureWebRequest(string method, Uri url)
192179
#if NETSTANDARD2_0
193180
webRequest.Proxy = null;
194181
#endif
195-
try
196-
{
182+
try {
197183
webRequest.ServicePoint.Expect100Continue = false;
198184
}
199-
catch (PlatformNotSupportedException)
200-
{
185+
catch (PlatformNotSupportedException) {
201186
// Avoid to crash in UWP apps
202187
}
203188

@@ -253,25 +238,20 @@ protected virtual HttpWebRequest ConfigureWebRequest(string method, Uri url)
253238

254239
// handle restricted headers the .NET way - thanks @dimebrain!
255240
// http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx
256-
void AppendHeaders()
257-
{
258-
foreach (var header in Headers)
259-
{
241+
void AppendHeaders() {
242+
foreach (var header in Headers) {
260243
if (_restrictedHeaderActions.TryGetValue(header.Name, out var restrictedHeaderAction))
261244
restrictedHeaderAction.Invoke(webRequest, header.Value);
262245
else
263246
webRequest.Headers.Add(header.Name, header.Value);
264247
}
265248
}
266249

267-
void AppendCookies()
268-
{
250+
void AppendCookies() {
269251
webRequest.CookieContainer = CookieContainer ?? new CookieContainer();
270252

271-
foreach (var httpCookie in Cookies)
272-
{
273-
var cookie = new Cookie
274-
{
253+
foreach (var httpCookie in Cookies) {
254+
var cookie = new Cookie {
275255
Name = httpCookie.Name,
276256
Value = httpCookie.Value,
277257
Domain = webRequest.RequestUri.Host

src/RestSharp/Http.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static void AddRange(HttpWebRequest r, string range) {
182182
public IList<HttpCookie> Cookies { get; internal set; } = null!;
183183

184184
/// <inheritdoc />
185-
public string RequestBody { get; set; } = null!;
185+
public string? RequestBody { get; set; }
186186

187187
/// <inheritdoc />
188188
public string RequestContentType { get; set; } = null!;

src/RestSharp/IHttp.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public interface IHttp
126126
/// <summary>
127127
/// Request body to be sent with request
128128
/// </summary>
129-
string RequestBody { get; set; }
129+
string? RequestBody { get; set; }
130130

131131
/// <summary>
132132
/// Content type of the request body.

test/RestSharp.IntegrationTests/AsyncRequestBodyTests.cs

+13-42
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,18 @@ static void AssertHasRequestBody(string contentType, string bodyData) {
2424
Assert.Equal(bodyData, RequestBodyCapturer.CapturedEntityBody);
2525
}
2626

27-
class RequestBodyCapturer {
28-
public const string RESOURCE = "Capture";
29-
30-
public static string CapturedContentType { get; set; }
31-
32-
public static bool CapturedHasEntityBody { get; set; }
33-
34-
public static string CapturedEntityBody { get; set; }
35-
36-
public static void Capture(HttpListenerContext context) {
37-
var request = context.Request;
38-
39-
CapturedContentType = request.ContentType;
40-
CapturedHasEntityBody = request.HasEntityBody;
41-
CapturedEntityBody = StreamToString(request.InputStream);
42-
}
43-
44-
static string StreamToString(Stream stream) {
45-
var streamReader = new StreamReader(stream);
46-
return streamReader.ReadToEnd();
47-
}
48-
}
49-
5027
[Fact]
51-
public void Can_Be_Added_To_COPY_Request() {
28+
public async Task Can_Be_Added_To_COPY_Request() {
5229
const Method httpMethod = Method.COPY;
5330

54-
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
31+
var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
5532

5633
const string contentType = "text/plain";
5734
const string bodyData = "abc123 foo bar baz BING!";
5835

5936
request.AddParameter(contentType, bodyData, ParameterType.RequestBody);
6037

61-
var resetEvent = new ManualResetEvent(false);
62-
63-
_client.ExecuteAsync(request, response => resetEvent.Set());
64-
resetEvent.WaitOne();
38+
await _client.ExecuteAsync(request);
6539

6640
AssertHasRequestBody(contentType, bodyData);
6741
}
@@ -70,7 +44,7 @@ public void Can_Be_Added_To_COPY_Request() {
7044
public void Can_Be_Added_To_DELETE_Request() {
7145
const Method httpMethod = Method.DELETE;
7246

73-
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
47+
var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
7448

7549
const string contentType = "text/plain";
7650
const string bodyData = "abc123 foo bar baz BING!";
@@ -89,7 +63,7 @@ public void Can_Be_Added_To_DELETE_Request() {
8963
public void Can_Be_Added_To_OPTIONS_Request() {
9064
const Method httpMethod = Method.OPTIONS;
9165

92-
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
66+
var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
9367

9468
const string contentType = "text/plain";
9569
const string bodyData = "abc123 foo bar baz BING!";
@@ -108,7 +82,7 @@ public void Can_Be_Added_To_OPTIONS_Request() {
10882
public void Can_Be_Added_To_PATCH_Request() {
10983
const Method httpMethod = Method.PATCH;
11084

111-
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
85+
var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
11286

11387
const string contentType = "text/plain";
11488
const string bodyData = "abc123 foo bar baz BING!";
@@ -127,7 +101,7 @@ public void Can_Be_Added_To_PATCH_Request() {
127101
public void Can_Be_Added_To_POST_Request() {
128102
const Method httpMethod = Method.POST;
129103

130-
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
104+
var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
131105

132106
const string contentType = "text/plain";
133107
const string bodyData = "abc123 foo bar baz BING!";
@@ -146,7 +120,7 @@ public void Can_Be_Added_To_POST_Request() {
146120
public void Can_Be_Added_To_PUT_Request() {
147121
const Method httpMethod = Method.PUT;
148122

149-
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
123+
var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
150124

151125
const string contentType = "text/plain";
152126
const string bodyData = "abc123 foo bar baz BING!";
@@ -165,7 +139,7 @@ public void Can_Be_Added_To_PUT_Request() {
165139
public void Can_Have_No_Body_Added_To_POST_Request() {
166140
const Method httpMethod = Method.POST;
167141

168-
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
142+
var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
169143
var resetEvent = new ManualResetEvent(false);
170144

171145
_client.ExecuteAsync(request, response => resetEvent.Set());
@@ -175,20 +149,17 @@ public void Can_Have_No_Body_Added_To_POST_Request() {
175149
}
176150

177151
[Fact]
178-
public void Can_Not_Be_Added_To_GET_Request() {
152+
public async Task Can_Be_Added_To_GET_Request() {
179153
const Method httpMethod = Method.GET;
180154

181-
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
155+
var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
182156

183157
const string contentType = "text/plain";
184158
const string bodyData = "abc123 foo bar baz BING!";
185159

186160
request.AddParameter(contentType, bodyData, ParameterType.RequestBody);
187161

188-
var resetEvent = new ManualResetEvent(false);
189-
190-
_client.ExecuteAsync(request, response => resetEvent.Set());
191-
resetEvent.WaitOne();
162+
await _client.ExecuteAsync(request);
192163

193164
AssertHasNoRequestBody();
194165
}
@@ -197,7 +168,7 @@ public void Can_Not_Be_Added_To_GET_Request() {
197168
public void Can_Not_Be_Added_To_HEAD_Request() {
198169
const Method httpMethod = Method.HEAD;
199170

200-
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
171+
var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
201172

202173
const string contentType = "text/plain";
203174
const string bodyData = "abc123 foo bar baz BING!";

test/RestSharp.IntegrationTests/RequestBodyTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,14 @@ public void Query_Parameters_With_Json_Body() {
216216
}
217217

218218
static void AssertHasNoRequestBody() {
219-
Assert.Null(RequestBodyCapturer.CapturedContentType);
220-
Assert.False(RequestBodyCapturer.CapturedHasEntityBody);
221-
Assert.Equal(string.Empty, RequestBodyCapturer.CapturedEntityBody);
219+
RequestBodyCapturer.CapturedContentType.Should().BeNull();
220+
RequestBodyCapturer.CapturedHasEntityBody.Should().BeFalse();
221+
RequestBodyCapturer.CapturedEntityBody.Should().BeNullOrEmpty();
222222
}
223223

224224
static void AssertHasRequestBody(string contentType, string bodyData) {
225-
Assert.Equal(contentType, RequestBodyCapturer.CapturedContentType);
226-
Assert.True(RequestBodyCapturer.CapturedHasEntityBody);
227-
Assert.Equal(bodyData, RequestBodyCapturer.CapturedEntityBody);
225+
RequestBodyCapturer.CapturedContentType.Should().Be(contentType);
226+
RequestBodyCapturer.CapturedHasEntityBody.Should().BeTrue();
227+
RequestBodyCapturer.CapturedEntityBody.Should().Be(bodyData);
228228
}
229229
}

test/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net452;net5</TargetFrameworks>
3+
<TargetFrameworks>net452;net6.0</TargetFrameworks>
44
</PropertyGroup>
55
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net452|AnyCPU'">
66
<WarningLevel>0</WarningLevel>
77
</PropertyGroup>
8-
<PropertyGroup Condition="'$(TargetFramework)'=='net5'">
8+
<PropertyGroup Condition="'$(TargetFramework)'=='net6.0'">
99
<DefineConstants>NETCORE</DefineConstants>
1010
</PropertyGroup>
1111
<ItemGroup>

test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
4-
<TargetFramework>net5</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66
<ItemGroup>
77
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj"/>

test/RestSharp.Serializers.Tests/RestSharp.Serializers.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net461;net5</TargetFrameworks>
3+
<TargetFrameworks>net461;net6.0</TargetFrameworks>
44
</PropertyGroup>
55
<ItemGroup>
66
<ProjectReference Include="..\..\src\RestSharp.Serializers.NewtonsoftJson\RestSharp.Serializers.NewtonsoftJson.csproj"/>

test/RestSharp.Tests.Shared/RestSharp.Tests.Shared.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net452;net5</TargetFrameworks>
3+
<TargetFrameworks>net452;net6.0</TargetFrameworks>
44
<IsTestProject>false</IsTestProject>
55
</PropertyGroup>
66
<ItemGroup Condition="'$(TargetFramework)' == 'net452'">

test/RestSharp.Tests/RestSharp.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net452;net5</TargetFrameworks>
3+
<TargetFrameworks>net452;net6.0</TargetFrameworks>
44
</PropertyGroup>
55
<ItemGroup>
66
<PackageReference Include="Moq" Version="4.16.1" />

0 commit comments

Comments
 (0)