Skip to content

Commit fca7c73

Browse files
z77mapranavkm
andauthored
Fix: HttpMethodMatcherPolicy.ApplyAsync ArgumentNullException (#39759)
* Fix NullArgumentException while creating rejection endpoint * Update HttpMethodMatcherPolicy.cs * Apply suggestions from code review * Update src/Http/Routing/test/UnitTests/Matching/HttpMethodMatcherPolicyTest.cs Co-authored-by: Pranav K <[email protected]>
1 parent df923d8 commit fca7c73

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/Http/Routing/src/Matching/HttpMethodMatcherPolicy.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public Task ApplyAsync(HttpContext httpContext, CandidateSet candidates)
162162
if (needs405Endpoint == true)
163163
{
164164
// We saw some endpoints coming in, and we eliminated them all.
165-
httpContext.SetEndpoint(CreateRejectionEndpoint(methods!.OrderBy(m => m, StringComparer.OrdinalIgnoreCase)));
165+
httpContext.SetEndpoint(CreateRejectionEndpoint(methods?.OrderBy(m => m, StringComparer.OrdinalIgnoreCase)));
166166
httpContext.Request.RouteValues = null!;
167167
}
168168

@@ -398,9 +398,9 @@ public PolicyJumpTable BuildJumpTable(int exitDestination, IReadOnlyList<PolicyJ
398398
}
399399
}
400400

401-
private static Endpoint CreateRejectionEndpoint(IEnumerable<string> httpMethods)
401+
private static Endpoint CreateRejectionEndpoint(IEnumerable<string>? httpMethods)
402402
{
403-
var allow = string.Join(", ", httpMethods);
403+
var allow = httpMethods is null ? string.Empty : string.Join(", ", httpMethods);
404404
return new Endpoint(
405405
(context) =>
406406
{

src/Http/Routing/test/UnitTests/Matching/HttpMethodMatcherPolicyTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,28 @@ public void IEndpointSelectorPolicy_AppliesToNode_EndpointIsNotDynamic_ReturnsFa
151151
Assert.False(result);
152152
}
153153

154+
[Theory]
155+
[InlineData(1)]
156+
[InlineData(2)]
157+
public async Task IEndpointSelectorPolicy_ApplyAsync_ProcessesInvalidCandidate(int candidateNum)
158+
{
159+
var policy = (IEndpointSelectorPolicy)CreatePolicy();
160+
161+
var endpoints = new RouteEndpoint[candidateNum];
162+
for (int i = 0; i < candidateNum; i++)
163+
{
164+
endpoints[i] = CreateEndpoint("/", new HttpMethodMetadata(new[] { "DEL" }));
165+
}
166+
167+
var candidates = new CandidateSet(endpoints, new RouteValueDictionary[endpoints.Length], Enumerable.Repeat<int>(-1, candidateNum).ToArray());
168+
var httpContext = new DefaultHttpContext();
169+
170+
await policy.ApplyAsync(httpContext, candidates);
171+
172+
Assert.Equal(httpContext.GetEndpoint().Metadata, EndpointMetadataCollection.Empty);
173+
Assert.True(string.Equals(httpContext.GetEndpoint().DisplayName, Http405EndpointDisplayName, StringComparison.OrdinalIgnoreCase));
174+
}
175+
154176
[Fact]
155177
public void GetEdges_GroupsByHttpMethod()
156178
{

0 commit comments

Comments
 (0)