forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebHeaderCollectionTest.cs
More file actions
760 lines (661 loc) · 26.2 KB
/
WebHeaderCollectionTest.cs
File metadata and controls
760 lines (661 loc) · 26.2 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
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
754
755
756
757
758
759
760
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections;
using System.Collections.Generic;
using Xunit;
namespace System.Net.Tests
{
public partial class WebHeaderCollectionTest
{
[Fact]
public void Ctor_Success()
{
new WebHeaderCollection();
}
[Fact]
public void DefaultPropertyValues_ReturnEmptyAfterConstruction_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
Assert.Equal(0, w.AllKeys.Length);
Assert.Equal(0, w.Count);
Assert.Equal("\r\n", w.ToString());
Assert.Empty(w);
Assert.Empty(w.AllKeys);
}
[Fact]
public void HttpRequestHeader_Add_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w[HttpRequestHeader.Connection] = "keep-alive";
Assert.Equal(1, w.Count);
Assert.Equal("keep-alive", w[HttpRequestHeader.Connection]);
Assert.Equal("Connection", w.AllKeys[0]);
}
[Theory]
[InlineData((HttpRequestHeader)int.MinValue)]
[InlineData((HttpRequestHeader)(-1))]
[InlineData((HttpRequestHeader)int.MaxValue)]
public void HttpRequestHeader_AddInvalid_Throws(HttpRequestHeader header)
{
WebHeaderCollection w = new WebHeaderCollection();
Assert.Throws<IndexOutOfRangeException>(() => w[header] = "foo");
}
[Theory]
[InlineData((HttpResponseHeader)int.MinValue)]
[InlineData((HttpResponseHeader)(-1))]
[InlineData((HttpResponseHeader)int.MaxValue)]
public void HttpResponseHeader_AddInvalid_Throws(HttpResponseHeader header)
{
WebHeaderCollection w = new WebHeaderCollection();
Assert.Throws<IndexOutOfRangeException>(() => w[header] = "foo");
}
[Fact]
public void CustomHeader_AddQuery_Success()
{
string customHeader = "Custom-Header";
string customValue = "Custom;.-Value";
WebHeaderCollection w = new WebHeaderCollection();
w[customHeader] = customValue;
Assert.Equal(1, w.Count);
Assert.Equal(customValue, w[customHeader]);
Assert.Equal(customHeader, w.AllKeys[0]);
}
[Fact]
public void HttpResponseHeader_AddQuery_CommonHeader_Success()
{
string headerValue = "value123";
WebHeaderCollection w = new WebHeaderCollection();
w[HttpResponseHeader.ProxyAuthenticate] = headerValue;
w[HttpResponseHeader.WwwAuthenticate] = headerValue;
Assert.Equal(headerValue, w[HttpResponseHeader.ProxyAuthenticate]);
Assert.Equal(headerValue, w[HttpResponseHeader.WwwAuthenticate]);
}
[Fact]
public void HttpRequest_AddQuery_CommonHeader_Success()
{
string headerValue = "value123";
WebHeaderCollection w = new WebHeaderCollection();
w[HttpRequestHeader.Accept] = headerValue;
Assert.Equal(headerValue, w[HttpRequestHeader.Accept]);
}
[Fact]
public void RequestThenResponseHeaders_Add_Throws()
{
WebHeaderCollection w = new WebHeaderCollection();
w[HttpRequestHeader.Accept] = "text/json";
Assert.Throws<InvalidOperationException>(() => w[HttpResponseHeader.ContentLength] = "123");
}
[Fact]
public void ResponseThenRequestHeaders_Add_Throws()
{
WebHeaderCollection w = new WebHeaderCollection();
w[HttpResponseHeader.ContentLength] = "123";
Assert.Throws<InvalidOperationException>(() => w[HttpRequestHeader.Accept] = "text/json");
}
[Fact]
public void ResponseHeader_QueryRequest_Throws()
{
WebHeaderCollection w = new WebHeaderCollection();
w[HttpResponseHeader.ContentLength] = "123";
Assert.Throws<InvalidOperationException>(() => w[HttpRequestHeader.Accept]);
}
[Fact]
public void RequestHeader_QueryResponse_Throws()
{
WebHeaderCollection w = new WebHeaderCollection();
w[HttpRequestHeader.Accept] = "text/json";
Assert.Throws<InvalidOperationException>(() => w[HttpResponseHeader.ContentLength]);
}
[Fact]
public void Setter_ValidName_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w["Accept"] = "text/json";
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void Setter_NullOrEmptyName_Throws(string name)
{
WebHeaderCollection w = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentNullException>("name", () => w[name] = "test");
}
public static object[][] InvalidNames = {
new object[] { "(" },
new object[] { "\u1234" },
new object[] { "\u0019" }
};
[Theory, MemberData(nameof(InvalidNames))]
public void Setter_InvalidName_Throws(string name)
{
WebHeaderCollection w = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentException>("name", () => w[name] = "test");
}
public static object[][] InvalidValues = {
new object[] { "value1\rvalue2\r" },
new object[] { "value1\nvalue2\r" },
new object[] { "value1\u007fvalue2" },
new object[] { "value1\r\nvalue2" },
new object[] { "value1\u0019value2" }
};
[Theory, MemberData(nameof(InvalidValues))]
public void Setter_InvalidValue_Throws(string value)
{
WebHeaderCollection w = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentException>("value", () => w["custom"] = value);
}
public static object[][] ValidValues = {
new object[] { null },
new object[] { "" },
new object[] { "value1\r\n" },
new object[] { "value1\tvalue2" },
new object[] { "value1\r\n\tvalue2" },
new object[] { "value1\r\n value2" }
};
[Theory, MemberData(nameof(ValidValues))]
public void Setter_ValidValue_Success(string value)
{
WebHeaderCollection w = new WebHeaderCollection();
w["custom"] = value;
}
[Theory]
[InlineData("name", "name")]
[InlineData("name", "NaMe")]
[InlineData("nAmE", "name")]
public void Setter_SameHeaderTwice_Success(string firstName, string secondName)
{
WebHeaderCollection w = new WebHeaderCollection();
w[firstName] = "first";
w[secondName] = "second";
Assert.Equal(1, w.Count);
Assert.NotEmpty(w);
Assert.NotEmpty(w.AllKeys);
Assert.Equal(new[] { firstName }, w.AllKeys);
Assert.Equal("second", w[firstName]);
Assert.Equal("second", w[secondName]);
}
[Theory]
[InlineData("name")]
[InlineData("nAMe")]
public void Remove_HeaderExists_RemovesFromCollection(string name)
{
var headers = new WebHeaderCollection()
{
{ "name", "value" }
};
headers.Remove(name);
Assert.Empty(headers);
headers.Remove(name);
Assert.Empty(headers);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void Remove_NullOrEmptyHeader_ThrowsArgumentNullException(string name)
{
var headers = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentNullException>("name", () => headers.Remove(name));
}
[Theory]
[InlineData(" \r \t \n")]
[InlineData(" name ")]
[MemberData(nameof(InvalidValues))]
public void Remove_InvalidHeader_ThrowsArgumentException(string name)
{
var headers = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentException>("name", () => headers.Remove(name));
}
[Fact]
public void Remove_IllegalCharacter_Throws()
{
WebHeaderCollection w = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentException>("name", () => w.Remove("{"));
}
[Fact]
public void Remove_EmptyCollection_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Remove("foo");
Assert.Equal(0, w.Count);
Assert.Empty(w);
Assert.Empty(w.AllKeys);
}
[Theory]
[InlineData("name", "name")]
[InlineData("name", "NaMe")]
public void Remove_SetThenRemove_Success(string setName, string removeName)
{
WebHeaderCollection w = new WebHeaderCollection();
w[setName] = "value";
w.Remove(removeName);
Assert.Equal(0, w.Count);
Assert.Empty(w);
Assert.Empty(w.AllKeys);
}
[Theory]
[InlineData("name", "name")]
[InlineData("name", "NaMe")]
public void Remove_SetTwoThenRemoveOne_Success(string setName, string removeName)
{
WebHeaderCollection w = new WebHeaderCollection();
w[setName] = "value";
w["foo"] = "bar";
w.Remove(removeName);
Assert.Equal(1, w.Count);
Assert.NotEmpty(w);
Assert.NotEmpty(w.AllKeys);
Assert.Equal(new[] { "foo" }, w.AllKeys);
Assert.Equal("bar", w["foo"]);
}
[Fact]
public void Getter_EmptyCollection_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
Assert.Null(w["name"]);
Assert.Equal(0, w.Count);
Assert.Empty(w);
Assert.Empty(w.AllKeys);
}
[Fact]
public void Getter_NonEmptyCollectionNonExistentHeader_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w["name"] = "value";
Assert.Null(w["foo"]);
Assert.Equal(1, w.Count);
Assert.NotEmpty(w);
Assert.NotEmpty(w.AllKeys);
Assert.Equal(new[] { "name" }, w.AllKeys);
Assert.Equal("value", w["name"]);
}
[Fact]
public void Getter_Success()
{
string[] keys = { "Accept", "uPgRaDe", "Custom" };
string[] values = { "text/plain, text/html", " HTTP/2.0 , SHTTP/1.3, , RTA/x11 ", "\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\"" };
WebHeaderCollection w = new WebHeaderCollection();
for (int i = 0; i < keys.Length; ++i)
{
string key = keys[i];
string value = values[i];
w[key] = value;
}
for (int i = 0; i < keys.Length; ++i)
{
string key = keys[i];
string expected = values[i].Trim();
Assert.Equal(expected, w[key]);
Assert.Equal(expected, w[key.ToUpperInvariant()]);
Assert.Equal(expected, w[key.ToLowerInvariant()]);
}
}
[Fact]
public void ToString_Empty_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
Assert.Equal("\r\n", w.ToString());
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void ToString_SingleHeaderWithEmptyValue_Success(string value)
{
WebHeaderCollection w = new WebHeaderCollection();
w["name"] = value;
Assert.Equal("name: \r\n\r\n", w.ToString());
}
[Fact]
public void ToString_NotEmpty_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w["Accept"] = "text/plain";
w["Content-Length"] = "123";
Assert.Equal(
"Accept: text/plain\r\nContent-Length: 123\r\n\r\n",
w.ToString());
}
[Fact]
public void IterateCollection_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w["Accept"] = "text/plain";
w["Content-Length"] = "123";
string result = "";
foreach (var item in w)
{
result += item;
}
Assert.Equal("AcceptContent-Length", result);
}
[Fact]
public void Enumerator_Success()
{
string item1 = "Accept";
string item2 = "Content-Length";
string item3 = "Name";
WebHeaderCollection w = new WebHeaderCollection();
w[item1] = "text/plain";
w[item2] = "123";
w[item3] = "value";
IEnumerable collection = w;
IEnumerator e = collection.GetEnumerator();
for (int i = 0; i < 2; i++)
{
// Not started
Assert.Throws<InvalidOperationException>(() => e.Current);
Assert.True(e.MoveNext());
Assert.Same(item1, e.Current);
Assert.True(e.MoveNext());
Assert.Same(item2, e.Current);
Assert.True(e.MoveNext());
Assert.Same(item3, e.Current);
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
Assert.False(e.MoveNext());
// Ended
Assert.Throws<InvalidOperationException>(() => e.Current);
e.Reset();
}
}
public static IEnumerable<object[]> SerializeDeserialize_Roundtrip_MemberData()
{
for (int i = 0; i < 10; i++)
{
var wc = new WebHeaderCollection();
for (int j = 0; j < i; j++)
{
wc[$"header{j}"] = $"value{j}";
}
yield return new object[] { wc };
}
}
public static IEnumerable<object[]> Add_Value_TestData()
{
yield return new object[] { null, string.Empty };
yield return new object[] { string.Empty, string.Empty };
yield return new object[] { "VaLue", "VaLue" };
yield return new object[] { " value ", "value" };
// Documentation says this should fail but it does not.
string longString = new string('a', 65536);
yield return new object[] { longString, longString };
}
[Theory]
[MemberData(nameof(Add_Value_TestData))]
public void Add_ValidValue_Success(string value, string expectedValue)
{
var headers = new WebHeaderCollection
{
{ "name", value }
};
Assert.Equal(expectedValue, headers["name"]);
}
[Fact]
public void Add_HeaderAlreadyExists_AppendsValue()
{
var headers = new WebHeaderCollection
{
{ "name", "value1" },
{ "name", null },
{ "name", "value2" },
{ "NAME", "value3" },
{ "name", "" }
};
Assert.Equal("value1,,value2,value3,", headers["name"]);
}
[Fact]
public void Add_NullName_ThrowsArgumentNullException()
{
var headers = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentNullException>("name", () => headers.Add(null, "value"));
}
[Theory]
[InlineData("")]
[InlineData("(")]
[InlineData("\r \t \n")]
[InlineData(" name ")]
[MemberData(nameof(InvalidValues))]
public void Add_InvalidName_ThrowsArgumentException(string name)
{
var headers = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentException>("name", () => headers.Add(name, "value"));
}
[Theory]
[MemberData(nameof(InvalidValues))]
public void Add_InvalidValue_ThrowsArgumentException(string value)
{
var headers = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentException>("value", () => headers.Add("name", value));
}
[Fact]
public void Add_ValidHeader_AddsToHeaders()
{
var headers = new WebHeaderCollection()
{
"name:value1",
"name:",
"NaMe:value2",
"name: ",
};
Assert.Equal("value1,,value2,", headers["name"]);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void Add_NullHeader_ThrowsArgumentNullException(string header)
{
var headers = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentNullException>("header", () => headers.Add(header));
}
[Theory]
[InlineData(" \r \t \n", "header")]
[InlineData("nocolon", "header")]
[InlineData(" :value", "name")]
[InlineData("name :value", "name")]
[InlineData("name:va\rlue", "value")]
public void Add_InvalidHeader_ThrowsArgumentException(string header, string paramName)
{
var headers = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentException>(paramName, () => headers.Add(header));
}
private const string HeaderType = "Set-Cookie";
private const string Cookie1 = "locale=en; path=/; expires=Fri, 05 Oct 2018 06:28:57 -0000";
private const string Cookie2 = "uuid=123abc; path=/; expires=Fri, 05 Oct 2018 06:28:57 -0000; secure; HttpOnly";
private const string Cookie3 = "country=US; path=/; expires=Fri, 05 Oct 2018 06:28:57 -0000";
private const string Cookie4 = "m_session=session1; path=/; expires=Sun, 08 Oct 2017 00:28:57 -0000; secure; HttpOnly";
private const string Cookie1NoAttribute = "locale=en";
private const string Cookie2NoAttribute = "uuid=123abc";
private const string Cookie3NoAttribute = "country=US";
private const string Cookie4NoAttribute = "m_session=session1";
private const string CookieInvalid = "helloWorld";
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.Mono, "Does not work in Mono")]
public void GetValues_MultipleSetCookieHeadersWithExpiresAttribute_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add(HeaderType, Cookie1);
w.Add(HeaderType, Cookie2);
w.Add(HeaderType, Cookie3);
w.Add(HeaderType, Cookie4);
string[] values = w.GetValues(HeaderType);
Assert.Equal(4, values.Length);
Assert.Equal(Cookie1, values[0]);
Assert.Equal(Cookie2, values[1]);
Assert.Equal(Cookie3, values[2]);
Assert.Equal(Cookie4, values[3]);
}
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.Mono, "Does not work in Mono")]
public void GetValues_SingleSetCookieHeaderWithMultipleCookiesWithExpiresAttribute_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add(HeaderType, Cookie1 + "," + Cookie2 + "," + Cookie3 + "," + Cookie4);
string[] values = w.GetValues(HeaderType);
Assert.Equal(4, values.Length);
Assert.Equal(Cookie1, values[0]);
Assert.Equal(Cookie2, values[1]);
Assert.Equal(Cookie3, values[2]);
Assert.Equal(Cookie4, values[3]);
}
[Fact]
public void GetValues_MultipleSetCookieHeadersWithNoAttribute_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add(HeaderType, Cookie1NoAttribute);
w.Add(HeaderType, Cookie2NoAttribute);
w.Add(HeaderType, Cookie3NoAttribute);
w.Add(HeaderType, Cookie4NoAttribute);
string[] values = w.GetValues(HeaderType);
Assert.Equal(4, values.Length);
Assert.Equal(Cookie1NoAttribute, values[0]);
Assert.Equal(Cookie2NoAttribute, values[1]);
Assert.Equal(Cookie3NoAttribute, values[2]);
Assert.Equal(Cookie4NoAttribute, values[3]);
}
[Fact]
public void GetValues_SingleSetCookieHeaderWithMultipleCookiesWithNoAttribute_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add(HeaderType, Cookie1NoAttribute + "," + Cookie2NoAttribute + "," + Cookie3NoAttribute + "," + Cookie4NoAttribute);
string[] values = w.GetValues(HeaderType);
Assert.Equal(4, values.Length);
Assert.Equal(Cookie1NoAttribute, values[0]);
Assert.Equal(Cookie2NoAttribute, values[1]);
Assert.Equal(Cookie3NoAttribute, values[2]);
Assert.Equal(Cookie4NoAttribute, values[3]);
}
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.Mono, "Does not work in Mono")]
public void GetValues_InvalidSetCookieHeader_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add(HeaderType, CookieInvalid);
string[] values = w.GetValues(HeaderType);
Assert.Equal(0, values.Length);
}
[Fact]
public void GetValues_MultipleValuesHeader_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
string headerType = "Accept";
w.Add(headerType, "text/plain, text/html");
string[] values = w.GetValues(headerType);
Assert.Equal(2, values.Length);
Assert.Equal("text/plain", values[0]);
Assert.Equal("text/html", values[1]);
}
[Fact]
public void HttpRequestHeader_Add_Rmemove_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add(HttpRequestHeader.Warning, "Warning1");
Assert.Equal(1, w.Count);
Assert.Equal("Warning1", w[HttpRequestHeader.Warning]);
Assert.Equal("Warning", w.AllKeys[0]);
w.Remove(HttpRequestHeader.Warning);
Assert.Equal(0, w.Count);
}
[Fact]
public void HttpRequestHeader_Get_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add("header1", "value1");
w.Add("header1", "value2");
string[] values = w.GetValues(0);
Assert.Equal("value1", values[0]);
Assert.Equal("value2", values[1]);
}
[Fact]
public void HttpRequestHeader_ToByteArray_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add("header1", "value1");
w.Add("header1", "value2");
byte[] byteArr = w.ToByteArray();
Assert.NotEmpty(byteArr);
}
[Fact]
public void HttpRequestHeader_GetKey_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add("header1", "value1");
w.Add("header1", "value2");
Assert.NotEmpty(w.GetKey(0));
}
[Fact]
public void HttpRequestHeader_GetValues_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add("header1", "value1");
Assert.Equal("value1", w.GetValues("header1")[0]);
}
[Fact]
public void HttpRequestHeader_Clear_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add("header1", "value1");
w.Add("header1", "value2");
w.Clear();
Assert.Equal(0, w.Count);
}
[Fact]
public void HttpRequestHeader_IsRestricted_Success()
{
Assert.True(WebHeaderCollection.IsRestricted("Accept"));
Assert.False(WebHeaderCollection.IsRestricted("Age"));
Assert.False(WebHeaderCollection.IsRestricted("Accept", true));
}
[Fact]
public void HttpRequestHeader_AddHeader_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add(HttpRequestHeader.ContentLength, "10");
w.Add(HttpRequestHeader.ContentType, "text/html");
Assert.Equal(2,w.Count);
}
[Fact]
public void WebHeaderCollection_Keys_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Add(HttpRequestHeader.ContentLength, "10");
w.Add(HttpRequestHeader.ContentType, "text/html");
Assert.Equal(2, w.Keys.Count);
}
[Fact]
public void HttpRequestHeader_AddHeader_Failure()
{
WebHeaderCollection w = new WebHeaderCollection();
char[] arr = new char[ushort.MaxValue + 1];
string maxStr = new string(arr);
AssertExtensions.Throws<ArgumentException>("value", () => w.Add(HttpRequestHeader.ContentLength,maxStr));
AssertExtensions.Throws<ArgumentException>("value", () => w.Add("ContentLength", maxStr));
}
[Fact]
public void HttpResponseHeader_Set_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Set(HttpResponseHeader.ProxyAuthenticate, "value123");
Assert.Equal("value123", w[HttpResponseHeader.ProxyAuthenticate]);
}
[Fact]
public void HttpRequestHeader_Set_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Set(HttpRequestHeader.Connection, "keep-alive");
Assert.Equal(1, w.Count);
Assert.Equal("keep-alive", w[HttpRequestHeader.Connection]);
Assert.Equal("Connection", w.AllKeys[0]);
}
[Fact]
public void NameValue_Set_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
w.Set("firstName", "first");
Assert.Equal(1, w.Count);
Assert.NotEmpty(w);
Assert.NotEmpty(w.AllKeys);
Assert.Equal(new[] { "firstName" }, w.AllKeys);
Assert.Equal("first", w["firstName"]);
}
}
}