private async Task TestBadRequest(string request, string expectedResponseStatusCode, string expectedExceptionMessage, string expectedAllowHeader = null) { BadHttpRequestException loggedException = null; var mockKestrelTrace = new Mock <IKestrelTrace>(); mockKestrelTrace .Setup(trace => trace.IsEnabled(LogLevel.Information)) .Returns(true); mockKestrelTrace .Setup(trace => trace.ConnectionBadRequest(It.IsAny <string>(), It.IsAny <BadHttpRequestException>())) .Callback <string, BadHttpRequestException>((connectionId, exception) => loggedException = exception); await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory, mockKestrelTrace.Object))) { using (var connection = server.CreateConnection()) { await connection.SendAll(request); await ReceiveBadRequestResponse(connection, expectedResponseStatusCode, server.Context.DateHeaderValue, expectedAllowHeader); } } mockKestrelTrace.Verify(trace => trace.ConnectionBadRequest(It.IsAny <string>(), It.IsAny <BadHttpRequestException>())); Assert.Equal(expectedExceptionMessage, loggedException.Message); }
public async Task RequestBodyTooLargeContentLengthExceedsGlobalLimit() { var globalMaxRequestBodySize = 0x100000000; BadHttpRequestException exception = null; using (var testServer = await TestServer.Create( async ctx => { try { await ctx.Request.Body.ReadAsync(new byte[2000]); } catch (BadHttpRequestException ex) { exception = ex; throw ex; } }, LoggerFactory)) { using (var connection = testServer.CreateConnection()) { await connection.Send( "POST / HTTP/1.1", $"Content-Length: {globalMaxRequestBodySize + 1}", "Host: localhost", "", ""); await connection.Receive("HTTP/1.1 413 Payload Too Large"); } } Assert.Equal(CoreStrings.BadRequest_RequestBodyTooLarge, exception.Message); }
public async Task RequestBodyTooLargeChunked() { var maxRequestSize = 0x1000; BadHttpRequestException exception = null; using (var testServer = await TestServer.Create( async ctx => { try { while (true) { var num = await ctx.Request.Body.ReadAsync(new byte[2000]); } } catch (BadHttpRequestException ex) { exception = ex; throw ex; } }, LoggerFactory, new IISServerOptions { MaxRequestBodySize = maxRequestSize })) { using (var connection = testServer.CreateConnection()) { await connection.Send( "POST / HTTP/1.1", "Transfer-Encoding: chunked", "Host: localhost", "", "1001", new string('a', 4097), "0", ""); await connection.Receive("HTTP/1.1 413 Payload Too Large"); } } Assert.NotNull(exception); Assert.Equal(CoreStrings.BadRequest_RequestBodyTooLarge, exception.Message); }
public async Task RequestBodyTooLargeContentLengthExceedingPerRequestLimit() { var maxRequestSize = 0x10000; var perRequestMaxRequestBodySize = 0x100; BadHttpRequestException exception = null; using (var testServer = await TestServer.Create( async ctx => { try { var feature = ctx.Features.Get <IHttpMaxRequestBodySizeFeature>(); Assert.Equal(maxRequestSize, feature.MaxRequestBodySize); feature.MaxRequestBodySize = perRequestMaxRequestBodySize; await ctx.Request.Body.ReadAsync(new byte[2000]); } catch (BadHttpRequestException ex) { exception = ex; throw ex; } }, LoggerFactory, new IISServerOptions { MaxRequestBodySize = maxRequestSize })) { using (var connection = testServer.CreateConnection()) { await connection.Send( "POST / HTTP/1.1", $"Content-Length: {perRequestMaxRequestBodySize + 1}", "Host: localhost", "", ""); await connection.Receive("HTTP/1.1 413 Payload Too Large"); } } Assert.Equal(CoreStrings.BadRequest_RequestBodyTooLarge, exception.Message); }
public async Task EveryReadFailsWhenContentLengthHeaderExceedsGlobalLimit() { BadHttpRequestException requestRejectedEx1 = null; BadHttpRequestException requestRejectedEx2 = null; using (var testServer = await TestServer.Create( async ctx => { var buffer = new byte[1]; requestRejectedEx1 = await Assert.ThrowsAnyAsync <BadHttpRequestException>( async() => await ctx.Request.Body.ReadAsync(buffer, 0, 1)); requestRejectedEx2 = await Assert.ThrowsAnyAsync <BadHttpRequestException>( async() => await ctx.Request.Body.ReadAsync(buffer, 0, 1)); throw requestRejectedEx2; }, LoggerFactory, new IISServerOptions { MaxRequestBodySize = 0 })) { using (var connection = testServer.CreateConnection()) { await connection.Send( "POST / HTTP/1.1", "Host: localhost", "Content-Length: " + (new IISServerOptions().MaxRequestBodySize + 1), "", ""); await connection.Receive( "HTTP/1.1 413 Payload Too Large"); } } Assert.NotNull(requestRejectedEx1); Assert.NotNull(requestRejectedEx2); Assert.Equal(CoreStrings.BadRequest_RequestBodyTooLarge, requestRejectedEx1.Message); Assert.Equal(CoreStrings.BadRequest_RequestBodyTooLarge, requestRejectedEx2.Message); }
public void ConnectionBadRequest(string connectionId, Microsoft.AspNetCore.Http.BadHttpRequestException ex) { }
public void ConnectionBadRequest(string connectionId, Microsoft.AspNetCore.Http.BadHttpRequestException ex) { _trace1.ConnectionBadRequest(connectionId, ex); _trace2.ConnectionBadRequest(connectionId, ex); }
public virtual void ConnectionBadRequest(string connectionId, Microsoft.AspNetCore.Http.BadHttpRequestException ex) { _connectionBadRequest(_logger, connectionId, ex.Message, ex); }
private static partial void ConnectionBadRequest(ILogger logger, string connectionId, string message, Microsoft.AspNetCore.Http.BadHttpRequestException ex);
public static void ConnectionBadRequest(ILogger logger, string connectionId, Microsoft.AspNetCore.Http.BadHttpRequestException ex) { _connectionBadRequest(logger, connectionId, ex.Message, ex); }