Skip to content

Commit 497d619

Browse files
committed
Added download data tests
1 parent f8612cc commit 497d619

4 files changed

Lines changed: 133 additions & 57 deletions

File tree

Directory.Packages.props

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
11
<Project>
2-
<PropertyGroup>
3-
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4-
</PropertyGroup>
5-
<PropertyGroup Label="Package versions for .NET 10" Condition="'$(TargetFramework)' == 'net10.0'">
6-
<MicrosoftTestHostVer>10.0.0-rc.2.25502.107</MicrosoftTestHostVer>
7-
<SystemTextJsonVer>10.0.0-rc.2.25502.107</SystemTextJsonVer>
8-
</PropertyGroup>
9-
<PropertyGroup Label="Package versions for .NET 9" Condition="'$(TargetFramework)' == 'net9.0'">
10-
<MicrosoftTestHostVer>9.0.10</MicrosoftTestHostVer>
11-
</PropertyGroup>
12-
<PropertyGroup Label="Package versions for .NET 8" Condition="'$(TargetFramework)' == 'net8.0'">
13-
<MicrosoftTestHostVer>8.0.21</MicrosoftTestHostVer>
14-
</PropertyGroup>
15-
<PropertyGroup Label="Package versions for pre-.NET 10" Condition="'$(TargetFramework)' != 'net10.0'">
16-
<SystemTextJsonVer>9.0.10</SystemTextJsonVer>
17-
</PropertyGroup>
18-
<ItemGroup Label="Runtime dependencies">
19-
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3"/>
20-
<PackageVersion Include="CsvHelper" Version="33.0.1"/>
21-
<PackageVersion Include="System.Text.Json" Version="$(SystemTextJsonVer)"/>
22-
</ItemGroup>
23-
<ItemGroup Label="Compile dependencies">
24-
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0"/>
25-
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0"/>
26-
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0"/>
27-
<PackageVersion Include="MinVer" Version="6.0.0"/>
28-
<PackageVersion Include="Nullable" Version="1.3.1"/>
29-
<PackageVersion Include="Microsoft.NETFramework.ReferenceAssemblies.net472" Version="1.0.3"/>
30-
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0"/>
31-
<PackageVersion Include="JetBrains.Annotations" Version="2024.3.0"/>
32-
</ItemGroup>
33-
<ItemGroup Label="Testing dependencies">
34-
<PackageVersion Include="HttpMultipartParser" Version="8.4.0"/>
35-
<PackageVersion Include="PolySharp" Version="1.15.0"/>
36-
<PackageVersion Include="AutoFixture" Version="4.18.1"/>
37-
<PackageVersion Include="coverlet.collector" Version="6.0.2"/>
38-
<PackageVersion Include="FluentAssertions" Version="7.0.0"/>
39-
<PackageVersion Include="HttpTracer" Version="2.1.1"/>
40-
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="$(MicrosoftTestHostVer)"/>
41-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
42-
<PackageVersion Include="Moq" Version="4.20.72"/>
43-
<PackageVersion Include="Polly" Version="8.5.0"/>
44-
<PackageVersion Include="rest-mock-core" Version="0.7.12"/>
45-
<PackageVersion Include="RichardSzalay.MockHttp" Version="7.0.0"/>
46-
<PackageVersion Include="System.Net.Http.Json" Version="9.0.0"/>
47-
<PackageVersion Include="Xunit.Extensions.Logging" Version="1.1.0"/>
48-
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="All"/>
49-
<PackageVersion Include="xunit" Version="2.9.2"/>
50-
<PackageVersion Include="WireMock.Net" Version="1.6.10"/>
51-
<PackageVersion Include="WireMock.Net.FluentAssertions" Version="1.5.51"/>
52-
</ItemGroup>
2+
<PropertyGroup>
3+
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4+
</PropertyGroup>
5+
<PropertyGroup Label="Package versions for .NET 10" Condition="'$(TargetFramework)' == 'net10.0'">
6+
<MicrosoftTestHostVer>10.0.0-rc.2.25502.107</MicrosoftTestHostVer>
7+
<SystemTextJsonVer>10.0.0-rc.2.25502.107</SystemTextJsonVer>
8+
</PropertyGroup>
9+
<PropertyGroup Label="Package versions for .NET 9" Condition="'$(TargetFramework)' == 'net9.0'">
10+
<MicrosoftTestHostVer>9.0.10</MicrosoftTestHostVer>
11+
</PropertyGroup>
12+
<PropertyGroup Label="Package versions for .NET 8" Condition="'$(TargetFramework)' == 'net8.0'">
13+
<MicrosoftTestHostVer>8.0.21</MicrosoftTestHostVer>
14+
</PropertyGroup>
15+
<PropertyGroup Label="Package versions for pre-.NET 10" Condition="'$(TargetFramework)' != 'net10.0'">
16+
<SystemTextJsonVer>9.0.10</SystemTextJsonVer>
17+
</PropertyGroup>
18+
<ItemGroup Label="Runtime dependencies">
19+
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
20+
<PackageVersion Include="CsvHelper" Version="33.0.1" />
21+
<PackageVersion Include="System.Text.Json" Version="$(SystemTextJsonVer)" />
22+
</ItemGroup>
23+
<ItemGroup Label="Compile dependencies">
24+
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
25+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" />
26+
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" />
27+
<PackageVersion Include="MinVer" Version="6.0.0" />
28+
<PackageVersion Include="Nullable" Version="1.3.1" />
29+
<PackageVersion Include="Microsoft.NETFramework.ReferenceAssemblies.net472" Version="1.0.3" />
30+
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
31+
<PackageVersion Include="JetBrains.Annotations" Version="2024.3.0" />
32+
</ItemGroup>
33+
<ItemGroup Label="Testing dependencies">
34+
<PackageVersion Include="HttpMultipartParser" Version="8.4.0" />
35+
<PackageVersion Include="PolySharp" Version="1.15.0" />
36+
<PackageVersion Include="AutoFixture" Version="4.18.1" />
37+
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
38+
<PackageVersion Include="FluentAssertions" Version="7.0.0" />
39+
<PackageVersion Include="HttpTracer" Version="2.1.1" />
40+
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="$(MicrosoftTestHostVer)" />
41+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
42+
<PackageVersion Include="Moq" Version="4.20.72" />
43+
<PackageVersion Include="Polly" Version="8.5.0" />
44+
<PackageVersion Include="rest-mock-core" Version="0.7.12" />
45+
<PackageVersion Include="RichardSzalay.MockHttp" Version="7.0.0" />
46+
<PackageVersion Include="System.Net.Http.Json" Version="9.0.0" />
47+
<PackageVersion Include="Xunit.Extensions.Logging" Version="1.1.0" />
48+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="All" />
49+
<PackageVersion Include="xunit" Version="2.9.2" />
50+
<PackageVersion Include="WireMock.Net" Version="1.15.0" />
51+
<PackageVersion Include="WireMock.Net.FluentAssertions" Version="1.5.51" />
52+
</ItemGroup>
5353
</Project>

