Skip to content

Commit 5339609

Browse files
committed
Make the fix conditional
1 parent 221c467 commit 5339609

File tree

9 files changed

+60
-33
lines changed

9 files changed

+60
-33
lines changed

src/RestSharp/Request/RequestContent.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Runtime.Serialization;
1818
using RestSharp.Extensions;
1919
using static RestSharp.KnownHeaders;
20+
2021
// ReSharper disable InvertIf
2122
// ReSharper disable SuggestBaseTypeForParameter
2223

@@ -27,8 +28,6 @@ class RequestContent : IDisposable {
2728
readonly RestRequest _request;
2829
readonly List<Stream> _streams = new();
2930

30-
31-
3231
HttpContent? Content { get; set; }
3332

3433
public RequestContent(RestClient client, RestRequest request) {
@@ -56,8 +55,7 @@ void AddFiles() {
5655
_streams.Add(stream);
5756
var fileContent = new StreamContent(stream);
5857

59-
if (file.ContentType != null)
60-
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(file.ContentType);
58+
if (file.ContentType != null) fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(file.ContentType);
6159

6260
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
6361
Name = $"\"{file.Name}\"",
@@ -98,8 +96,7 @@ HttpContent GetSerialized() {
9896

9997
var content = serializer.Serialize(body);
10098

101-
if (content == null)
102-
throw new SerializationException("Request body serialized to null");
99+
if (content == null) throw new SerializationException("Request body serialized to null");
103100

104101
return new StringContent(
105102
content,
@@ -151,23 +148,33 @@ void AddPostParameters(ParametersCollection? postParameters) {
151148
// we got the multipart form already instantiated, just add parameters to it
152149
foreach (var postParameter in postParameters!) {
153150
var parameterName = postParameter.Name!;
151+
154152
mpContent.Add(
155153
new StringContent(postParameter.Value!.ToString()!, _client.Options.Encoding, postParameter.ContentType),
156154
_request.MultipartFormQuoteParameters ? $"\"{parameterName}\"" : parameterName
157155
);
158156
}
159157
}
160158
else {
161-
// we should not have anything else except the parameters, so we send them as form URL encoded. However due
162-
// to bugs in HttpClient FormUrlEncodedContent (see https://github.com/restsharp/RestSharp/issues/1814) we
159+
#if NETCORE
160+
// We should not have anything else except the parameters, so we send them as form URL encoded.
161+
var formContent = new FormUrlEncodedContent(
162+
_request.Parameters
163+
.Where(x => x.Type == ParameterType.GetOrPost)
164+
.Select(x => new KeyValuePair<string, string>(x.Name!, x.Value!.ToString()!))!
165+
);
166+
Content = formContent;
167+
#else
168+
// However due to bugs in HttpClient FormUrlEncodedContent (see https://github.com/restsharp/RestSharp/issues/1814) we
163169
// do the encoding ourselves using WebUtility.UrlEncode instead.
164170
var formData = _request.Parameters
165171
.Where(x => x.Type == ParameterType.GetOrPost)
166172
.Select(x => new KeyValuePair<string, string>(x.Name!, x.Value!.ToString()!))!;
167-
var encodedItems = formData.Select(i => $"{WebUtility.UrlEncode(i.Key)}={WebUtility.UrlEncode(i.Value)}"/*.Replace("%20", "+")*/);
173+
var encodedItems = formData.Select(i => $"{WebUtility.UrlEncode(i.Key)}={WebUtility.UrlEncode(i.Value)}" /*.Replace("%20", "+")*/);
168174
var encodedContent = new StringContent(string.Join("&", encodedItems), null, "application/x-www-form-urlencoded");
169-
175+
170176
Content = encodedContent;
177+
#endif
171178
}
172179
}
173180

test/Directory.Build.props

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<IsTestProject>true</IsTestProject>
55
<IsPackable>false</IsPackable>
6-
<TargetFramework>net6.0</TargetFramework>
6+
<TargetFrameworks>net472;net6.0</TargetFrameworks>
77
<Nullable>disable</Nullable>
88
</PropertyGroup>
99

@@ -17,6 +17,9 @@
1717
<PackageReference Include="AutoFixture" Version="4.17.0"/>
1818
<PackageReference Include="FluentAssertions" Version="6.7.0"/>
1919
</ItemGroup>
20+
<ItemGroup Condition="$(TargetFramework) == 'net472'">
21+
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net472" Version="1.0.2" PrivateAssets="All"/>
22+
</ItemGroup>
2023

2124
<ItemGroup>
2225
<Using Include="Xunit"/>

test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
44
<IsTestProject>false</IsTestProject>
5+
<TargetFrameworks>net6</TargetFrameworks>
56
</PropertyGroup>
67
<ItemGroup>
78
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj" />

test/RestSharp.Tests.Integrated/PostTests.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,39 @@ public async Task Should_post_json() {
1515
var request = new RestRequest("post/json").AddJsonBody(body);
1616
var response = await _client.ExecutePostAsync<TestResponse>(request);
1717

18-
response.Data.Message.Should().Be(body.Data);
18+
response!.Data!.Message.Should().Be(body.Data);
1919
}
20-
20+
2121
[Fact]
2222
public async Task Should_post_json_with_PostAsync() {
2323
var body = new TestRequest("foo", 100);
2424
var request = new RestRequest("post/json").AddJsonBody(body);
2525
var response = await _client.PostAsync<TestResponse>(request);
2626

27-
response.Message.Should().Be(body.Data);
27+
response!.Message.Should().Be(body.Data);
2828
}
29-
29+
3030
[Fact]
3131
public async Task Should_post_json_with_PostJsonAsync() {
3232
var body = new TestRequest("foo", 100);
3333
var response = await _client.PostJsonAsync<TestRequest, TestResponse>("post/json", body);
3434

35-
response.Message.Should().Be(body.Data);
36-
}
37-
38-
class Response {
39-
public string Message { get; set; }
35+
response!.Message.Should().Be(body.Data);
4036
}
4137

4238
[Fact]
4339
public async Task Should_post_large_form_data() {
44-
const int length = 1024 * 1024;
45-
var superLongString = new string('?', length);
46-
var request = new RestRequest("post/form", Method.Post).AddParameter("big_string", superLongString);
47-
var response = await _client.ExecuteAsync<Response>(request);
40+
const int length = 1024 * 1024;
41+
42+
var superLongString = new string('?', length);
43+
var request = new RestRequest("post/form", Method.Post).AddParameter("big_string", superLongString);
44+
var response = await _client.ExecuteAsync<Response>(request);
4845

4946
response.StatusCode.Should().Be(HttpStatusCode.OK);
5047
response.Data!.Message.Should().Be($"Works! Length: {length}");
5148
}
49+
50+
class Response {
51+
public string Message { get; set; }
52+
}
5253
}

test/RestSharp.Tests.Integrated/RestSharp.Tests.Integrated.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Nullable>disable</Nullable>
4+
<TargetFrameworks>net6</TargetFrameworks>
45
</PropertyGroup>
56
<ItemGroup>
67
<ProjectReference Include="..\..\src\RestSharp.Serializers.Xml\RestSharp.Serializers.Xml.csproj" />

test/RestSharp.Tests.Legacy/RestSharp.Tests.Legacy.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net48</TargetFramework>
3+
<TargetFramework>net472</TargetFramework>
44
<Nullable>disable</Nullable>
55
</PropertyGroup>
66
<ItemGroup>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<None Update="SampleData\boolean_from_string.xml" CopyToOutputDirectory="PreserveNewest" />
1212
<None Update="SampleData\deserialize_as_list.xml" CopyToOutputDirectory="PreserveNewest" />
1313
<None Update="SampleData\directlists.xml" CopyToOutputDirectory="PreserveNewest" />
14-
<None Update="SampleData\eventful.xml" CopyToOutputDirectory="PreserveNewest" />
14+
<None Update="SampleData\eventful.xml" CopyToOutputDirectory="Always" />
1515
<None Update="SampleData\Goodreads.xml" CopyToOutputDirectory="PreserveNewest" />
1616
<None Update="SampleData\GoodreadsFormatError.xml" CopyToOutputDirectory="PreserveNewest" />
1717
<None Update="SampleData\GoogleWeather.xml" CopyToOutputDirectory="PreserveNewest" />

test/RestSharp.Tests.Serializers.Xml/XmlAttributeDeserializerTests.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@
33
using RestSharp.Serializers.Xml;
44
using RestSharp.Tests.Serializers.Xml.SampleClasses;
55

6-
namespace RestSharp.Tests.Serializers.Xml;
6+
namespace RestSharp.Tests.Serializers.Xml;
77

88
public class XmlAttributeDeserializerTests {
9+
readonly ITestOutputHelper _output;
10+
911
const string GuidString = "AC1FC4BC-087A-4242-B8EE-C53EBE9887A5";
1012

13+
#if NETCORE
1114
readonly string _sampleDataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SampleData");
15+
#else
16+
readonly string _sampleDataPath = Path.Combine(Directory.GetCurrentDirectory(), "SampleData");
17+
#endif
1218

1319
string PathFor(string sampleFile) => Path.Combine(_sampleDataPath, sampleFile);
1420

21+
public XmlAttributeDeserializerTests(ITestOutputHelper output) {
22+
_output = output;
23+
}
24+
1525
[Fact]
1626
public void Can_Deserialize_Lists_of_Simple_Types() {
1727
var xmlPath = PathFor("xmllists.xml");
@@ -24,7 +34,7 @@ public void Can_Deserialize_Lists_of_Simple_Types() {
2434

2535
Assert.NotEmpty(output.Numbers);
2636
Assert.False(output.Names[0].Length == 0);
27-
Assert.False(output.Numbers.Sum() == 0);
37+
Assert.False(output.Numbers.Sum() == 0);
2838
}
2939

3040
[Fact]
@@ -572,7 +582,7 @@ static string CreateUnderscoresXml() {
572582
friends.Add(
573583
new XElement(
574584
"Friend",
575-
new XElement("Name", "Friend" + i),
585+
new XElement("Name", "Friend" + i),
576586
new XAttribute("Since", DateTime.Now.Year - i)
577587
)
578588
);
@@ -621,7 +631,7 @@ static string CreateLowercaseUnderscoresXml() {
621631
friends.Add(
622632
new XElement(
623633
"Friend",
624-
new XElement("Name", "Friend" + i),
634+
new XElement("Name", "Friend" + i),
625635
new XAttribute("Since", DateTime.Now.Year - i)
626636
)
627637
);
@@ -670,7 +680,7 @@ static string CreateDashesXml() {
670680
friends.Add(
671681
new XElement(
672682
"Friend",
673-
new XElement("Name", "Friend" + i),
683+
new XElement("Name", "Friend" + i),
674684
new XAttribute("Since", DateTime.Now.Year - i)
675685
)
676686
);
@@ -738,7 +748,7 @@ static string CreateElementsXml() {
738748
friends.Add(
739749
new XElement(
740750
"Friend",
741-
new XElement("Name", "Friend" + i),
751+
new XElement("Name", "Friend" + i),
742752
new XElement("Since", DateTime.Now.Year - i)
743753
)
744754
);
@@ -870,4 +880,4 @@ static string CreateXmlWithAttributesAndNullValuesAndPopulatedValues() {
870880

871881
return doc.ToString();
872882
}
873-
}
883+
}

test/RestSharp.Tests.Serializers.Xml/XmlDeserializerTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ namespace RestSharp.Tests.Serializers.Xml;
99
public class XmlDeserializerTests {
1010
const string GuidString = "AC1FC4BC-087A-4242-B8EE-C53EBE9887A5";
1111

12+
#if NETCORE
1213
readonly string _sampleDataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SampleData");
14+
#else
15+
readonly string _sampleDataPath = Path.Combine(Directory.GetCurrentDirectory(), "SampleData");
16+
#endif
1317

1418
string PathFor(string sampleFile) => Path.Combine(_sampleDataPath, sampleFile);
1519

0 commit comments

Comments
 (0)