[Mvc][Fixes #11783] Replace header.Add with header.TryAddWithoutValidation (#14339)

This commit is contained in:
Javier Calvarro Nelson 2019-09-25 17:07:12 +02:00 committed by GitHub
parent 0e8136179d
commit 6d50f25ac3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -92,8 +92,8 @@ namespace Microsoft.AspNetCore.Mvc.Testing.Handlers
{
foreach (var header in originalRequestContent.Headers)
{
contentCopy.Headers.Add(header.Key, header.Value);
newRequestContent.Headers.Add(header.Key, header.Value);
contentCopy.Headers.TryAddWithoutValidation(header.Key, header.Value);
newRequestContent.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
}
@ -103,7 +103,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing.Handlers
{
foreach (var header in originalRequestHeaders)
{
newRequestHeaders.Add(header.Key, header.Value);
newRequestHeaders.TryAddWithoutValidation(header.Key, header.Value);
}
}

View File

@ -75,6 +75,29 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
Assert.Equal(5, handlerResponse.Body);
}
[Fact]
public async Task TestingInfrastructure_RedirectHandlerWorksWithInvalidRequestAndContentHeaders()
{
// Act
var request = new HttpRequestMessage(HttpMethod.Post, "Testing/RedirectHandler/2")
{
Content = new ObjectContent<Number>(new Number { Value = 5 }, new JsonMediaTypeFormatter())
};
request.Headers.Add("X-Pass-Thru", "Some-Value");
Assert.True(request.Headers.TryAddWithoutValidation("X-Invalid-Request-Header", "Bearer 1234,5678"));
Assert.True(request.Content.Headers.TryAddWithoutValidation("X-Invalid-Content-Header", "Bearer 1234,5678"));
var response = await Client.SendAsync(request);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var xPassThruValue = Assert.Single(response.Headers.GetValues("X-Pass-Thru"));
Assert.Equal("Some-Value", xPassThruValue);
var handlerResponse = await response.Content.ReadAsAsync<RedirectHandlerResponse>();
Assert.Equal(5, handlerResponse.Url);
Assert.Equal(5, handlerResponse.Body);
}
[Fact]
public async Task TestingInfrastructure_RedirectHandlerUsesOriginalRequestHeaders()
{