Skip to content

Commit

Permalink
Preserve parsed & raw header value ordering (#67227)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan authored Mar 29, 2022
1 parent a7c5546 commit 69f60b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1162,8 +1162,8 @@ internal static void GetStoreValuesAsStringOrStringArray(HeaderDescriptor descri
}

int currentIndex = 0;
ReadStoreValues<string?>(values, info.RawValue, null, ref currentIndex);
ReadStoreValues<object?>(values, info.ParsedAndInvalidValues, descriptor.Parser, ref currentIndex);
ReadStoreValues<string?>(values, info.RawValue, null, ref currentIndex);

Debug.Assert(currentIndex == length);
}
Expand Down Expand Up @@ -1196,8 +1196,8 @@ internal static int GetStoreValuesIntoStringArray(HeaderDescriptor descriptor, o
}

int currentIndex = 0;
ReadStoreValues<string?>(values, info.RawValue, null, ref currentIndex);
ReadStoreValues<object?>(values, info.ParsedAndInvalidValues, descriptor.Parser, ref currentIndex);
ReadStoreValues<string?>(values, info.RawValue, null, ref currentIndex);
Debug.Assert(currentIndex == length);
}

Expand All @@ -1208,9 +1208,7 @@ private static int GetValueCount(HeaderStoreItemInfo info)
{
Debug.Assert(info != null);

int valueCount = Count<string>(info.RawValue);
valueCount += Count<object>(info.ParsedAndInvalidValues);
return valueCount;
return Count<object>(info.ParsedAndInvalidValues) + Count<string>(info.RawValue);

static int Count<T>(object? valueStore) =>
valueStore is null ? 0 :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ public void TryAddWithoutValidation_ValidAndInvalidValues_InsertionOrderIsPreser
Assert.Equal(new[] { "text/bar", "invalid", "text/baz" }, accept);
}

[Fact]
public void MixedAddAndTryAddWithoutValidation_ParsedAndRawValues_InsertionOrderIsPreserved()
{
HttpHeaders headers = new HttpRequestMessage().Headers;

headers.Add("Accept", "text/foo");
headers.TryAddWithoutValidation("Accept", "text/bar");
headers.TryAddWithoutValidation("Accept", "invalid");

var expectedValues = new string[] { "text/foo", "text/bar", "invalid" };

Assert.Equal(expectedValues, headers.NonValidated["Accept"]);

Assert.True(headers.TryGetValues("Accept", out IEnumerable<string>? values));
Assert.Equal(expectedValues, values);

Assert.Equal(expectedValues, headers.NonValidated["Accept"]);
}

[Theory]
[InlineData(null)]
[InlineData("")]
Expand Down Expand Up @@ -1431,7 +1450,7 @@ public void NonValidated_SetValidAndInvalidHeaderValues_AllHeaderValuesReturned(
headers.TryAddWithoutValidation(headers.Descriptor, "value2,value3");
headers.TryAddWithoutValidation(headers.Descriptor, invalidHeaderValue);

string expectedValue = "value2,value3---" + invalidHeaderValue + "---" + parsedPrefix + "1";
string expectedValue = $"{parsedPrefix}1---value2,value3---{invalidHeaderValue}";

Assert.Equal(1, headers.NonValidated.Count);

Expand Down

0 comments on commit 69f60b3

Please sign in to comment.