-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathXmppFileUploadTests.cs
More file actions
196 lines (160 loc) · 4.7 KB
/
XmppFileUploadTests.cs
File metadata and controls
196 lines (160 loc) · 4.7 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Waher.Content.Binary;
using Waher.Networking.Sniffers;
using Waher.Networking.XMPP.HttpFileUpload;
using Waher.Runtime.Console;
namespace Waher.Networking.XMPP.Test
{
[TestClass]
public class XmppFileUploadTests
{
protected XmppClient client1;
protected Exception ex1 = null;
protected ManualResetEvent connected1 = new(false);
protected ManualResetEvent error1 = new(false);
protected ManualResetEvent offline1 = new(false);
private HttpFileUploadClient httpUpload = null;
[TestInitialize]
public void TestInitialize()
{
this.connected1.Reset();
this.error1.Reset();
this.offline1.Reset();
this.ex1 = null;
this.client1 = new XmppClient("xmpp.is", 5222, "unit.test", "testpassword", "en", typeof(CommunicationTests).Assembly)
{
DefaultNrRetries = 2,
DefaultRetryTimeout = 1000,
DefaultMaxRetryTimeout = 5000,
DefaultDropOff = true
};
this.client1.SetTag("ShowE2E", true);
this.client1.Add(new ConsoleOutSniffer(BinaryPresentationMethod.ByteCount, LineEnding.NewLine));
this.client1.OnConnectionError += this.Client_OnConnectionError1;
this.client1.OnError += this.Client_OnError1;
this.client1.OnStateChanged += this.Client_OnStateChanged1;
this.client1.SetPresence(Availability.Chat, new KeyValuePair<string, string>("en", "Live and well"));
this.client1.Connect();
this.WaitConnected1(5000);
}
private Task Client_OnStateChanged1(object _, XmppState NewState)
{
switch (NewState)
{
case XmppState.Connected:
this.connected1.Set();
break;
case XmppState.Error:
this.error1.Set();
break;
case XmppState.Offline:
this.offline1.Set();
break;
case XmppState.Connecting:
break;
}
return Task.CompletedTask;
}
Task Client_OnError1(object Sender, Exception Exception)
{
this.ex1 = Exception;
return Task.CompletedTask;
}
Task Client_OnConnectionError1(object Sender, Exception Exception)
{
this.ex1 = Exception;
return Task.CompletedTask;
}
private int Wait1(int Timeout)
{
return WaitHandle.WaitAny(new WaitHandle[] { this.connected1, this.error1, this.offline1 }, Timeout);
}
private void WaitConnected1(int Timeout)
{
AssertWaitConnected(this.Wait1(Timeout));
}
private static void AssertWaitConnected(int Event)
{
switch (Event)
{
case -1:
case WaitHandle.WaitTimeout:
Assert.Fail("Unable to connect. Timeout occurred.");
break;
case 0: // Connected
break;
case 1:
Assert.Fail("Unable to connect. Error occurred.");
break;
case 2:
Assert.Fail("Unable to connect. Client turned offline.");
break;
}
}
[TestCleanup]
public async Task TestCleanup()
{
if (this.client1 is not null)
{
await this.client1.OfflineAndDisposeAsync(false);
this.client1 = null;
}
if (this.ex1 is not null)
throw new TargetInvocationException(this.ex1);
}
[TestMethod]
public void FileUpload_Test_01_Discovery()
{
ManualResetEvent Done = new(false);
ManualResetEvent Error = new(false);
this.httpUpload = new HttpFileUploadClient(this.client1);
this.httpUpload.Discover((Sender, e) =>
{
if (this.httpUpload.HasSupport)
Done.Set();
else
Error.Set();
return Task.CompletedTask;
});
Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }), 5000);
ConsoleOut.WriteLine("JID: " + this.httpUpload.FileUploadJid);
ConsoleOut.WriteLine("Max File Size: " + this.httpUpload.MaxFileSize.ToString());
}
[TestMethod]
public void FileUpload_Test_02_RequestUploadSlot()
{
this.FileUpload_Test_01_Discovery();
string FileName = Guid.NewGuid().ToString().Replace("-", string.Empty) + ".bin";
string ContentType = BinaryCodec.DefaultContentType;
Random Rnd = new();
byte[] Bin = new byte[1024];
Rnd.NextBytes(Bin);
ManualResetEvent Done = new(false);
ManualResetEvent Error = new(false);
HttpFileUploadEventArgs e = null;
this.httpUpload.RequestUploadSlot(FileName, ContentType, Bin.Length, (sender, e2) =>
{
e = e2;
if (e2.Ok)
Done.Set();
else
Error.Set();
return Task.CompletedTask;
}, null);
Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }), 5000);
ConsoleOut.WriteLine("GET URL:" + e.GetUrl);
ConsoleOut.WriteLine("PUT URL:" + e.PutUrl);
ConsoleOut.WriteLine("PUT Headers:");
if (e.PutHeaders is not null)
{
foreach (KeyValuePair<string, string> Header in e.PutHeaders)
ConsoleOut.WriteLine(Header.Key + ": " + Header.Value);
}
}
}
}