Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
55 changes: 55 additions & 0 deletions Tests/HttpUnitTests/WebHeaderCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

using nanoFramework.TestFramework;
using System.Net;

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

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

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

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

[TestMethod]
public void Add_Authorization_ValidSingleLetterPair_ShouldSucceed()
{
var headers = new WebHeaderCollection();
headers.Add("Authorization: a b");
string value = headers["Authorization"];
Assert.AreEqual("a b", value);
}
}
}
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