src/RestSharp/RestClient.Extensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,10 @@ [EnumeratorCancellation] CancellationToken cancellationToken
171171

172172
using var reader = new StreamReader(stream);
173173

174-
while (!reader.EndOfStream && !cancellationToken.IsCancellationRequested) {
175174
#if NET7_0_OR_GREATER
176-
var line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false);
175+
while (await reader.ReadLineAsync(cancellationToken) is { } line && !cancellationToken.IsCancellationRequested) {
177176
#else
178-
var line = await reader.ReadLineAsync().ConfigureAwait(false);
177+
while (await reader.ReadLineAsync() is { } line && !cancellationToken.IsCancellationRequested) {
179178
#endif
180179
if (string.IsNullOrWhiteSpace(line)) continue;
181180

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using RestSharp.Extensions;
2+
3+
namespace RestSharp.Tests.Integrated;
4+
5+
public sealed class DownloadDataTests : IDisposable {
6+
const string LocalPath = "Assets/Koala.jpg";
7+
8+
readonly WireMockServer _server = WireMockServer.Start();
9+
readonly RestClient _client;
10+
readonly string _path = AppDomain.CurrentDomain.BaseDirectory;
11+
12+
public DownloadDataTests() {
13+
var pathToFile = Path.Combine(_path, Path.Combine(LocalPath.Split('/')));
14+
15+
_server
16+
.Given(Request.Create().WithPath($"/{LocalPath}"))
17+
.RespondWith(Response.Create().WithBodyFromFile(pathToFile));
18+
var options = new RestClientOptions($"{_server.Url}/{LocalPath}") { ThrowOnAnyError = true };
19+
_client = new(options);
20+
}
21+
22+
public void Dispose() => _server.Dispose();
23+
24+
[Fact]
25+
public void DownloadDataAsync_returns_null_when_stream_is_null() {
26+
var request = new RestRequest("/invalid-endpoint");
27+
28+
var action = () => _client.DownloadData(request);
29+
30+
action.Should().ThrowExactly<HttpRequestException>();
31+
}
32+
33+
[Fact]
34+
public async Task DownloadDataAsync_returns_bytes_when_stream_has_content() {
35+
var request = new RestRequest("");
36+
37+
var bytes = await _client.DownloadDataAsync(request);
38+
39+
bytes.Should().NotBeNull();
40+
bytes!.Length.Should().BeGreaterThan(0);
41+
}
42+
43+
[Fact]
44+
public void DownloadData_sync_wraps_async() {
45+
var request = new RestRequest("");
46+
47+
var bytes = _client.DownloadData(request);
48+
49+
bytes.Should().NotBeNull();
50+
bytes!.Length.Should().BeGreaterThan(0);
51+
}
52+
53+
[Fact]
54+
public void DownloadStream_sync_wraps_async() {
55+
var request = new RestRequest("");
56+
57+
using var stream = _client.DownloadStream(request);
58+
59+
stream.Should().NotBeNull();
60+
}
61+
62+
[Fact]
63+
public async Task DownloadStream_then_ReadAsBytes_matches_DownloadDataAsync() {
64+
var request = new RestRequest("");
65+
66+
#if NET6_0_OR_GREATER
67+
await using var stream = await _client.DownloadStreamAsync(request);
68+
#else
69+
using var stream = await _client.DownloadStreamAsync(request);
70+
#endif
71+
var bytesFromStream = stream == null ? null : await stream.ReadAsBytes(default);
72+
73+
var bytesDirect = await _client.DownloadDataAsync(request);
74+
75+
bytesFromStream.Should().NotBeNull();
76+
bytesDirect.Should().NotBeNull();
77+
bytesFromStream!.Should().BeEquivalentTo(bytesDirect);
78+
}
79+
}

test/RestSharp.Tests.Integrated/DownloadFileTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ public sealed class DownloadFileTests : IDisposable {
77
const string LocalPath = "Assets/Koala.jpg";
88

99
public DownloadFileTests() {
10-
// _server = HttpServerFixture.StartServer("Assets/Koala.jpg", FileHandler);
11-
1210
var pathToFile = Path.Combine(_path, Path.Combine(LocalPath.Split('/')));
1311

1412
_server
1513
.Given(Request.Create().WithPath($"/{LocalPath}"))
1614
.RespondWith(Response.Create().WithBodyFromFile(pathToFile));
1715
var options = new RestClientOptions($"{_server.Url}/{LocalPath}") { ThrowOnAnyError = true };
18-
_client = new RestClient(options);
16+
_client = new(options);
1917
}
2018

2119
public void Dispose() => _server.Dispose();

0 commit comments

Comments
 (0)