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

Fix two segment paths returning 405 status instead of 404 #701

Merged
merged 1 commit into from
Dec 27, 2019
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
16 changes: 11 additions & 5 deletions src/Grpc.AspNetCore.Server/Model/Internal/ServiceRouteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,20 @@ internal static void CreateUnimplementedEndpoints(

private static void CreateUnimplementedEndpoint(IEndpointRouteBuilder endpointRouteBuilder, string pattern, string displayName, RequestDelegate requestDelegate)
{
var routePattern = RoutePatternFactory.Parse(pattern, defaults: null, new { contentType = GrpcContentTypeConstraint.Instance });
var routePattern = RoutePatternFactory.Parse(pattern, defaults: null, new { contentType = GrpcUnimplementedConstraint.Instance });
var endpointBuilder = endpointRouteBuilder.Map(routePattern, requestDelegate);

endpointBuilder.Add(ep =>
{
ep.DisplayName = $"gRPC - {displayName}";
ep.Metadata.Add(new HttpMethodMetadata(new[] { "POST" }));
// Don't add POST metadata here. It will return 405 status for other HTTP methods which isn't
// what we want. That check is made in a constraint instead.
});
}

private class GrpcContentTypeConstraint : IRouteConstraint
private class GrpcUnimplementedConstraint : IRouteConstraint
{
public static readonly GrpcContentTypeConstraint Instance = new GrpcContentTypeConstraint();
public static readonly GrpcUnimplementedConstraint Instance = new GrpcUnimplementedConstraint();

public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
{
Expand All @@ -152,10 +153,15 @@ public bool Match(HttpContext httpContext, IRouter route, string routeKey, Route
return false;
}

if (!HttpMethods.IsPost(httpContext.Request.Method))
{
return false;
}

return GrpcProtocolHelpers.IsGrpcContentType(httpContext.Request.ContentType);
}

private GrpcContentTypeConstraint()
private GrpcUnimplementedConstraint()
{
}
}
Expand Down
9 changes: 6 additions & 3 deletions test/FunctionalTests/Server/UnimplementedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ public async Task HttpContextExtensionMethod_ReturnContextInTrailer()
response.AssertTrailerStatus(StatusCode.Unimplemented, "Service is unimplemented.");
}

[TestCase("application/grpc", HttpStatusCode.OK, StatusCode.Unimplemented)]
[TestCase("application/json", (HttpStatusCode)418, null)]
public async Task UnimplementedContentType_ReturnUnimplementedForAppGrpc(string contentType, HttpStatusCode httpStatusCode, StatusCode? grpcStatusCode)
[TestCase("application/grpc", "POST", HttpStatusCode.OK, StatusCode.Unimplemented)]
[TestCase("application/grpc", "GET", (HttpStatusCode)404, null)]
[TestCase("application/json", "POST", (HttpStatusCode)404, null)]
[TestCase("application/json", "GET", (HttpStatusCode)404, null)]
public async Task UnimplementedContentType_ReturnUnimplementedForAppGrpc(string contentType, string httpMethod, HttpStatusCode httpStatusCode, StatusCode? grpcStatusCode)
{
// Arrange
var requestMessage = new HelloRequest
Expand All @@ -96,6 +98,7 @@ public async Task UnimplementedContentType_ReturnUnimplementedForAppGrpc(string
MessageHelpers.WriteMessage(requestStream, requestMessage);

var httpRequest = GrpcHttpHelper.Create("HasMapped/Extra");
httpRequest.Method = new HttpMethod(httpMethod);
httpRequest.Content = new StreamContent(requestStream);
httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

Expand Down
6 changes: 0 additions & 6 deletions testassets/FunctionalTestsWebsite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ public void Configure(IApplicationBuilder app)

endpoints.DataSources.Add(endpoints.ServiceProvider.GetRequiredService<DynamicEndpointDataSource>());

endpoints.Map("{FirstSegment}/{SecondSegment}", context =>
{
context.Response.StatusCode = StatusCodes.Status418ImATeapot;
return Task.CompletedTask;
});

endpoints.MapGet("/generateJwtToken", context =>
{
return context.Response.WriteAsync(GenerateJwtToken());
Expand Down