Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Tests/HttpUnitTests/HttpUnitTests.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Compile Include="MockContent.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UriUnitTests.cs" />
<Compile Include="WebHeaderCollectionTests.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.lock.json" />
Expand Down
101 changes: 101 additions & 0 deletions Tests/HttpUnitTests/WebHeaderCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

using nanoFramework.TestFramework;
using System;
using System.Net;

namespace HttpUnitTests
{
[TestClass]
public class WebHeaderCollectionTests
{
[TestMethod]
public void Add_Authorization_BearerWithSpaceAndNoValue()
{
var headers = new WebHeaderCollection();
headers.Add("Authorization: Bearer ");
}

[TestMethod]
public void Add_Authorization_NoSpaceSingleChar()
{
var headers = new WebHeaderCollection();
headers.Add("Authorization: 1");
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

[TestMethod]
public void Add_Authorization_ValidBearer()
{
var headers = new WebHeaderCollection();
headers.Add("Authorization: Bearer a11111");
string value = headers["Authorization"];
Assert.AreEqual("Bearer a11111", value);
}

[TestMethod]
public void Add_Authorization_ValidTestValue()
{
var headers = new WebHeaderCollection();
headers.Add("Authorization: test 1");
string value = headers["Authorization"];
Assert.AreEqual("test 1", value);
}

[TestMethod]
public void Add_Authorization_ValidSingleLetterPair()
{
var headers = new WebHeaderCollection();
headers.Add("Authorization: a b");
string value = headers["Authorization"];
Assert.AreEqual("a b", value);
}
[TestMethod]
public void Add_Authorization_EmptyValue()
{
var headers = new WebHeaderCollection();
headers.Add("Authorization:");
string value = headers["Authorization"];
Assert.AreEqual(string.Empty, value);
}

[TestMethod]
public void Add_Authorization_ColonWithSpaceOnly()
{
var headers = new WebHeaderCollection();
headers.Add("Authorization: ");
string value = headers["Authorization"];
Assert.AreEqual(string.Empty, value);
}

[TestMethod]
public void Add_NullHeader_ThrowsArgumentNullException()
{
var headers = new WebHeaderCollection();
Assert.ThrowsException(typeof(ArgumentNullException), () => headers.Add(null));
}

[TestMethod]
public void Add_EmptyHeader_ThrowsArgumentNullException()
{
var headers = new WebHeaderCollection();
Assert.ThrowsException(typeof(ArgumentNullException), () => headers.Add(string.Empty));
}

[TestMethod]
public void Add_HeaderWithNoColon_ThrowsArgumentException()
{
var headers = new WebHeaderCollection();
Assert.ThrowsException(typeof(ArgumentException), () => headers.Add("Authorization"));
}

[TestMethod]
public void Add_HeaderNameWithSpace_ThrowsArgumentException()
{
var headers = new WebHeaderCollection();
Assert.ThrowsException(typeof(ArgumentException), () => headers.Add("My Header: value"));
}
Comment thread
josesimoes marked this conversation as resolved.
}
}
11 changes: 10 additions & 1 deletion nanoFramework.System.Net.Http/Http/System.Net.WebHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,16 @@ public void Add(string header)
}

string name = header.Substring(0, colpos);
string value = header.Substring(colpos + 1);
// Handle empty header value
string value;
if (colpos + 1 >= header.Length)
{
value = string.Empty;
}
else
{
value = header.Substring(colpos + 1);
}
Comment thread
benyuz marked this conversation as resolved.
Comment thread
benyuz marked this conversation as resolved.

name = CheckBadChars(name, false);
ThrowOnRestrictedHeader(name);
Expand Down