Repository navigation
Expand file tree
/
Copy pathAsyncProgressTests.cs
More file actions
122 lines (98 loc) · 4.11 KB
/
Copy pathAsyncProgressTests.cs
File metadata and controls
122 lines (98 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) Service Stack LLC. All Rights Reserved.
// License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints.Tests.Support.Host;
namespace ServiceStack.WebHost.Endpoints.Tests
{
public class AsyncProgressTests
{
private const string ListeningOn = "http://localhost:1337/";
ExampleAppHostHttpListener appHost;
[TestFixtureSetUp]
public void OnTestFixtureSetUp()
{
appHost = new ExampleAppHostHttpListener();
appHost.Init();
appHost.Start(ListeningOn);
}
[TestFixtureTearDown]
public void OnTestFixtureTearDown()
{
appHost.Dispose();
}
[Test]
public async Task Can_report_progress_when_downloading_GET_async()
{
var hold = AsyncServiceClient.BufferSize;
AsyncServiceClient.BufferSize = 100;
try
{
var asyncClient = new JsonServiceClient(ListeningOn);
var progress = new List<string>();
//Note: total = -1 when 'Transfer-Encoding: chunked'
//Available in ASP.NET or in HttpListener when downloading responses with known lengths:
//E.g: Strings, Files, etc.
asyncClient.OnDownloadProgress = (done, total) =>
progress.Add("{0}/{1} bytes downloaded".Fmt(done, total));
var response = await asyncClient.GetAsync(new TestProgressString());
progress.Each(x => x.Print());
Assert.That(response.Length, Is.GreaterThan(0));
Assert.That(progress.Count, Is.GreaterThan(0));
Assert.That(progress.First(), Is.EqualTo("100/1160 bytes downloaded"));
Assert.That(progress.Last(), Is.EqualTo("1160/1160 bytes downloaded"));
}
finally
{
AsyncServiceClient.BufferSize = hold;
}
}
[Test]
public async Task Can_report_progress_when_downloading_async_with_Post()
{
await AsyncDownloadWithProgress(new TestProgressString());
}
[Test]
public async Task Can_report_progress_when_downloading_async_with_Post_bytes()
{
await AsyncDownloadWithProgress(new TestProgressBytes());
}
[Test]
public async Task Can_report_progress_when_downloading_async_with_Post_File_bytes()
{
await AsyncDownloadWithProgress(new TestProgressBinaryFile());
}
[Test]
public async Task Can_report_progress_when_downloading_async_with_Post_File_text()
{
await AsyncDownloadWithProgress(new TestProgressTextFile());
}
private static async Task AsyncDownloadWithProgress<TResponse>(IReturn<TResponse> requestDto)
{
var hold = AsyncServiceClient.BufferSize;
AsyncServiceClient.BufferSize = 100;
try
{
var asyncClient = new JsonServiceClient(ListeningOn);
var progress = new List<string>();
//Note: total = -1 when 'Transfer-Encoding: chunked'
//Available in ASP.NET or in HttpListener when downloading responses with known lengths:
//E.g: Strings, Files, etc.
asyncClient.OnDownloadProgress = (done, total) =>
progress.Add("{0}/{1} bytes downloaded".Fmt(done, total));
var response = await asyncClient.PostAsync(requestDto);
progress.Each(x => x.Print());
Assert.That(progress.Count, Is.GreaterThan(0));
Assert.That(progress.First(), Is.EqualTo("100/1160 bytes downloaded"));
Assert.That(progress.Last(), Is.EqualTo("1160/1160 bytes downloaded"));
}
finally
{
AsyncServiceClient.BufferSize = hold;
}
}
}
}