diff --git a/src/RestSharp/Http.Sync.cs b/src/RestSharp/Http.Sync.cs
index 18f217834..75be33873 100644
--- a/src/RestSharp/Http.Sync.cs
+++ b/src/RestSharp/Http.Sync.cs
@@ -18,13 +18,11 @@
using RestSharp.Authenticators.OAuth.Extensions;
using RestSharp.Extensions;
-namespace RestSharp
-{
+namespace RestSharp {
///
/// HttpWebRequest wrapper (sync methods)
///
- public partial class Http
- {
+ public partial class Http {
///
/// Execute a POST request
///
@@ -81,60 +79,54 @@ public partial class Http
HttpResponse GetStyleMethodInternal(string method)
=> ExecuteRequest(
- method, r =>
- {
+ method,
+ r => {
if (!HasBody) return;
-
+
if (!CanGetWithBody())
- throw new NotSupportedException($"Http verb {method} does not support body");
+ throw new NotSupportedException($"HTTP verb {method} does not support body");
r.ContentType = RequestContentType;
WriteRequestBody(r);
- bool CanGetWithBody() => method == "DELETE" || method == "OPTIONS";
+ bool CanGetWithBody() => method is "DELETE" or "OPTIONS";
}
);
HttpResponse PostPutInternal(string method)
=> ExecuteRequest(
- method, r =>
- {
+ method,
+ r => {
PreparePostBody(r);
WriteRequestBody(r);
}
);
- HttpResponse ExecuteRequest(string httpMethod, Action prepareRequest)
- {
+ HttpResponse ExecuteRequest(string httpMethod, Action prepareRequest) {
var webRequest = ConfigureWebRequest(httpMethod, Url);
prepareRequest(webRequest);
- try
- {
+ try {
using var webResponse = GetRawResponse(webRequest);
return ExtractResponseData(webResponse);
}
- catch (Exception ex)
- {
+ catch (Exception ex) {
if (ThrowOnAnyError)
throw;
return ExtractErrorResponse(ex);
}
- static HttpResponse ExtractErrorResponse(Exception ex)
- {
- var response = new HttpResponse {ErrorMessage = ex.Message};
+ static HttpResponse ExtractErrorResponse(Exception ex) {
+ var response = new HttpResponse { ErrorMessage = ex.Message };
- if (ex is WebException webException && webException.Status == WebExceptionStatus.Timeout)
- {
+ if (ex is WebException webException && webException.Status == WebExceptionStatus.Timeout) {
response.ResponseStatus = ResponseStatus.TimedOut;
response.ErrorException = webException;
}
- else
- {
+ else {
response.ErrorException = ex;
response.ResponseStatus = ResponseStatus.Error;
}
@@ -142,14 +134,11 @@ static HttpResponse ExtractErrorResponse(Exception ex)
return response;
}
- static HttpWebResponse GetRawResponse(WebRequest request)
- {
- try
- {
- return (HttpWebResponse) request.GetResponse();
+ static HttpWebResponse GetRawResponse(WebRequest request) {
+ try {
+ return (HttpWebResponse)request.GetResponse();
}
- catch (WebException ex)
- {
+ catch (WebException ex) {
// Check to see if this is an HTTP error or a transport error.
// In cases where an HTTP error occurs ( status code >= 400 )
// return the underlying HTTP response, otherwise assume a
@@ -164,8 +153,7 @@ static HttpWebResponse GetRawResponse(WebRequest request)
}
}
- void WriteRequestBody(WebRequest webRequest)
- {
+ void WriteRequestBody(WebRequest webRequest) {
if (HasBody || HasFiles || AlwaysMultipartFormData)
webRequest.ContentLength = CalculateContentLength();
@@ -180,8 +168,7 @@ void WriteRequestBody(WebRequest webRequest)
}
[Obsolete("Use the WebRequestConfigurator delegate instead of overriding this method")]
- protected virtual HttpWebRequest ConfigureWebRequest(string method, Uri url)
- {
+ protected virtual HttpWebRequest ConfigureWebRequest(string method, Uri url) {
var webRequest = CreateWebRequest(url) ?? CreateRequest(url);
webRequest.UseDefaultCredentials = UseDefaultCredentials;
@@ -192,12 +179,10 @@ protected virtual HttpWebRequest ConfigureWebRequest(string method, Uri url)
#if NETSTANDARD2_0
webRequest.Proxy = null;
#endif
- try
- {
+ try {
webRequest.ServicePoint.Expect100Continue = false;
}
- catch (PlatformNotSupportedException)
- {
+ catch (PlatformNotSupportedException) {
// Avoid to crash in UWP apps
}
@@ -253,10 +238,8 @@ protected virtual HttpWebRequest ConfigureWebRequest(string method, Uri url)
// handle restricted headers the .NET way - thanks @dimebrain!
// http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx
- void AppendHeaders()
- {
- foreach (var header in Headers)
- {
+ void AppendHeaders() {
+ foreach (var header in Headers) {
if (_restrictedHeaderActions.TryGetValue(header.Name, out var restrictedHeaderAction))
restrictedHeaderAction.Invoke(webRequest, header.Value);
else
@@ -264,14 +247,11 @@ void AppendHeaders()
}
}
- void AppendCookies()
- {
+ void AppendCookies() {
webRequest.CookieContainer = CookieContainer ?? new CookieContainer();
- foreach (var httpCookie in Cookies)
- {
- var cookie = new Cookie
- {
+ foreach (var httpCookie in Cookies) {
+ var cookie = new Cookie {
Name = httpCookie.Name,
Value = httpCookie.Value,
Domain = webRequest.RequestUri.Host
diff --git a/src/RestSharp/Http.cs b/src/RestSharp/Http.cs
index 8c41616b8..7d4e690fe 100644
--- a/src/RestSharp/Http.cs
+++ b/src/RestSharp/Http.cs
@@ -182,7 +182,7 @@ static void AddRange(HttpWebRequest r, string range) {
public IList Cookies { get; internal set; } = null!;
///
- public string RequestBody { get; set; } = null!;
+ public string? RequestBody { get; set; }
///
public string RequestContentType { get; set; } = null!;
diff --git a/src/RestSharp/IHttp.cs b/src/RestSharp/IHttp.cs
index b964c85a1..d4d6cd9af 100644
--- a/src/RestSharp/IHttp.cs
+++ b/src/RestSharp/IHttp.cs
@@ -126,7 +126,7 @@ public interface IHttp
///
/// Request body to be sent with request
///
- string RequestBody { get; set; }
+ string? RequestBody { get; set; }
///
/// Content type of the request body.
diff --git a/test/RestSharp.IntegrationTests/AsyncRequestBodyTests.cs b/test/RestSharp.IntegrationTests/AsyncRequestBodyTests.cs
index ffd21bb98..2027b1057 100644
--- a/test/RestSharp.IntegrationTests/AsyncRequestBodyTests.cs
+++ b/test/RestSharp.IntegrationTests/AsyncRequestBodyTests.cs
@@ -24,44 +24,18 @@ static void AssertHasRequestBody(string contentType, string bodyData) {
Assert.Equal(bodyData, RequestBodyCapturer.CapturedEntityBody);
}
- 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);
- }
-
- static string StreamToString(Stream stream) {
- var streamReader = new StreamReader(stream);
- return streamReader.ReadToEnd();
- }
- }
-
[Fact]
- public void Can_Be_Added_To_COPY_Request() {
+ public async Task Can_Be_Added_To_COPY_Request() {
const Method httpMethod = Method.COPY;
- var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
+ 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();
+ await _client.ExecuteAsync(request);
AssertHasRequestBody(contentType, bodyData);
}
@@ -70,7 +44,7 @@ public void Can_Be_Added_To_COPY_Request() {
public void Can_Be_Added_To_DELETE_Request() {
const Method httpMethod = Method.DELETE;
- var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
+ var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
const string contentType = "text/plain";
const string bodyData = "abc123 foo bar baz BING!";
@@ -89,7 +63,7 @@ public void Can_Be_Added_To_DELETE_Request() {
public void Can_Be_Added_To_OPTIONS_Request() {
const Method httpMethod = Method.OPTIONS;
- var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
+ var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
const string contentType = "text/plain";
const string bodyData = "abc123 foo bar baz BING!";
@@ -108,7 +82,7 @@ public void Can_Be_Added_To_OPTIONS_Request() {
public void Can_Be_Added_To_PATCH_Request() {
const Method httpMethod = Method.PATCH;
- var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
+ var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
const string contentType = "text/plain";
const string bodyData = "abc123 foo bar baz BING!";
@@ -127,7 +101,7 @@ public void Can_Be_Added_To_PATCH_Request() {
public void Can_Be_Added_To_POST_Request() {
const Method httpMethod = Method.POST;
- var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
+ var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
const string contentType = "text/plain";
const string bodyData = "abc123 foo bar baz BING!";
@@ -146,7 +120,7 @@ public void Can_Be_Added_To_POST_Request() {
public void Can_Be_Added_To_PUT_Request() {
const Method httpMethod = Method.PUT;
- var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
+ var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
const string contentType = "text/plain";
const string bodyData = "abc123 foo bar baz BING!";
@@ -165,7 +139,7 @@ public void Can_Be_Added_To_PUT_Request() {
public void Can_Have_No_Body_Added_To_POST_Request() {
const Method httpMethod = Method.POST;
- var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
+ var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
var resetEvent = new ManualResetEvent(false);
_client.ExecuteAsync(request, response => resetEvent.Set());
@@ -175,20 +149,17 @@ public void Can_Have_No_Body_Added_To_POST_Request() {
}
[Fact]
- public void Can_Not_Be_Added_To_GET_Request() {
+ public async Task Can_Be_Added_To_GET_Request() {
const Method httpMethod = Method.GET;
- var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
+ 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();
+ await _client.ExecuteAsync(request);
AssertHasNoRequestBody();
}
@@ -197,7 +168,7 @@ public void Can_Not_Be_Added_To_GET_Request() {
public void Can_Not_Be_Added_To_HEAD_Request() {
const Method httpMethod = Method.HEAD;
- var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
+ var request = new RestRequest(RequestBodyCapturer.Resource, httpMethod);
const string contentType = "text/plain";
const string bodyData = "abc123 foo bar baz BING!";
diff --git a/test/RestSharp.IntegrationTests/RequestBodyTests.cs b/test/RestSharp.IntegrationTests/RequestBodyTests.cs
index f6bf8c0b7..d1727ed64 100644
--- a/test/RestSharp.IntegrationTests/RequestBodyTests.cs
+++ b/test/RestSharp.IntegrationTests/RequestBodyTests.cs
@@ -216,14 +216,14 @@ public void Query_Parameters_With_Json_Body() {
}
static void AssertHasNoRequestBody() {
- Assert.Null(RequestBodyCapturer.CapturedContentType);
- Assert.False(RequestBodyCapturer.CapturedHasEntityBody);
- Assert.Equal(string.Empty, RequestBodyCapturer.CapturedEntityBody);
+ RequestBodyCapturer.CapturedContentType.Should().BeNull();
+ RequestBodyCapturer.CapturedHasEntityBody.Should().BeFalse();
+ RequestBodyCapturer.CapturedEntityBody.Should().BeNullOrEmpty();
}
static void AssertHasRequestBody(string contentType, string bodyData) {
- Assert.Equal(contentType, RequestBodyCapturer.CapturedContentType);
- Assert.True(RequestBodyCapturer.CapturedHasEntityBody);
- Assert.Equal(bodyData, RequestBodyCapturer.CapturedEntityBody);
+ RequestBodyCapturer.CapturedContentType.Should().Be(contentType);
+ RequestBodyCapturer.CapturedHasEntityBody.Should().BeTrue();
+ RequestBodyCapturer.CapturedEntityBody.Should().Be(bodyData);
}
}
\ No newline at end of file
diff --git a/test/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj b/test/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj
index 31859eb68..ba8d58168 100644
--- a/test/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj
+++ b/test/RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj
@@ -1,11 +1,11 @@
- net452;net5
+ net452;net6.0
0
-
+
NETCORE
diff --git a/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj b/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj
index b86c3fade..0055c78bc 100644
--- a/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj
+++ b/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj
@@ -1,7 +1,7 @@
Exe
- net5
+ net6.0
diff --git a/test/RestSharp.Serializers.Tests/RestSharp.Serializers.Tests.csproj b/test/RestSharp.Serializers.Tests/RestSharp.Serializers.Tests.csproj
index e90e8368c..54386be80 100644
--- a/test/RestSharp.Serializers.Tests/RestSharp.Serializers.Tests.csproj
+++ b/test/RestSharp.Serializers.Tests/RestSharp.Serializers.Tests.csproj
@@ -1,6 +1,6 @@
- net461;net5
+ net461;net6.0
diff --git a/test/RestSharp.Tests.Shared/RestSharp.Tests.Shared.csproj b/test/RestSharp.Tests.Shared/RestSharp.Tests.Shared.csproj
index b31bda8c2..761d5215b 100644
--- a/test/RestSharp.Tests.Shared/RestSharp.Tests.Shared.csproj
+++ b/test/RestSharp.Tests.Shared/RestSharp.Tests.Shared.csproj
@@ -1,6 +1,6 @@
- net452;net5
+ net452;net6.0
false
diff --git a/test/RestSharp.Tests/RestSharp.Tests.csproj b/test/RestSharp.Tests/RestSharp.Tests.csproj
index db2630ff3..ad1d96e6b 100644
--- a/test/RestSharp.Tests/RestSharp.Tests.csproj
+++ b/test/RestSharp.Tests/RestSharp.Tests.csproj
@@ -1,6 +1,6 @@
- net452;net5
+ net452;net6.0