Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/6.0] Fix method HttpHeadersNonValidated uses to get HeaderDescriptor #62194

Merged
merged 1 commit into from
Dec 15, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace System.Net.Http.Headers
/// <returns>true if the collection contains the header; otherwise, false.</returns>
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 _);

/// <summary>Gets the values for the specified header name.</summary>
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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<string> 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()
{
Expand Down