1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System;
6 using System.Collections;
7 using System.Collections.Generic;
8 using System.Runtime.Serialization.Formatters.Tests;
9 
10 using Xunit;
11 
12 namespace System.Net.Tests
13 {
14     public partial class WebHeaderCollectionTest
15     {
16         [Fact]
Ctor_Success()17         public void Ctor_Success()
18         {
19             new WebHeaderCollection();
20         }
21 
22         [Fact]
DefaultPropertyValues_ReturnEmptyAfterConstruction_Success()23         public void DefaultPropertyValues_ReturnEmptyAfterConstruction_Success()
24         {
25             WebHeaderCollection w = new WebHeaderCollection();
26             Assert.Equal(0, w.AllKeys.Length);
27             Assert.Equal(0, w.Count);
28             Assert.Equal("\r\n", w.ToString());
29             Assert.Empty(w);
30             Assert.Empty(w.AllKeys);
31         }
32 
33         [Fact]
HttpRequestHeader_Add_Success()34         public void HttpRequestHeader_Add_Success()
35         {
36             WebHeaderCollection w = new WebHeaderCollection();
37             w[HttpRequestHeader.Connection] = "keep-alive";
38 
39             Assert.Equal(1, w.Count);
40             Assert.Equal("keep-alive", w[HttpRequestHeader.Connection]);
41             Assert.Equal("Connection", w.AllKeys[0]);
42         }
43 
44         [Theory]
45         [InlineData((HttpRequestHeader)int.MinValue)]
46         [InlineData((HttpRequestHeader)(-1))]
47         [InlineData((HttpRequestHeader)int.MaxValue)]
HttpRequestHeader_AddInvalid_Throws(HttpRequestHeader header)48         public void HttpRequestHeader_AddInvalid_Throws(HttpRequestHeader header)
49         {
50             WebHeaderCollection w = new WebHeaderCollection();
51             Assert.Throws<IndexOutOfRangeException>(() => w[header] = "foo");
52         }
53 
54         [Theory]
55         [InlineData((HttpResponseHeader)int.MinValue)]
56         [InlineData((HttpResponseHeader)(-1))]
57         [InlineData((HttpResponseHeader)int.MaxValue)]
HttpResponseHeader_AddInvalid_Throws(HttpResponseHeader header)58         public void HttpResponseHeader_AddInvalid_Throws(HttpResponseHeader header)
59         {
60             WebHeaderCollection w = new WebHeaderCollection();
61             Assert.Throws<IndexOutOfRangeException>(() => w[header] = "foo");
62         }
63 
64         [Fact]
CustomHeader_AddQuery_Success()65         public void CustomHeader_AddQuery_Success()
66         {
67             string customHeader = "Custom-Header";
68             string customValue = "Custom;.-Value";
69             WebHeaderCollection w = new WebHeaderCollection();
70             w[customHeader] = customValue;
71 
72             Assert.Equal(1, w.Count);
73             Assert.Equal(customValue, w[customHeader]);
74             Assert.Equal(customHeader, w.AllKeys[0]);
75         }
76 
77         [Fact]
HttpResponseHeader_AddQuery_CommonHeader_Success()78         public void HttpResponseHeader_AddQuery_CommonHeader_Success()
79         {
80             string headerValue = "value123";
81             WebHeaderCollection w = new WebHeaderCollection();
82             w[HttpResponseHeader.ProxyAuthenticate] = headerValue;
83             w[HttpResponseHeader.WwwAuthenticate] = headerValue;
84 
85             Assert.Equal(headerValue, w[HttpResponseHeader.ProxyAuthenticate]);
86             Assert.Equal(headerValue, w[HttpResponseHeader.WwwAuthenticate]);
87         }
88 
89         [Fact]
HttpRequest_AddQuery_CommonHeader_Success()90         public void HttpRequest_AddQuery_CommonHeader_Success()
91         {
92             string headerValue = "value123";
93             WebHeaderCollection w = new WebHeaderCollection();
94             w[HttpRequestHeader.Accept] = headerValue;
95 
96             Assert.Equal(headerValue, w[HttpRequestHeader.Accept]);
97         }
98 
99         [Fact]
RequestThenResponseHeaders_Add_Throws()100         public void RequestThenResponseHeaders_Add_Throws()
101         {
102             WebHeaderCollection w = new WebHeaderCollection();
103             w[HttpRequestHeader.Accept] = "text/json";
104             Assert.Throws<InvalidOperationException>(() => w[HttpResponseHeader.ContentLength] = "123");
105         }
106 
107         [Fact]
ResponseThenRequestHeaders_Add_Throws()108         public void ResponseThenRequestHeaders_Add_Throws()
109         {
110             WebHeaderCollection w = new WebHeaderCollection();
111             w[HttpResponseHeader.ContentLength] = "123";
112             Assert.Throws<InvalidOperationException>(() => w[HttpRequestHeader.Accept] = "text/json");
113         }
114 
115         [Fact]
ResponseHeader_QueryRequest_Throws()116         public void ResponseHeader_QueryRequest_Throws()
117         {
118             WebHeaderCollection w = new WebHeaderCollection();
119             w[HttpResponseHeader.ContentLength] = "123";
120             Assert.Throws<InvalidOperationException>(() => w[HttpRequestHeader.Accept]);
121         }
122 
123         [Fact]
RequestHeader_QueryResponse_Throws()124         public void RequestHeader_QueryResponse_Throws()
125         {
126             WebHeaderCollection w = new WebHeaderCollection();
127             w[HttpRequestHeader.Accept] = "text/json";
128             Assert.Throws<InvalidOperationException>(() => w[HttpResponseHeader.ContentLength]);
129         }
130 
131         [Fact]
Setter_ValidName_Success()132         public void Setter_ValidName_Success()
133         {
134             WebHeaderCollection w = new WebHeaderCollection();
135             w["Accept"] = "text/json";
136         }
137 
138         [Theory]
139         [InlineData(null)]
140         [InlineData("")]
Setter_NullOrEmptyName_Throws(string name)141         public void Setter_NullOrEmptyName_Throws(string name)
142         {
143             WebHeaderCollection w = new WebHeaderCollection();
144             AssertExtensions.Throws<ArgumentNullException>("name", () => w[name] = "test");
145         }
146 
147         public static object[][] InvalidNames = {
148             new object[] { "(" },
149             new object[] { "\u1234" },
150             new object[] { "\u0019" }
151         };
152 
153         [Theory, MemberData(nameof(InvalidNames))]
Setter_InvalidName_Throws(string name)154         public void Setter_InvalidName_Throws(string name)
155         {
156             WebHeaderCollection w = new WebHeaderCollection();
157             AssertExtensions.Throws<ArgumentException>("name", () => w[name] = "test");
158         }
159 
160         public static object[][] InvalidValues = {
161             new object[] { "value1\rvalue2\r" },
162             new object[] { "value1\nvalue2\r" },
163             new object[] { "value1\u007fvalue2" },
164             new object[] { "value1\r\nvalue2" },
165             new object[] { "value1\u0019value2" }
166         };
167 
168         [Theory, MemberData(nameof(InvalidValues))]
Setter_InvalidValue_Throws(string value)169         public void Setter_InvalidValue_Throws(string value)
170         {
171             WebHeaderCollection w = new WebHeaderCollection();
172             AssertExtensions.Throws<ArgumentException>("value", () => w["custom"] = value);
173         }
174 
175         public static object[][] ValidValues = {
176             new object[] { null },
177             new object[] { "" },
178             new object[] { "value1\r\n" },
179             new object[] { "value1\tvalue2" },
180             new object[] { "value1\r\n\tvalue2" },
181             new object[] { "value1\r\n value2" }
182         };
183 
184         [Theory, MemberData(nameof(ValidValues))]
Setter_ValidValue_Success(string value)185         public void Setter_ValidValue_Success(string value)
186         {
187             WebHeaderCollection w = new WebHeaderCollection();
188             w["custom"] = value;
189         }
190 
191         [Theory]
192         [InlineData("name", "name")]
193         [InlineData("name", "NaMe")]
194         [InlineData("nAmE", "name")]
Setter_SameHeaderTwice_Success(string firstName, string secondName)195         public void Setter_SameHeaderTwice_Success(string firstName, string secondName)
196         {
197             WebHeaderCollection w = new WebHeaderCollection();
198             w[firstName] = "first";
199             w[secondName] = "second";
200             Assert.Equal(1, w.Count);
201             Assert.NotEmpty(w);
202             Assert.NotEmpty(w.AllKeys);
203             Assert.Equal(new[] { firstName }, w.AllKeys);
204             Assert.Equal("second", w[firstName]);
205             Assert.Equal("second", w[secondName]);
206         }
207 
208         [Theory]
209         [InlineData("name")]
210         [InlineData("nAMe")]
Remove_HeaderExists_RemovesFromCollection(string name)211         public void Remove_HeaderExists_RemovesFromCollection(string name)
212         {
213             var headers = new WebHeaderCollection()
214             {
215                 { "name", "value" }
216             };
217             headers.Remove(name);
218             Assert.Empty(headers);
219 
220             headers.Remove(name);
221             Assert.Empty(headers);
222         }
223 
224         [Theory]
225         [InlineData(null)]
226         [InlineData("")]
Remove_NullOrEmptyHeader_ThrowsArgumentNullException(string name)227         public void Remove_NullOrEmptyHeader_ThrowsArgumentNullException(string name)
228         {
229             var headers = new WebHeaderCollection();
230             AssertExtensions.Throws<ArgumentNullException>("name", () => headers.Remove(name));
231         }
232 
233         [Theory]
234         [InlineData(" \r \t \n")]
235         [InlineData("  name  ")]
236         [MemberData(nameof(InvalidValues))]
Remove_InvalidHeader_ThrowsArgumentException(string name)237         public void Remove_InvalidHeader_ThrowsArgumentException(string name)
238         {
239             var headers = new WebHeaderCollection();
240             AssertExtensions.Throws<ArgumentException>("name", () => headers.Remove(name));
241         }
242 
243         [Fact]
Remove_IllegalCharacter_Throws()244         public void Remove_IllegalCharacter_Throws()
245         {
246             WebHeaderCollection w = new WebHeaderCollection();
247             AssertExtensions.Throws<ArgumentException>("name", () => w.Remove("{"));
248         }
249 
250         [Fact]
Remove_EmptyCollection_Success()251         public void Remove_EmptyCollection_Success()
252         {
253             WebHeaderCollection w = new WebHeaderCollection();
254             w.Remove("foo");
255             Assert.Equal(0, w.Count);
256             Assert.Empty(w);
257             Assert.Empty(w.AllKeys);
258         }
259 
260         [Theory]
261         [InlineData("name", "name")]
262         [InlineData("name", "NaMe")]
Remove_SetThenRemove_Success(string setName, string removeName)263         public void Remove_SetThenRemove_Success(string setName, string removeName)
264         {
265             WebHeaderCollection w = new WebHeaderCollection();
266             w[setName] = "value";
267             w.Remove(removeName);
268             Assert.Equal(0, w.Count);
269             Assert.Empty(w);
270             Assert.Empty(w.AllKeys);
271         }
272 
273         [Theory]
274         [InlineData("name", "name")]
275         [InlineData("name", "NaMe")]
Remove_SetTwoThenRemoveOne_Success(string setName, string removeName)276         public void Remove_SetTwoThenRemoveOne_Success(string setName, string removeName)
277         {
278             WebHeaderCollection w = new WebHeaderCollection();
279             w[setName] = "value";
280             w["foo"] = "bar";
281             w.Remove(removeName);
282             Assert.Equal(1, w.Count);
283             Assert.NotEmpty(w);
284             Assert.NotEmpty(w.AllKeys);
285             Assert.Equal(new[] { "foo" }, w.AllKeys);
286             Assert.Equal("bar", w["foo"]);
287         }
288 
289         [Fact]
Getter_EmptyCollection_Success()290         public void Getter_EmptyCollection_Success()
291         {
292             WebHeaderCollection w = new WebHeaderCollection();
293             Assert.Null(w["name"]);
294             Assert.Equal(0, w.Count);
295             Assert.Empty(w);
296             Assert.Empty(w.AllKeys);
297         }
298 
299         [Fact]
Getter_NonEmptyCollectionNonExistentHeader_Success()300         public void Getter_NonEmptyCollectionNonExistentHeader_Success()
301         {
302             WebHeaderCollection w = new WebHeaderCollection();
303             w["name"] = "value";
304             Assert.Null(w["foo"]);
305             Assert.Equal(1, w.Count);
306             Assert.NotEmpty(w);
307             Assert.NotEmpty(w.AllKeys);
308             Assert.Equal(new[] { "name" }, w.AllKeys);
309             Assert.Equal("value", w["name"]);
310         }
311 
312         [Fact]
Getter_Success()313         public void Getter_Success()
314         {
315             string[] keys = { "Accept", "uPgRaDe", "Custom" };
316             string[] values = { "text/plain, text/html", " HTTP/2.0 , SHTTP/1.3,  , RTA/x11 ", "\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\"" };
317             WebHeaderCollection w = new WebHeaderCollection();
318 
319             for (int i = 0; i < keys.Length; ++i)
320             {
321                 string key = keys[i];
322                 string value = values[i];
323                 w[key] = value;
324             }
325 
326             for (int i = 0; i < keys.Length; ++i)
327             {
328                 string key = keys[i];
329                 string expected = values[i].Trim();
330                 Assert.Equal(expected, w[key]);
331                 Assert.Equal(expected, w[key.ToUpperInvariant()]);
332                 Assert.Equal(expected, w[key.ToLowerInvariant()]);
333             }
334         }
335 
336         [Fact]
ToString_Empty_Success()337         public void ToString_Empty_Success()
338         {
339             WebHeaderCollection w = new WebHeaderCollection();
340             Assert.Equal("\r\n", w.ToString());
341         }
342 
343         [Theory]
344         [InlineData(null)]
345         [InlineData("")]
ToString_SingleHeaderWithEmptyValue_Success(string value)346         public void ToString_SingleHeaderWithEmptyValue_Success(string value)
347         {
348             WebHeaderCollection w = new WebHeaderCollection();
349             w["name"] = value;
350             Assert.Equal("name: \r\n\r\n", w.ToString());
351         }
352 
353         [Fact]
ToString_NotEmpty_Success()354         public void ToString_NotEmpty_Success()
355         {
356             WebHeaderCollection w = new WebHeaderCollection();
357             w["Accept"] = "text/plain";
358             w["Content-Length"] = "123";
359             Assert.Equal(
360                 "Accept: text/plain\r\nContent-Length: 123\r\n\r\n",
361                 w.ToString());
362         }
363 
364         [Fact]
IterateCollection_Success()365         public void IterateCollection_Success()
366         {
367             WebHeaderCollection w = new WebHeaderCollection();
368             w["Accept"] = "text/plain";
369             w["Content-Length"] = "123";
370 
371             string result = "";
372             foreach (var item in w)
373             {
374                 result += item;
375             }
376 
377             Assert.Equal("AcceptContent-Length", result);
378         }
379 
380         [Fact]
Enumerator_Success()381         public void Enumerator_Success()
382         {
383             string item1 = "Accept";
384             string item2 = "Content-Length";
385             string item3 = "Name";
386 
387             WebHeaderCollection w = new WebHeaderCollection();
388             w[item1] = "text/plain";
389             w[item2] = "123";
390             w[item3] = "value";
391 
392             IEnumerable collection = w;
393             IEnumerator e = collection.GetEnumerator();
394 
395             for (int i = 0; i < 2; i++)
396             {
397                 // Not started
398                 Assert.Throws<InvalidOperationException>(() => e.Current);
399 
400                 Assert.True(e.MoveNext());
401                 Assert.Same(item1, e.Current);
402 
403                 Assert.True(e.MoveNext());
404                 Assert.Same(item2, e.Current);
405 
406                 Assert.True(e.MoveNext());
407                 Assert.Same(item3, e.Current);
408 
409                 Assert.False(e.MoveNext());
410                 Assert.False(e.MoveNext());
411                 Assert.False(e.MoveNext());
412 
413                 // Ended
414                 Assert.Throws<InvalidOperationException>(() => e.Current);
415 
416                 e.Reset();
417             }
418         }
419 
SerializeDeserialize_Roundtrip_MemberData()420         public static IEnumerable<object[]> SerializeDeserialize_Roundtrip_MemberData()
421         {
422             for (int i = 0; i < 10; i++)
423             {
424                 var wc = new WebHeaderCollection();
425                 for (int j = 0; j < i; j++)
426                 {
427                     wc[$"header{j}"] = $"value{j}";
428                 }
429                 yield return new object[] { wc };
430             }
431         }
432 
Add_Value_TestData()433         public static IEnumerable<object[]> Add_Value_TestData()
434         {
435             yield return new object[] { null, string.Empty };
436             yield return new object[] { string.Empty, string.Empty };
437             yield return new object[] { "VaLue", "VaLue" };
438             yield return new object[] { "  value  ", "value" };
439 
440             // Documentation says this should fail but it does not.
441             string longString = new string('a', 65536);
442             yield return new object[] { longString, longString };
443         }
444 
445         [Theory]
446         [MemberData(nameof(Add_Value_TestData))]
Add_ValidValue_Success(string value, string expectedValue)447         public void Add_ValidValue_Success(string value, string expectedValue)
448         {
449             var headers = new WebHeaderCollection
450             {
451                 { "name", value }
452             };
453 
454             Assert.Equal(expectedValue, headers["name"]);
455         }
456 
457         [Fact]
Add_HeaderAlreadyExists_AppendsValue()458         public void Add_HeaderAlreadyExists_AppendsValue()
459         {
460             var headers = new WebHeaderCollection
461             {
462                 { "name", "value1" },
463                 { "name", null },
464                 { "name", "value2" },
465                 { "NAME", "value3" },
466                 { "name", "" }
467             };
468             Assert.Equal("value1,,value2,value3,", headers["name"]);
469         }
470 
471         [Fact]
Add_NullName_ThrowsArgumentNullException()472         public void Add_NullName_ThrowsArgumentNullException()
473         {
474             var headers = new WebHeaderCollection();
475             AssertExtensions.Throws<ArgumentNullException>("name", () => headers.Add(null, "value"));
476         }
477 
478         [Theory]
479         [InlineData("")]
480         [InlineData("(")]
481         [InlineData("\r \t \n")]
482         [InlineData("  name  ")]
483         [MemberData(nameof(InvalidValues))]
Add_InvalidName_ThrowsArgumentException(string name)484         public void Add_InvalidName_ThrowsArgumentException(string name)
485         {
486             var headers = new WebHeaderCollection();
487             AssertExtensions.Throws<ArgumentException>("name", () => headers.Add(name, "value"));
488         }
489 
490         [Theory]
491         [MemberData(nameof(InvalidValues))]
Add_InvalidValue_ThrowsArgumentException(string value)492         public void Add_InvalidValue_ThrowsArgumentException(string value)
493         {
494             var headers = new WebHeaderCollection();
495             AssertExtensions.Throws<ArgumentException>("value", () => headers.Add("name", value));
496         }
497 
498         [Fact]
Add_ValidHeader_AddsToHeaders()499         public void Add_ValidHeader_AddsToHeaders()
500         {
501             var headers = new WebHeaderCollection()
502             {
503                 "name:value1",
504                 "name:",
505                 "NaMe:value2",
506                 "name:  ",
507             };
508             Assert.Equal("value1,,value2,", headers["name"]);
509         }
510 
511         [Theory]
512         [InlineData(null)]
513         [InlineData("")]
Add_NullHeader_ThrowsArgumentNullException(string header)514         public void Add_NullHeader_ThrowsArgumentNullException(string header)
515         {
516             var headers = new WebHeaderCollection();
517             AssertExtensions.Throws<ArgumentNullException>("header", () => headers.Add(header));
518         }
519 
520         [Theory]
521         [InlineData(" \r \t \n", "header")]
522         [InlineData("nocolon", "header")]
523         [InlineData("  :value", "name")]
524         [InlineData("name  :value", "name")]
525         [InlineData("name:va\rlue", "value")]
Add_InvalidHeader_ThrowsArgumentException(string header, string paramName)526         public void Add_InvalidHeader_ThrowsArgumentException(string header, string paramName)
527         {
528             var headers = new WebHeaderCollection();
529             AssertExtensions.Throws<ArgumentException>(paramName, () => headers.Add(header));
530         }
531 
532         private const string HeaderType = "Set-Cookie";
533         private const string Cookie1 = "locale=en; path=/; expires=Fri, 05 Oct 2018 06:28:57 -0000";
534         private const string Cookie2 = "uuid=123abc; path=/; expires=Fri, 05 Oct 2018 06:28:57 -0000; secure; HttpOnly";
535         private const string Cookie3 = "country=US; path=/; expires=Fri, 05 Oct 2018 06:28:57 -0000";
536         private const string Cookie4 = "m_session=session1; path=/; expires=Sun, 08 Oct 2017 00:28:57 -0000; secure; HttpOnly";
537 
538         private const string Cookie1NoAttribute = "locale=en";
539         private const string Cookie2NoAttribute = "uuid=123abc";
540         private const string Cookie3NoAttribute = "country=US";
541         private const string Cookie4NoAttribute = "m_session=session1";
542 
543         private const string CookieInvalid = "helloWorld";
544 
545         [Fact]
546         [SkipOnTargetFramework(TargetFrameworkMonikers.Mono, "Does not work in Mono")]
547         [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Requires fix shipping in .NET 4.7.2")]
GetValues_MultipleSetCookieHeadersWithExpiresAttribute_Success()548         public void GetValues_MultipleSetCookieHeadersWithExpiresAttribute_Success()
549         {
550             WebHeaderCollection w = new WebHeaderCollection();
551             w.Add(HeaderType, Cookie1);
552             w.Add(HeaderType, Cookie2);
553             w.Add(HeaderType, Cookie3);
554             w.Add(HeaderType, Cookie4);
555 
556             string[] values = w.GetValues(HeaderType);
557             Assert.Equal(4, values.Length);
558             Assert.Equal(Cookie1, values[0]);
559             Assert.Equal(Cookie2, values[1]);
560             Assert.Equal(Cookie3, values[2]);
561             Assert.Equal(Cookie4, values[3]);
562         }
563 
564         [Fact]
565         [SkipOnTargetFramework(TargetFrameworkMonikers.Mono, "Does not work in Mono")]
566         [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Requires fix shipping in .NET 4.7.2")]
GetValues_SingleSetCookieHeaderWithMultipleCookiesWithExpiresAttribute_Success()567         public void GetValues_SingleSetCookieHeaderWithMultipleCookiesWithExpiresAttribute_Success()
568         {
569             WebHeaderCollection w = new WebHeaderCollection();
570             w.Add(HeaderType, Cookie1 + "," + Cookie2 + "," + Cookie3 + "," + Cookie4);
571 
572             string[] values = w.GetValues(HeaderType);
573             Assert.Equal(4, values.Length);
574             Assert.Equal(Cookie1, values[0]);
575             Assert.Equal(Cookie2, values[1]);
576             Assert.Equal(Cookie3, values[2]);
577             Assert.Equal(Cookie4, values[3]);
578         }
579 
580         [Fact]
581         [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Requires fix shipping in .NET 4.7.2")]
GetValues_MultipleSetCookieHeadersWithNoAttribute_Success()582         public void GetValues_MultipleSetCookieHeadersWithNoAttribute_Success()
583         {
584             WebHeaderCollection w = new WebHeaderCollection();
585             w.Add(HeaderType, Cookie1NoAttribute);
586             w.Add(HeaderType, Cookie2NoAttribute);
587             w.Add(HeaderType, Cookie3NoAttribute);
588             w.Add(HeaderType, Cookie4NoAttribute);
589 
590             string[] values = w.GetValues(HeaderType);
591             Assert.Equal(4, values.Length);
592             Assert.Equal(Cookie1NoAttribute, values[0]);
593             Assert.Equal(Cookie2NoAttribute, values[1]);
594             Assert.Equal(Cookie3NoAttribute, values[2]);
595             Assert.Equal(Cookie4NoAttribute, values[3]);
596         }
597 
598         [Fact]
599         [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Requires fix shipping in .NET 4.7.2")]
GetValues_SingleSetCookieHeaderWithMultipleCookiesWithNoAttribute_Success()600         public void GetValues_SingleSetCookieHeaderWithMultipleCookiesWithNoAttribute_Success()
601         {
602             WebHeaderCollection w = new WebHeaderCollection();
603             w.Add(HeaderType, Cookie1NoAttribute + "," + Cookie2NoAttribute + "," + Cookie3NoAttribute + "," + Cookie4NoAttribute);
604 
605             string[] values = w.GetValues(HeaderType);
606             Assert.Equal(4, values.Length);
607             Assert.Equal(Cookie1NoAttribute, values[0]);
608             Assert.Equal(Cookie2NoAttribute, values[1]);
609             Assert.Equal(Cookie3NoAttribute, values[2]);
610             Assert.Equal(Cookie4NoAttribute, values[3]);
611         }
612 
613         [Fact]
614         [SkipOnTargetFramework(TargetFrameworkMonikers.Mono, "Does not work in Mono")]
615         [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Requires fix shipping in .NET 4.7.2")]
GetValues_InvalidSetCookieHeader_Success()616         public void GetValues_InvalidSetCookieHeader_Success()
617         {
618             WebHeaderCollection w = new WebHeaderCollection();
619             w.Add(HeaderType, CookieInvalid);
620 
621             string[] values = w.GetValues(HeaderType);
622             Assert.Equal(0, values.Length);
623         }
624 
625         [Fact]
GetValues_MultipleValuesHeader_Success()626         public void GetValues_MultipleValuesHeader_Success()
627         {
628             WebHeaderCollection w = new WebHeaderCollection();
629             string headerType = "Accept";
630             w.Add(headerType, "text/plain, text/html");
631             string[] values = w.GetValues(headerType);
632             Assert.Equal(2, values.Length);
633             Assert.Equal("text/plain", values[0]);
634             Assert.Equal("text/html", values[1]);
635         }
636 
637         [Fact]
HttpRequestHeader_Add_Rmemove_Success()638         public void HttpRequestHeader_Add_Rmemove_Success()
639         {
640             WebHeaderCollection w = new WebHeaderCollection();
641             w.Add(HttpRequestHeader.Warning, "Warning1");
642 
643             Assert.Equal(1, w.Count);
644             Assert.Equal("Warning1", w[HttpRequestHeader.Warning]);
645             Assert.Equal("Warning", w.AllKeys[0]);
646 
647             w.Remove(HttpRequestHeader.Warning);
648             Assert.Equal(0, w.Count);
649         }
650 
651         [Fact]
HttpRequestHeader_Get_Success()652         public void HttpRequestHeader_Get_Success()
653         {
654             WebHeaderCollection w = new WebHeaderCollection();
655             w.Add("header1", "value1");
656             w.Add("header1", "value2");
657             string[] values = w.GetValues(0);
658             Assert.Equal("value1", values[0]);
659             Assert.Equal("value2", values[1]);
660         }
661 
662         [Fact]
HttpRequestHeader_ToByteArray_Success()663         public void HttpRequestHeader_ToByteArray_Success()
664         {
665             WebHeaderCollection w = new WebHeaderCollection();
666             w.Add("header1", "value1");
667             w.Add("header1", "value2");
668             byte[] byteArr = w.ToByteArray();
669             Assert.NotEmpty(byteArr);
670         }
671 
672         [Fact]
HttpRequestHeader_GetKey_Success()673         public void HttpRequestHeader_GetKey_Success()
674         {
675             WebHeaderCollection w = new WebHeaderCollection();
676             w.Add("header1", "value1");
677             w.Add("header1", "value2");
678             Assert.NotEmpty(w.GetKey(0));
679         }
680 
681         [Fact]
HttpRequestHeader_GetValues_Success()682         public void HttpRequestHeader_GetValues_Success()
683         {
684             WebHeaderCollection w = new WebHeaderCollection();
685             w.Add("header1", "value1");
686             Assert.Equal("value1", w.GetValues("header1")[0]);
687         }
688 
689         [Fact]
HttpRequestHeader_Clear_Success()690         public void HttpRequestHeader_Clear_Success()
691         {
692             WebHeaderCollection w = new WebHeaderCollection();
693             w.Add("header1", "value1");
694             w.Add("header1", "value2");
695             w.Clear();
696             Assert.Equal(0, w.Count);
697         }
698 
699         [Fact]
HttpRequestHeader_IsRestricted_Success()700         public void HttpRequestHeader_IsRestricted_Success()
701         {
702             Assert.True(WebHeaderCollection.IsRestricted("Accept"));
703             Assert.False(WebHeaderCollection.IsRestricted("Age"));
704             Assert.False(WebHeaderCollection.IsRestricted("Accept", true));
705         }
706 
707         [Fact]
HttpRequestHeader_AddHeader_Success()708         public void HttpRequestHeader_AddHeader_Success()
709         {
710             WebHeaderCollection w = new WebHeaderCollection();
711             w.Add(HttpRequestHeader.ContentLength, "10");
712             w.Add(HttpRequestHeader.ContentType, "text/html");
713             Assert.Equal(2,w.Count);
714         }
715 
716         [Fact]
WebHeaderCollection_Keys_Success()717         public void WebHeaderCollection_Keys_Success()
718         {
719             WebHeaderCollection w = new WebHeaderCollection();
720             w.Add(HttpRequestHeader.ContentLength, "10");
721             w.Add(HttpRequestHeader.ContentType, "text/html");
722             Assert.Equal(2, w.Keys.Count);
723         }
724 
725         [Fact]
HttpRequestHeader_AddHeader_Failure()726         public void HttpRequestHeader_AddHeader_Failure()
727         {
728             WebHeaderCollection w = new WebHeaderCollection();
729             char[] arr = new char[ushort.MaxValue + 1];
730             string maxStr = new string(arr);
731             AssertExtensions.Throws<ArgumentException>("value", () => w.Add(HttpRequestHeader.ContentLength,maxStr));
732             AssertExtensions.Throws<ArgumentException>("value", () => w.Add("ContentLength", maxStr));
733         }
734 
735         [Fact]
HttpResponseHeader_Set_Success()736         public void HttpResponseHeader_Set_Success()
737         {
738             WebHeaderCollection w = new WebHeaderCollection();
739             w.Set(HttpResponseHeader.ProxyAuthenticate, "value123");
740 
741             Assert.Equal("value123", w[HttpResponseHeader.ProxyAuthenticate]);
742         }
743 
744         [Fact]
HttpRequestHeader_Set_Success()745         public void HttpRequestHeader_Set_Success()
746         {
747             WebHeaderCollection w = new WebHeaderCollection();
748             w.Set(HttpRequestHeader.Connection, "keep-alive");
749 
750             Assert.Equal(1, w.Count);
751             Assert.Equal("keep-alive", w[HttpRequestHeader.Connection]);
752             Assert.Equal("Connection", w.AllKeys[0]);
753         }
754 
755         [Fact]
NameValue_Set_Success()756         public void NameValue_Set_Success()
757         {
758             WebHeaderCollection w = new WebHeaderCollection();
759             w.Set("firstName", "first");
760             Assert.Equal(1, w.Count);
761             Assert.NotEmpty(w);
762             Assert.NotEmpty(w.AllKeys);
763             Assert.Equal(new[] { "firstName" }, w.AllKeys);
764             Assert.Equal("first", w["firstName"]);
765         }
766     }
767 }
768