@@ -404,8 +404,107 @@ public static IEnumerable<object[]> SerializeDeserialize_Roundtrip_MemberData()
404404 }
405405 }
406406
407+ public static IEnumerable < object [ ] > Add_Value_TestData ( )
408+ {
409+ yield return new object [ ] { null , string . Empty } ;
410+ yield return new object [ ] { string . Empty , string . Empty } ;
411+ yield return new object [ ] { "VaLue" , "VaLue" } ;
412+ yield return new object [ ] { " value " , "value" } ;
413+
414+ // Documentation says this should fail but it does not.
415+ string longString = new string ( 'a' , 65536 ) ;
416+ yield return new object [ ] { longString , longString } ;
417+ }
418+
419+ [ Theory ]
420+ [ MemberData ( nameof ( Add_Value_TestData ) ) ]
421+ public void Add_ValidValue_Success ( string value , string expectedValue )
422+ {
423+ var headers = new WebHeaderCollection
424+ {
425+ { "name" , value }
426+ } ;
427+
428+ Assert . Equal ( expectedValue , headers [ "name" ] ) ;
429+ }
430+
431+ [ Fact ]
432+ public void Add_HeaderAlreadyExists_AppendsValue ( )
433+ {
434+ var headers = new WebHeaderCollection
435+ {
436+ { "name" , "value1" } ,
437+ { "name" , null } ,
438+ { "name" , "value2" } ,
439+ { "NAME" , "value3" } ,
440+ { "name" , "" }
441+ } ;
442+ Assert . Equal ( "value1,,value2,value3," , headers [ "name" ] ) ;
443+ }
444+
445+ [ Fact ]
446+ public void Add_NullName_ThrowsArgumentNullException ( )
447+ {
448+ var headers = new WebHeaderCollection ( ) ;
449+ Assert . Throws < ArgumentNullException > ( "name" , ( ) => headers . Add ( null , "value" ) ) ;
450+ }
451+
452+ [ Theory ]
453+ [ InlineData ( "" ) ]
454+ [ InlineData ( "(" ) ]
455+ [ InlineData ( "\r \t \n " ) ]
456+ [ InlineData ( " name " ) ]
457+ [ MemberData ( nameof ( InvalidValues ) ) ]
458+ public void Add_InvalidName_ThrowsArgumentException ( string name )
459+ {
460+ var headers = new WebHeaderCollection ( ) ;
461+ Assert . Throws < ArgumentException > ( "name" , ( ) => headers . Add ( name , "value" ) ) ;
462+ }
463+
464+ [ Theory ]
465+ [ MemberData ( nameof ( InvalidValues ) ) ]
466+ public void Add_InvalidValue_ThrowsArgumentException ( string value )
467+ {
468+ var headers = new WebHeaderCollection ( ) ;
469+ Assert . Throws < ArgumentException > ( "value" , ( ) => headers . Add ( "name" , value ) ) ;
470+ }
471+
472+ [ Fact ]
473+ public void Add_ValidHeader_AddsToHeaders ( )
474+ {
475+ var headers = new WebHeaderCollection ( )
476+ {
477+ "name:value1" ,
478+ "name:" ,
479+ "NaMe:value2" ,
480+ "name: " ,
481+ } ;
482+ Assert . Equal ( "value1,,value2," , headers [ "name" ] ) ;
483+ }
484+
485+ [ Theory ]
486+ [ InlineData ( null ) ]
487+ [ InlineData ( "" ) ]
488+ [ InlineData ( " \r \t \n " ) ]
489+ public void Add_NullHeader_ThrowsArgumentNullException ( string header )
490+ {
491+ var headers = new WebHeaderCollection ( ) ;
492+ Assert . Throws < ArgumentNullException > ( "header" , ( ) => headers . Add ( header ) ) ;
493+ }
494+
495+ [ Theory ]
496+ [ InlineData ( "nocolon" , "header" ) ]
497+ [ InlineData ( " :value" , "name" ) ]
498+ [ InlineData ( "name :value" , "name" ) ]
499+ [ InlineData ( "name:va\r lue" , "value" ) ]
500+ public void Add_InvalidHeader_ThrowsArgumentException ( string header , string paramName )
501+ {
502+ var headers = new WebHeaderCollection ( ) ;
503+ Assert . Throws < ArgumentException > ( paramName , ( ) => headers . Add ( header ) ) ;
504+ }
505+
407506 [ Fact ]
408- public void HttpRequestHeader_Add_Remove_Success ( )
507+ public void HttpRequestHeader_Add_Rmemove_Success ( )
409508 {
410509 WebHeaderCollection w = new WebHeaderCollection ( ) ;
411510 w . Add ( HttpRequestHeader . Warning , "Warning1" ) ;
0 commit comments