diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs
index c31e628159db0..ee53b9e626c75 100644
--- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs
+++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs
@@ -1051,7 +1051,7 @@ private HeaderDescriptor GetHeaderDescriptor(string name)
throw new InvalidOperationException(SR.Format(SR.net_http_headers_not_allowed_header_name, name));
}
- private bool TryGetHeaderDescriptor(string name, out HeaderDescriptor descriptor)
+ internal bool TryGetHeaderDescriptor(string name, out HeaderDescriptor descriptor)
{
if (string.IsNullOrEmpty(name))
{
diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeadersNonValidated.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeadersNonValidated.cs
index ad1a1850ea83c..5e67476d116a9 100644
--- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeadersNonValidated.cs
+++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeadersNonValidated.cs
@@ -32,7 +32,7 @@ namespace System.Net.Http.Headers
/// true if the collection contains the header; otherwise, false.
public bool Contains(string headerName) =>
_headers is HttpHeaders headers &&
- HeaderDescriptor.TryGet(headerName, out HeaderDescriptor descriptor) &&
+ headers.TryGetHeaderDescriptor(headerName, out HeaderDescriptor descriptor) &&
headers.TryGetHeaderValue(descriptor, out _);
/// Gets the values for the specified header name.
@@ -62,7 +62,7 @@ public HeaderStringValues this[string headerName]
public bool TryGetValues(string headerName, out HeaderStringValues values)
{
if (_headers is HttpHeaders headers &&
- HeaderDescriptor.TryGet(headerName, out HeaderDescriptor descriptor) &&
+ headers.TryGetHeaderDescriptor(headerName, out HeaderDescriptor descriptor) &&
headers.TryGetHeaderValue(descriptor, out object? info))
{
HttpHeaders.GetStoreValuesAsStringOrStringArray(descriptor, info, out string? singleValue, out string[]? multiValue);
diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs
index f56f85ac5bd0d..139422163a95b 100644
--- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs
+++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs
@@ -2112,6 +2112,44 @@ public void AddHeaders_SourceHasInvalidHeaderValues_InvalidHeadersRemovedFromSou
Assert.False(destination.Contains("custom"), "destination contains 'custom' header.");
}
+ [Fact]
+ public void AddHeaders_ResponseHeaderToRequestHeaders_Success()
+ {
+ const string Name = "WWW-Authenticate";
+ const string Value = "Basic realm=\"Access to the staging site\", charset=\"UTF-8\"";
+
+ var request = new HttpRequestMessage();
+ Assert.True(request.Headers.TryAddWithoutValidation(Name, Value));
+
+ Assert.True(request.Headers.Contains(Name));
+ Assert.True(request.Headers.NonValidated.Contains(Name));
+
+ Assert.True(request.Headers.TryGetValues(Name, out IEnumerable values));
+ Assert.Equal(Value, values.Single());
+
+ Assert.True(request.Headers.NonValidated.TryGetValues(Name, out HeaderStringValues nvValues));
+ Assert.Equal(Value, nvValues.Single());
+ }
+
+ [Fact]
+ public void AddHeaders_RequestHeaderToResponseHeaders_Success()
+ {
+ const string Name = "Referer";
+ const string Value = "https://dot.net";
+
+ var response = new HttpResponseMessage();
+ Assert.True(response.Headers.TryAddWithoutValidation(Name, Value));
+
+ Assert.True(response.Headers.Contains(Name));
+ Assert.True(response.Headers.NonValidated.Contains(Name));
+
+ Assert.True(response.Headers.TryGetValues(Name, out IEnumerable values));
+ Assert.Equal(Value, values.Single());
+
+ Assert.True(response.Headers.NonValidated.TryGetValues(Name, out HeaderStringValues nvValues));
+ Assert.Equal(Value, nvValues.Single());
+ }
+
[Fact]
public void HeaderStringValues_Default_Empty()
{