diff --git a/src/RestSharp/Request/RequestContent.cs b/src/RestSharp/Request/RequestContent.cs index 7e7619ab9..87ec03f42 100644 --- a/src/RestSharp/Request/RequestContent.cs +++ b/src/RestSharp/Request/RequestContent.cs @@ -17,6 +17,7 @@ using System.Runtime.Serialization; using RestSharp.Extensions; using static RestSharp.KnownHeaders; + // ReSharper disable InvertIf // ReSharper disable SuggestBaseTypeForParameter @@ -27,8 +28,6 @@ class RequestContent : IDisposable { readonly RestRequest _request; readonly List _streams = new(); - - HttpContent? Content { get; set; } public RequestContent(RestClient client, RestRequest request) { @@ -56,8 +55,7 @@ void AddFiles() { _streams.Add(stream); var fileContent = new StreamContent(stream); - if (file.ContentType != null) - fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(file.ContentType); + if (file.ContentType != null) fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(file.ContentType); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = $"\"{file.Name}\"", @@ -98,8 +96,7 @@ HttpContent GetSerialized() { var content = serializer.Serialize(body); - if (content == null) - throw new SerializationException("Request body serialized to null"); + if (content == null) throw new SerializationException("Request body serialized to null"); return new StringContent( content, @@ -151,6 +148,7 @@ void AddPostParameters(ParametersCollection? postParameters) { // we got the multipart form already instantiated, just add parameters to it foreach (var postParameter in postParameters!) { var parameterName = postParameter.Name!; + mpContent.Add( new StringContent(postParameter.Value!.ToString()!, _client.Options.Encoding, postParameter.ContentType), _request.MultipartFormQuoteParameters ? $"\"{parameterName}\"" : parameterName @@ -158,16 +156,25 @@ void AddPostParameters(ParametersCollection? postParameters) { } } else { - // we should not have anything else except the parameters, so we send them as form URL encoded. However due - // to bugs in HttpClient FormUrlEncodedContent (see https://github.com/restsharp/RestSharp/issues/1814) we +#if NETCORE + // We should not have anything else except the parameters, so we send them as form URL encoded. + var formContent = new FormUrlEncodedContent( + _request.Parameters + .Where(x => x.Type == ParameterType.GetOrPost) + .Select(x => new KeyValuePair(x.Name!, x.Value!.ToString()!))! + ); + Content = formContent; +#else + // However due to bugs in HttpClient FormUrlEncodedContent (see https://github.com/restsharp/RestSharp/issues/1814) we // do the encoding ourselves using WebUtility.UrlEncode instead. var formData = _request.Parameters .Where(x => x.Type == ParameterType.GetOrPost) .Select(x => new KeyValuePair(x.Name!, x.Value!.ToString()!))!; - var encodedItems = formData.Select(i => $"{WebUtility.UrlEncode(i.Key)}={WebUtility.UrlEncode(i.Value)}"/*.Replace("%20", "+")*/); + var encodedItems = formData.Select(i => $"{WebUtility.UrlEncode(i.Key)}={WebUtility.UrlEncode(i.Value)}" /*.Replace("%20", "+")*/); var encodedContent = new StringContent(string.Join("&", encodedItems), null, "application/x-www-form-urlencoded"); - + Content = encodedContent; +#endif } } diff --git a/test/Directory.Build.props b/test/Directory.Build.props index 9812b1e01..ce557c25b 100644 --- a/test/Directory.Build.props +++ b/test/Directory.Build.props @@ -3,7 +3,7 @@ true false - net6.0 + net472;net6.0 disable @@ -17,6 +17,9 @@ + + + diff --git a/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj b/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj index bb6d160b5..af57941ed 100644 --- a/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj +++ b/test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj @@ -2,6 +2,7 @@ Exe false + net6 diff --git a/test/RestSharp.Tests.Integrated/PostTests.cs b/test/RestSharp.Tests.Integrated/PostTests.cs index 566b2981d..a37a454a6 100644 --- a/test/RestSharp.Tests.Integrated/PostTests.cs +++ b/test/RestSharp.Tests.Integrated/PostTests.cs @@ -15,38 +15,39 @@ public async Task Should_post_json() { var request = new RestRequest("post/json").AddJsonBody(body); var response = await _client.ExecutePostAsync(request); - response.Data.Message.Should().Be(body.Data); + response!.Data!.Message.Should().Be(body.Data); } - + [Fact] public async Task Should_post_json_with_PostAsync() { var body = new TestRequest("foo", 100); var request = new RestRequest("post/json").AddJsonBody(body); var response = await _client.PostAsync(request); - response.Message.Should().Be(body.Data); + response!.Message.Should().Be(body.Data); } - + [Fact] public async Task Should_post_json_with_PostJsonAsync() { var body = new TestRequest("foo", 100); var response = await _client.PostJsonAsync("post/json", body); - response.Message.Should().Be(body.Data); - } - - class Response { - public string Message { get; set; } + response!.Message.Should().Be(body.Data); } [Fact] public async Task Should_post_large_form_data() { - const int length = 1024 * 1024; - var superLongString = new string('?', length); - var request = new RestRequest("post/form", Method.Post).AddParameter("big_string", superLongString); - var response = await _client.ExecuteAsync(request); + const int length = 1024 * 1024; + + var superLongString = new string('?', length); + var request = new RestRequest("post/form", Method.Post).AddParameter("big_string", superLongString); + var response = await _client.ExecuteAsync(request); response.StatusCode.Should().Be(HttpStatusCode.OK); response.Data!.Message.Should().Be($"Works! Length: {length}"); } + + class Response { + public string Message { get; set; } + } } diff --git a/test/RestSharp.Tests.Integrated/RestSharp.Tests.Integrated.csproj b/test/RestSharp.Tests.Integrated/RestSharp.Tests.Integrated.csproj index 8e8d86cef..dd72dc5a2 100644 --- a/test/RestSharp.Tests.Integrated/RestSharp.Tests.Integrated.csproj +++ b/test/RestSharp.Tests.Integrated/RestSharp.Tests.Integrated.csproj @@ -1,6 +1,7 @@  disable + net6 diff --git a/test/RestSharp.Tests.Legacy/RestSharp.Tests.Legacy.csproj b/test/RestSharp.Tests.Legacy/RestSharp.Tests.Legacy.csproj index 5eb4f1271..442bc31df 100644 --- a/test/RestSharp.Tests.Legacy/RestSharp.Tests.Legacy.csproj +++ b/test/RestSharp.Tests.Legacy/RestSharp.Tests.Legacy.csproj @@ -1,6 +1,6 @@  - net48 + net472 disable diff --git a/test/RestSharp.Tests.Serializers.Xml/RestSharp.Tests.Serializers.Xml.csproj b/test/RestSharp.Tests.Serializers.Xml/RestSharp.Tests.Serializers.Xml.csproj index a121cfbc1..94e955b4a 100644 --- a/test/RestSharp.Tests.Serializers.Xml/RestSharp.Tests.Serializers.Xml.csproj +++ b/test/RestSharp.Tests.Serializers.Xml/RestSharp.Tests.Serializers.Xml.csproj @@ -11,7 +11,7 @@ - + diff --git a/test/RestSharp.Tests.Serializers.Xml/XmlAttributeDeserializerTests.cs b/test/RestSharp.Tests.Serializers.Xml/XmlAttributeDeserializerTests.cs index 54a34b9b4..08a5d3a53 100644 --- a/test/RestSharp.Tests.Serializers.Xml/XmlAttributeDeserializerTests.cs +++ b/test/RestSharp.Tests.Serializers.Xml/XmlAttributeDeserializerTests.cs @@ -3,15 +3,25 @@ using RestSharp.Serializers.Xml; using RestSharp.Tests.Serializers.Xml.SampleClasses; -namespace RestSharp.Tests.Serializers.Xml; +namespace RestSharp.Tests.Serializers.Xml; public class XmlAttributeDeserializerTests { + readonly ITestOutputHelper _output; + const string GuidString = "AC1FC4BC-087A-4242-B8EE-C53EBE9887A5"; + #if NETCORE readonly string _sampleDataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SampleData"); + #else + readonly string _sampleDataPath = Path.Combine(Directory.GetCurrentDirectory(), "SampleData"); + #endif string PathFor(string sampleFile) => Path.Combine(_sampleDataPath, sampleFile); + public XmlAttributeDeserializerTests(ITestOutputHelper output) { + _output = output; + } + [Fact] public void Can_Deserialize_Lists_of_Simple_Types() { var xmlPath = PathFor("xmllists.xml"); @@ -24,7 +34,7 @@ public void Can_Deserialize_Lists_of_Simple_Types() { Assert.NotEmpty(output.Numbers); Assert.False(output.Names[0].Length == 0); - Assert.False(output.Numbers.Sum() == 0); + Assert.False(output.Numbers.Sum() == 0); } [Fact] @@ -572,7 +582,7 @@ static string CreateUnderscoresXml() { friends.Add( new XElement( "Friend", - new XElement("Name", "Friend" + i), + new XElement("Name", "Friend" + i), new XAttribute("Since", DateTime.Now.Year - i) ) ); @@ -621,7 +631,7 @@ static string CreateLowercaseUnderscoresXml() { friends.Add( new XElement( "Friend", - new XElement("Name", "Friend" + i), + new XElement("Name", "Friend" + i), new XAttribute("Since", DateTime.Now.Year - i) ) ); @@ -670,7 +680,7 @@ static string CreateDashesXml() { friends.Add( new XElement( "Friend", - new XElement("Name", "Friend" + i), + new XElement("Name", "Friend" + i), new XAttribute("Since", DateTime.Now.Year - i) ) ); @@ -738,7 +748,7 @@ static string CreateElementsXml() { friends.Add( new XElement( "Friend", - new XElement("Name", "Friend" + i), + new XElement("Name", "Friend" + i), new XElement("Since", DateTime.Now.Year - i) ) ); @@ -870,4 +880,4 @@ static string CreateXmlWithAttributesAndNullValuesAndPopulatedValues() { return doc.ToString(); } -} \ No newline at end of file +} diff --git a/test/RestSharp.Tests.Serializers.Xml/XmlDeserializerTests.cs b/test/RestSharp.Tests.Serializers.Xml/XmlDeserializerTests.cs index f429085cf..b4efffa21 100644 --- a/test/RestSharp.Tests.Serializers.Xml/XmlDeserializerTests.cs +++ b/test/RestSharp.Tests.Serializers.Xml/XmlDeserializerTests.cs @@ -9,7 +9,11 @@ namespace RestSharp.Tests.Serializers.Xml; public class XmlDeserializerTests { const string GuidString = "AC1FC4BC-087A-4242-B8EE-C53EBE9887A5"; + #if NETCORE readonly string _sampleDataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SampleData"); + #else + readonly string _sampleDataPath = Path.Combine(Directory.GetCurrentDirectory(), "SampleData"); + #endif string PathFor(string sampleFile) => Path.Combine(_sampleDataPath, sampleFile);