Skip to content

Commit d3e1334

Browse files
committed
Merge branch 'kendallb-feature/http-client-issue-1814' into dev
2 parents f3af0ce + 5339609 commit d3e1334

File tree

10 files changed

+70
-22
lines changed

10 files changed

+70
-22
lines changed

Diff for: src/RestSharp/Request/RequestContent.cs

+18-7
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System.Net;
1516
using System.Net.Http.Headers;
1617
using System.Runtime.Serialization;
1718
using RestSharp.Extensions;
1819
using static RestSharp.KnownHeaders;
20+
1921
// ReSharper disable InvertIf
2022
// ReSharper disable SuggestBaseTypeForParameter
2123

@@ -26,8 +28,6 @@ class RequestContent : IDisposable {
2628
readonly RestRequest _request;
2729
readonly List<Stream> _streams = new();
2830

29-
30-
3131
HttpContent? Content { get; set; }
3232

3333
public RequestContent(RestClient client, RestRequest request) {
@@ -55,8 +55,7 @@ void AddFiles() {
5555
_streams.Add(stream);
5656
var fileContent = new StreamContent(stream);
5757

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

6160
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
6261
Name = $"\"{file.Name}\"",
@@ -97,8 +96,7 @@ HttpContent GetSerialized() {
9796

9897
var content = serializer.Serialize(body);
9998

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

103101
return new StringContent(
104102
content,
@@ -150,20 +148,33 @@ void AddPostParameters(ParametersCollection? postParameters) {
150148
// we got the multipart form already instantiated, just add parameters to it
151149
foreach (var postParameter in postParameters!) {
152150
var parameterName = postParameter.Name!;
151+
153152
mpContent.Add(
154153
new StringContent(postParameter.Value!.ToString()!, _client.Options.Encoding, postParameter.ContentType),
155154
_request.MultipartFormQuoteParameters ? $"\"{parameterName}\"" : parameterName
156155
);
157156
}
158157
}
159158
else {
160-
// we should not have anything else except the parameters, so we send them as form URL encoded
159+
#if NETCORE
160+
// We should not have anything else except the parameters, so we send them as form URL encoded.
161161
var formContent = new FormUrlEncodedContent(
162162
_request.Parameters
163163
.Where(x => x.Type == ParameterType.GetOrPost)
164164
.Select(x => new KeyValuePair<string, string>(x.Name!, x.Value!.ToString()!))!
165165
);
166166
Content = formContent;
167+
#else
168+
// However due to bugs in HttpClient FormUrlEncodedContent (see https://github.com/restsharp/RestSharp/issues/1814) we
169+
// do the encoding ourselves using WebUtility.UrlEncode instead.
170+
var formData = _request.Parameters
171+
.Where(x => x.Type == ParameterType.GetOrPost)
172+
.Select(x => new KeyValuePair<string, string>(x.Name!, x.Value!.ToString()!))!;
173+
var encodedItems = formData.Select(i => $"{WebUtility.UrlEncode(i.Key)}={WebUtility.UrlEncode(i.Value)}" /*.Replace("%20", "+")*/);
174+
var encodedContent = new StringContent(string.Join("&", encodedItems), null, "application/x-www-form-urlencoded");
175+
176+
Content = encodedContent;
177+
#endif
167178
}
168179
}
169180

Diff for: test/Directory.Build.props

+4-1
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"/>

Diff for: test/RestSharp.InteractiveTests/RestSharp.InteractiveTests.csproj

+1
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" />

Diff for: test/RestSharp.Tests.Integrated/PostTests.cs

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Net;
12
using RestSharp.Tests.Integrated.Server;
23

34
namespace RestSharp.Tests.Integrated;
@@ -14,23 +15,39 @@ public async Task Should_post_json() {
1415
var request = new RestRequest("post/json").AddJsonBody(body);
1516
var response = await _client.ExecutePostAsync<TestResponse>(request);
1617

17-
response.Data.Message.Should().Be(body.Data);
18+
response!.Data!.Message.Should().Be(body.Data);
1819
}
19-
20+
2021
[Fact]
2122
public async Task Should_post_json_with_PostAsync() {
2223
var body = new TestRequest("foo", 100);
2324
var request = new RestRequest("post/json").AddJsonBody(body);
2425
var response = await _client.PostAsync<TestResponse>(request);
2526

26-
response.Message.Should().Be(body.Data);
27+
response!.Message.Should().Be(body.Data);
2728
}
28-
29+
2930
[Fact]
3031
public async Task Should_post_json_with_PostJsonAsync() {
3132
var body = new TestRequest("foo", 100);
3233
var response = await _client.PostJsonAsync<TestRequest, TestResponse>("post/json", body);
3334

34-
response.Message.Should().Be(body.Data);
35+
response!.Message.Should().Be(body.Data);
36+
}
37+
38+
[Fact]
39+
public async Task Should_post_large_form_data() {
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);
45+
46+
response.StatusCode.Should().Be(HttpStatusCode.OK);
47+
response.Data!.Message.Should().Be($"Works! Length: {length}");
48+
}
49+
50+
class Response {
51+
public string Message { get; set; }
3552
}
3653
}

Diff for: test/RestSharp.Tests.Integrated/RestSharp.Tests.Integrated.csproj

+1
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" />

Diff for: test/RestSharp.Tests.Integrated/Server/TestServer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public HttpServer(ITestOutputHelper output = null) {
5353

5454
// POST
5555
_app.MapPost("/post/json", (TestRequest request) => new TestResponse { Message = request.Data });
56+
_app.MapPost("/post/form", (HttpContext context) => new TestResponse { Message = $"Works! Length: {context.Request.Form["big_string"].ToString().Length}" });
5657

5758
IResult HandleHeaders(HttpContext ctx) {
5859
var response = ctx.Request.Headers.Select(x => new TestServerResponse(x.Key, x.Value));

Diff for: test/RestSharp.Tests.Legacy/RestSharp.Tests.Legacy.csproj

+1-1
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>

Diff for: test/RestSharp.Tests.Serializers.Xml/RestSharp.Tests.Serializers.Xml.csproj

+1-1
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" />

Diff for: test/RestSharp.Tests.Serializers.Xml/XmlAttributeDeserializerTests.cs

+17-7
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+
}

Diff for: test/RestSharp.Tests.Serializers.Xml/XmlDeserializerTests.cs

+4
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)