-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathWebClientTest.cs
753 lines (628 loc) · 39.1 KB
/
WebClientTest.cs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net.Cache;
using System.Net.Test.Common;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace System.Net.Tests
{
using Configuration = System.Net.Test.Common.Configuration;
public class WebClientTest
{
[Fact]
public static void DefaultCtor_PropertiesReturnExpectedValues()
{
var wc = new WebClient();
Assert.Empty(wc.BaseAddress);
Assert.Null(wc.CachePolicy);
Assert.Null(wc.Credentials);
Assert.Equal(Encoding.Default, wc.Encoding);
Assert.Empty(wc.Headers);
Assert.False(wc.IsBusy);
Assert.NotNull(wc.Proxy);
Assert.Empty(wc.QueryString);
Assert.False(wc.UseDefaultCredentials);
}
[Fact]
public static void Properties_InvalidArguments_ThrowExceptions()
{
var wc = new WebClient();
AssertExtensions.Throws<ArgumentException>("value", () => { wc.BaseAddress = "http::/invalid url"; });
AssertExtensions.Throws<ArgumentNullException>("value", () => { wc.Encoding = null; });
}
[Fact]
public static void DownloadData_InvalidArguments_ThrowExceptions()
{
var wc = new WebClient();
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadData((string)null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadData((Uri)null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadDataAsync((Uri)null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadDataAsync((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.DownloadDataTaskAsync((string)null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadDataTaskAsync((Uri)null); });
}
[Fact]
public static void DownloadFile_InvalidArguments_ThrowExceptions()
{
var wc = new WebClient();
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadFile((string)null, ""); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadFile((Uri)null, ""); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadFileAsync((Uri)null, ""); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadFileAsync((Uri)null, "", null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.DownloadFileTaskAsync((string)null, ""); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadFileTaskAsync((Uri)null, ""); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.DownloadFile("http://localhost", null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.DownloadFile(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.DownloadFileAsync(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.DownloadFileAsync(new Uri("http://localhost"), null, null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.DownloadFileTaskAsync("http://localhost", null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.DownloadFileTaskAsync(new Uri("http://localhost"), null); });
}
[Fact]
public static void DownloadString_InvalidArguments_ThrowExceptions()
{
var wc = new WebClient();
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadString((string)null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadString((Uri)null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadStringAsync((Uri)null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadStringAsync((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.DownloadStringTaskAsync((string)null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.DownloadStringTaskAsync((Uri)null); });
}
[Fact]
public static void UploadData_InvalidArguments_ThrowExceptions()
{
var wc = new WebClient();
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadData((string)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadData((string)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadData((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadData((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadDataAsync((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadDataAsync((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadDataAsync((Uri)null, null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.UploadDataTaskAsync((string)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.UploadDataTaskAsync((string)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadDataTaskAsync((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadDataTaskAsync((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadData("http://localhost", null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadData("http://localhost", null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadData(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadData(new Uri("http://localhost"), null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadDataAsync(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadDataAsync(new Uri("http://localhost"), null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadDataAsync(new Uri("http://localhost"), null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadDataTaskAsync("http://localhost", null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadDataTaskAsync("http://localhost", null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadDataTaskAsync(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadDataTaskAsync(new Uri("http://localhost"), null, null); });
}
[Fact]
public static void UploadFile_InvalidArguments_ThrowExceptions()
{
var wc = new WebClient();
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadFile((string)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.UploadFile((string)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadFile((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadFile((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadFileAsync((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadFileAsync((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadFileAsync((Uri)null, null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.UploadFileTaskAsync((string)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.UploadFileTaskAsync((string)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadFileTaskAsync((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadFileTaskAsync((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.UploadFile("http://localhost", null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.UploadFile("http://localhost", null, null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.UploadFile(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.UploadFile(new Uri("http://localhost"), null, null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.UploadFileAsync(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.UploadFileAsync(new Uri("http://localhost"), null, null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.UploadFileAsync(new Uri("http://localhost"), null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.UploadFileTaskAsync("http://localhost", null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.UploadFileTaskAsync("http://localhost", null, null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.UploadFileTaskAsync(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("fileName", () => { wc.UploadFileTaskAsync(new Uri("http://localhost"), null, null); });
}
[Fact]
public static void UploadString_InvalidArguments_ThrowExceptions()
{
var wc = new WebClient();
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadString((string)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadString((string)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadString((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadString((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadStringAsync((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadStringAsync((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadStringAsync((Uri)null, null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.UploadStringTaskAsync((string)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.UploadStringTaskAsync((string)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadStringTaskAsync((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadStringTaskAsync((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadString("http://localhost", null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadString("http://localhost", null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadString(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadString(new Uri("http://localhost"), null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadStringAsync(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadStringAsync(new Uri("http://localhost"), null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadStringAsync(new Uri("http://localhost"), null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadStringTaskAsync("http://localhost", null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadStringTaskAsync("http://localhost", null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadStringTaskAsync(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadStringTaskAsync(new Uri("http://localhost"), null, null); });
}
[Fact]
public static void UploadValues_InvalidArguments_ThrowExceptions()
{
var wc = new WebClient();
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadValues((string)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadValues((string)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadValues((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadValues((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadValuesAsync((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadValuesAsync((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadValuesAsync((Uri)null, null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.UploadValuesTaskAsync((string)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", "path", () => { wc.UploadValuesTaskAsync((string)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadValuesTaskAsync((Uri)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.UploadValuesTaskAsync((Uri)null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadValues("http://localhost", null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadValues("http://localhost", null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadValues(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadValues(new Uri("http://localhost"), null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadValuesAsync(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadValuesAsync(new Uri("http://localhost"), null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadValuesAsync(new Uri("http://localhost"), null, null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadValuesTaskAsync("http://localhost", null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadValuesTaskAsync("http://localhost", null, null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadValuesTaskAsync(new Uri("http://localhost"), null); });
AssertExtensions.Throws<ArgumentNullException>("data", () => { wc.UploadValuesTaskAsync(new Uri("http://localhost"), null, null); });
}
[Fact]
public static void OpenWrite_InvalidArguments_ThrowExceptions()
{
var wc = new WebClient();
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.OpenWrite((string)null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.OpenWrite((string)null, null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.OpenWrite((Uri)null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.OpenWrite((Uri)null, null); });
}
[Fact]
public static void OpenRead_InvalidArguments_ThrowExceptions()
{
var wc = new WebClient();
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.OpenRead((string)null); });
AssertExtensions.Throws<ArgumentNullException>("address", () => { wc.OpenRead((Uri)null); });
}
[Fact]
public static void BaseAddress_Roundtrips()
{
var wc = new WebClient();
wc.BaseAddress = "http://localhost/";
Assert.Equal("http://localhost/", wc.BaseAddress);
wc.BaseAddress = null;
Assert.Equal(string.Empty, wc.BaseAddress);
}
[Fact]
public static void CachePolicy_Roundtrips()
{
var wc = new WebClient();
var c = new RequestCachePolicy(RequestCacheLevel.BypassCache);
wc.CachePolicy = c;
Assert.Same(c, wc.CachePolicy);
}
[Fact]
public static void Credentials_Roundtrips()
{
var wc = new WebClient();
var c = new DummyCredentials();
wc.Credentials = c;
Assert.Same(c, wc.Credentials);
wc.Credentials = null;
Assert.Null(wc.Credentials);
}
private sealed class DummyCredentials : ICredentials
{
public NetworkCredential GetCredential(Uri uri, string authType) => null;
}
[Fact]
public static void Proxy_Roundtrips()
{
var wc = new WebClient();
Assert.Same(WebRequest.DefaultWebProxy, wc.Proxy);
var p = new DummyProxy();
wc.Proxy = p;
Assert.Same(p, wc.Proxy);
wc.Proxy = null;
Assert.Null(wc.Proxy);
}
private sealed class DummyProxy : IWebProxy
{
public ICredentials Credentials { get; set; }
public Uri GetProxy(Uri destination) => null;
public bool IsBypassed(Uri host) => false;
}
[Fact]
public static void Encoding_Roundtrips()
{
var wc = new WebClient();
Encoding e = Encoding.UTF8;
wc.Encoding = e;
Assert.Same(e, wc.Encoding);
}
[Fact]
public static void Headers_Roundtrips()
{
var wc = new WebClient();
Assert.NotNull(wc.Headers);
Assert.Empty(wc.Headers);
wc.Headers = null;
Assert.NotNull(wc.Headers);
Assert.Empty(wc.Headers);
var whc = new WebHeaderCollection();
wc.Headers = whc;
Assert.Same(whc, wc.Headers);
}
[Fact]
public static void QueryString_Roundtrips()
{
var wc = new WebClient();
Assert.NotNull(wc.QueryString);
Assert.Empty(wc.QueryString);
wc.QueryString = null;
Assert.NotNull(wc.QueryString);
Assert.Empty(wc.QueryString);
var nvc = new NameValueCollection();
wc.QueryString = nvc;
Assert.Same(nvc, wc.QueryString);
}
[Fact]
public static void UseDefaultCredentials_Roundtrips()
{
var wc = new WebClient();
for (int i = 0; i < 2; i++)
{
wc.UseDefaultCredentials = true;
Assert.True(wc.UseDefaultCredentials);
wc.UseDefaultCredentials = false;
Assert.False(wc.UseDefaultCredentials);
}
}
[Fact]
public static async Task ResponseHeaders_ContainsHeadersAfterOperation()
{
var wc = new DerivedWebClient(); // verify we can use a derived type as well
Assert.Null(wc.ResponseHeaders);
await LoopbackServer.CreateServerAsync(async (server, url) =>
{
Task<string> download = wc.DownloadStringTaskAsync(url.ToString());
Assert.Null(wc.ResponseHeaders);
await server.AcceptConnectionSendResponseAndCloseAsync(additionalHeaders: "ArbitraryHeader: ArbitraryValue\r\n");
await download;
});
Assert.NotNull(wc.ResponseHeaders);
Assert.Equal("ArbitraryValue", wc.ResponseHeaders["ArbitraryHeader"]);
}
[OuterLoop("Uses external servers")]
[Theory]
[InlineData("Connection", "close")]
[InlineData("Expect", "100-continue")]
public static async Task RequestHeaders_AddDisallowedHeaderAndSendRequest_ThrowsWebException(string headerName, string headerValue)
{
var wc = new WebClient();
wc.Headers[headerName] = headerValue;
await Assert.ThrowsAsync<WebException>(() => wc.DownloadStringTaskAsync(Configuration.Http.RemoteEchoServer));
}
public static IEnumerable<object[]> RequestHeaders_AddHostHeaderAndSendRequest_ExpectedResult_MemberData()
{
yield return new object[] { $"http://{Configuration.Http.Host}", true };
yield return new object[] { Configuration.Http.Host, false };
}
[OuterLoop("Uses external servers")]
[Theory]
[MemberData(nameof(RequestHeaders_AddHostHeaderAndSendRequest_ExpectedResult_MemberData))]
public static async Task RequestHeaders_AddHostHeaderAndSendRequest_ExpectedResult(string hostHeaderValue, bool throwsWebException)
{
var wc = new WebClient();
wc.Headers["Host"] = hostHeaderValue;
if (throwsWebException)
{
await Assert.ThrowsAsync<WebException>(() => wc.DownloadStringTaskAsync(Configuration.Http.RemoteEchoServer));
}
else
{
await wc.DownloadStringTaskAsync(Configuration.Http.RemoteEchoServer);
}
}
[Fact]
public static async Task RequestHeaders_SpecialHeaders_RequestSucceeds()
{
var wc = new WebClient();
wc.Headers["Accept"] = "text/html";
wc.Headers["ContentType"] = "text/html; charset=utf-8";
wc.Headers["Referer"] = "http://localhost";
wc.Headers["User-Agent"] = ".NET";
await LoopbackServer.CreateServerAsync(async (server, url) =>
{
Task<string> download = wc.DownloadStringTaskAsync(url.ToString());
Assert.Null(wc.ResponseHeaders);
await server.AcceptConnectionSendResponseAndCloseAsync();
await download;
});
}
[Fact]
public static async Task ConcurrentOperations_Throw()
{
await LoopbackServer.CreateServerAsync((server, url) =>
{
var wc = new WebClient();
Task ignored = wc.DownloadDataTaskAsync(url); // won't complete
Assert.Throws<NotSupportedException>(() => { wc.DownloadData(url); });
Assert.Throws<NotSupportedException>(() => { wc.DownloadDataAsync(url); });
Assert.Throws<NotSupportedException>(() => { wc.DownloadDataTaskAsync(url); });
Assert.Throws<NotSupportedException>(() => { wc.DownloadString(url); });
Assert.Throws<NotSupportedException>(() => { wc.DownloadStringAsync(url); });
Assert.Throws<NotSupportedException>(() => { wc.DownloadStringTaskAsync(url); });
Assert.Throws<NotSupportedException>(() => { wc.DownloadFile(url, "path"); });
Assert.Throws<NotSupportedException>(() => { wc.DownloadFileAsync(url, "path"); });
Assert.Throws<NotSupportedException>(() => { wc.DownloadFileTaskAsync(url, "path"); });
Assert.Throws<NotSupportedException>(() => { wc.UploadData(url, new byte[42]); });
Assert.Throws<NotSupportedException>(() => { wc.UploadDataAsync(url, new byte[42]); });
Assert.Throws<NotSupportedException>(() => { wc.UploadDataTaskAsync(url, new byte[42]); });
Assert.Throws<NotSupportedException>(() => { wc.UploadString(url, "42"); });
Assert.Throws<NotSupportedException>(() => { wc.UploadStringAsync(url, "42"); });
Assert.Throws<NotSupportedException>(() => { wc.UploadStringTaskAsync(url, "42"); });
Assert.Throws<NotSupportedException>(() => { wc.UploadFile(url, "path"); });
Assert.Throws<NotSupportedException>(() => { wc.UploadFileAsync(url, "path"); });
Assert.Throws<NotSupportedException>(() => { wc.UploadFileTaskAsync(url, "path"); });
Assert.Throws<NotSupportedException>(() => { wc.UploadValues(url, new NameValueCollection()); });
Assert.Throws<NotSupportedException>(() => { wc.UploadValuesAsync(url, new NameValueCollection()); });
Assert.Throws<NotSupportedException>(() => { wc.UploadValuesTaskAsync(url, new NameValueCollection()); });
return Task.CompletedTask;
});
}
private sealed class DerivedWebClient : WebClient { }
}
public abstract class WebClientTestBase
{
public const int TimeoutMilliseconds = 30 * 1000;
public static readonly object[][] EchoServers = Configuration.Http.EchoServers;
public static readonly object[][] VerifyUploadServers = Configuration.Http.VerifyUploadServers;
const string ExpectedText =
"To be, or not to be, that is the question:" +
"Whether 'tis Nobler in the mind to suffer" +
"The Slings and Arrows of outrageous Fortune," +
"Or to take Arms against a Sea of troubles," +
"And by opposing end them:";
const string ExpectedTextAfterUrlEncode =
"To+be%2c+or+not+to+be%2c+that+is+the+question%3a" +
"Whether+%27tis+Nobler+in+the+mind+to+suffer" +
"The+Slings+and+Arrows+of+outrageous+Fortune%2c" +
"Or+to+take+Arms+against+a+Sea+of+troubles%2c" +
"And+by+opposing+end+them%3a";
protected abstract bool IsAsync { get; }
protected abstract Task<byte[]> DownloadDataAsync(WebClient wc, string address);
protected abstract Task DownloadFileAsync(WebClient wc, string address, string fileName);
protected abstract Task<string> DownloadStringAsync(WebClient wc, string address);
protected abstract Task<byte[]> UploadDataAsync(WebClient wc, string address, byte[] data);
protected abstract Task<byte[]> UploadFileAsync(WebClient wc, string address, string fileName);
protected abstract Task<string> UploadStringAsync(WebClient wc, string address, string data);
protected abstract Task<byte[]> UploadValuesAsync(WebClient wc, string address, NameValueCollection data);
protected abstract Task<Stream> OpenReadAsync(WebClient wc, string address);
protected abstract Task<Stream> OpenWriteAsync(WebClient wc, string address);
[Theory]
[InlineData(null)]
[InlineData("text/html; charset=utf-8")]
[InlineData("text/html; charset=us-ascii")]
public async Task DownloadString_Success(string contentType)
{
await LoopbackServer.CreateServerAsync(async (server, url) =>
{
var wc = new WebClient();
if (contentType != null)
{
wc.Headers[HttpRequestHeader.ContentType] = contentType;
}
Task<string> download = DownloadStringAsync(wc, url.ToString());
await server.AcceptConnectionSendResponseAndCloseAsync(content: ExpectedText);
Assert.Equal(ExpectedText, await download);
});
}
[Fact]
public async Task DownloadData_Success()
{
await LoopbackServer.CreateServerAsync(async (server, url) =>
{
var wc = new WebClient();
var downloadProgressInvoked = new TaskCompletionSource();
wc.DownloadProgressChanged += (s, e) => downloadProgressInvoked.TrySetResult();
Task<byte[]> download = DownloadDataAsync(wc, url.ToString());
await server.AcceptConnectionSendResponseAndCloseAsync(content: ExpectedText);
Assert.Equal(ExpectedText, Encoding.ASCII.GetString(await download));
if (IsAsync)
{
await downloadProgressInvoked.Task.WaitAsync(TimeSpan.FromMilliseconds(TimeoutMilliseconds));
}
});
}
[Fact]
public async Task DownloadData_LargeData_Success()
{
await LoopbackServer.CreateServerAsync(async (server, url) =>
{
string largeText = GetRandomText(1024 * 1024);
var downloadProgressInvokedWithContentLength = new TaskCompletionSource();
var wc = new WebClient();
wc.DownloadProgressChanged += (s, e) =>
{
if (e.TotalBytesToReceive == largeText.Length && e.BytesReceived < e.TotalBytesToReceive)
{
downloadProgressInvokedWithContentLength.TrySetResult();
}
};
Task<byte[]> download = DownloadDataAsync(wc, url.ToString());
await server.AcceptConnectionSendResponseAndCloseAsync(content: largeText);
Assert.Equal(largeText, Encoding.ASCII.GetString(await download));
if (IsAsync)
{
await downloadProgressInvokedWithContentLength.Task.WaitAsync(TimeSpan.FromMilliseconds(TimeoutMilliseconds));
}
});
}
[Fact]
public async Task DownloadFile_Success()
{
await LoopbackServer.CreateServerAsync(async (server, url) =>
{
string tempPath = Path.GetTempFileName();
try
{
var wc = new WebClient();
Task download = DownloadFileAsync(wc, url.ToString(), tempPath);
await server.AcceptConnectionSendResponseAndCloseAsync(content: ExpectedText);
await download;
Assert.Equal(ExpectedText, File.ReadAllText(tempPath));
}
finally
{
File.Delete(tempPath);
}
});
}
[Fact]
public async Task OpenRead_Success()
{
await LoopbackServer.CreateServerAsync(async (server, url) =>
{
var wc = new WebClient();
Task<Stream> download = OpenReadAsync(wc, url.ToString());
await server.AcceptConnectionSendResponseAndCloseAsync(content: ExpectedText);
using (var reader = new StreamReader(await download))
{
Assert.Equal(ExpectedText, await reader.ReadToEndAsync());
}
});
}
[OuterLoop("Uses external servers")]
[Theory]
[MemberData(nameof(EchoServers))]
public async Task OpenWrite_Success(Uri echoServer)
{
var wc = new WebClient();
using (Stream s = await OpenWriteAsync(wc, echoServer.ToString()))
{
byte[] data = Encoding.UTF8.GetBytes(ExpectedText);
await s.WriteAsync(data, 0, data.Length);
}
}
[OuterLoop("Uses external servers")]
[Theory]
[MemberData(nameof(VerifyUploadServers))]
public async Task UploadData_Success(Uri server)
{
var wc = new WebClient();
var uploadProgressInvoked = new TaskCompletionSource();
wc.UploadProgressChanged += (s, e) => uploadProgressInvoked.TrySetResult(); // to enable chunking of the upload
// Server will verify uploaded data. An exception will be thrown if there is a problem.
AddMD5Header(wc, ExpectedText);
byte[] ignored = await UploadDataAsync(wc, server.ToString(), Encoding.UTF8.GetBytes(ExpectedText));
if (IsAsync)
{
await uploadProgressInvoked.Task.WaitAsync(TimeSpan.FromMilliseconds(TimeoutMilliseconds));
}
}
[OuterLoop("Uses external servers")]
[Theory]
[MemberData(nameof(VerifyUploadServers))]
public async Task UploadData_LargeData_Success(Uri server)
{
var wc = new WebClient();
string largeText = GetRandomText(512 * 1024);
// Server will verify uploaded data. An exception will be thrown if there is a problem.
AddMD5Header(wc, largeText);
byte[] ignored = await UploadDataAsync(wc, server.ToString(), Encoding.UTF8.GetBytes(largeText));
}
private static string GetRandomText(int length) =>
new string(Enumerable.Range(0, 512 * 1024).Select(_ => (char)('a' + Random.Shared.Next(0, 26))).ToArray());
[OuterLoop("Uses external servers")]
[Theory]
[MemberData(nameof(EchoServers))]
public async Task UploadFile_Success(Uri echoServer)
{
string tempPath = Path.GetTempFileName();
try
{
File.WriteAllBytes(tempPath, Encoding.UTF8.GetBytes(ExpectedText));
var wc = new WebClient();
byte[] result = await UploadFileAsync(wc, echoServer.ToString(), tempPath);
Assert.Contains(ExpectedText, Encoding.UTF8.GetString(result));
}
finally
{
File.Delete(tempPath);
}
}
[OuterLoop("Uses external servers")]
[Theory]
[MemberData(nameof(VerifyUploadServers))]
public async Task UploadString_Success(Uri server)
{
var wc = new WebClient();
// Server will verify uploaded data. An exception will be thrown if there is a problem.
AddMD5Header(wc, ExpectedText);
string ignored = await UploadStringAsync(wc, server.ToString(), ExpectedText);
}
[OuterLoop("Uses external servers")]
[Theory]
[MemberData(nameof(EchoServers))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/79731")]
public async Task UploadValues_Success(Uri echoServer)
{
var wc = new WebClient();
byte[] result = await UploadValuesAsync(wc, echoServer.ToString(), new NameValueCollection() { { "Data", ExpectedText } });
Assert.Contains(ExpectedTextAfterUrlEncode, Encoding.UTF8.GetString(result));
}
private static void AddMD5Header(WebClient wc, string data)
{
// Compute MD5 hash of the data that will be uploaded. We convert the string to UTF-8 since
// that is the encoding used by WebClient when serializing the data on the wire.
string headerValue = Convert.ToBase64String(MD5.HashData(Encoding.UTF8.GetBytes(data)));
wc.Headers.Add("Content-MD5", headerValue);
}
}
public class SyncWebClientTest : WebClientTestBase
{
protected override bool IsAsync => false;
protected override Task<byte[]> DownloadDataAsync(WebClient wc, string address) => Task.Run(() => wc.DownloadData(address));
protected override Task DownloadFileAsync(WebClient wc, string address, string fileName) => Task.Run(() => wc.DownloadFile(address, fileName));
protected override Task<string> DownloadStringAsync(WebClient wc, string address) => Task.Run(() => wc.DownloadString(address));
protected override Task<byte[]> UploadDataAsync(WebClient wc, string address, byte[] data) => Task.Run(() => wc.UploadData(address, data));
protected override Task<byte[]> UploadFileAsync(WebClient wc, string address, string fileName) => Task.Run(() => wc.UploadFile(address, fileName));
protected override Task<string> UploadStringAsync(WebClient wc, string address, string data) => Task.Run(() => wc.UploadString(address, data));
protected override Task<byte[]> UploadValuesAsync(WebClient wc, string address, NameValueCollection data) => Task.Run(() => wc.UploadValues(address, data));
protected override Task<Stream> OpenReadAsync(WebClient wc, string address) => Task.Run(() => wc.OpenRead(address));
protected override Task<Stream> OpenWriteAsync(WebClient wc, string address) => Task.Run(() => wc.OpenWrite(address));
}
// NOTE: Today the XxTaskAsync APIs are implemented as wrappers for the XxAsync APIs.
// If that changes, we should add an EapWebClientTest here that targets those directly.
// In the meantime, though, there's no benefit to the extra testing it would provide.
public class TaskWebClientTest : WebClientTestBase
{
protected override bool IsAsync => true;
protected override Task<byte[]> DownloadDataAsync(WebClient wc, string address) => wc.DownloadDataTaskAsync(address);
protected override Task DownloadFileAsync(WebClient wc, string address, string fileName) => wc.DownloadFileTaskAsync(address, fileName);
protected override Task<string> DownloadStringAsync(WebClient wc, string address) => wc.DownloadStringTaskAsync(address);
protected override Task<byte[]> UploadDataAsync(WebClient wc, string address, byte[] data) => wc.UploadDataTaskAsync(address, data);
protected override Task<byte[]> UploadFileAsync(WebClient wc, string address, string fileName) => wc.UploadFileTaskAsync(address, fileName);
protected override Task<string> UploadStringAsync(WebClient wc, string address, string data) => wc.UploadStringTaskAsync(address, data);
protected override Task<byte[]> UploadValuesAsync(WebClient wc, string address, NameValueCollection data) => wc.UploadValuesTaskAsync(address, data);
protected override Task<Stream> OpenReadAsync(WebClient wc, string address) => wc.OpenReadTaskAsync(address);
protected override Task<Stream> OpenWriteAsync(WebClient wc, string address) => wc.OpenWriteTaskAsync(address);
}
}