public async Task RequestBody_ReadAsyncPartialBodyAndDisconnectedClient_Canceled() { StaggardContent content = new StaggardContent(); string address; using (var server = Utilities.CreateHttpServer(out address)) { var client = new HttpClient(); var responseTask = client.PostAsync(address, content); var context = await server.AcceptAsync(Utilities.DefaultTimeout); byte[] input = new byte[10]; int read = await context.Request.Body.ReadAsync(input, 0, input.Length, context.DisconnectToken); Assert.False(context.DisconnectToken.IsCancellationRequested); // The client should timeout and disconnect, making this read fail. var assertTask = Assert.ThrowsAsync <IOException>(async() => await context.Request.Body.ReadAsync(input, 0, input.Length, context.DisconnectToken)); client.CancelPendingRequests(); await assertTask; content.Block.Release(); context.Dispose(); await Assert.ThrowsAsync <TaskCanceledException>(async() => await responseTask); } }
public async Task Chunked_ReadAsyncPartialBodyUnderLimit_ThrowsAfterLimit() { var content = new StaggardContent(); string address; using (Utilities.CreateHttpServer(out address, async httpContext => { var feature = httpContext.Features.Get <IHttpMaxRequestBodySizeFeature>(); Assert.NotNull(feature); Assert.False(feature.IsReadOnly); Assert.Null(httpContext.Request.ContentLength); byte[] input = new byte[100]; int read = await httpContext.Request.Body.ReadAsync(input, 0, input.Length); Assert.Equal(10, read); content.Block.Release(); var ex = await Assert.ThrowsAsync <BadHttpRequestException>(() => httpContext.Request.Body.ReadAsync(input, 0, input.Length)); Assert.Equal("The total number of bytes read 20 has exceeded the request body size limit 10.", ex.Message); Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode); }, options => options.MaxRequestBodySize = 10)) { string response = await SendRequestAsync(address, content, chunked : true); Assert.Equal(string.Empty, response); } }
public async Task RequestBody_ReadAsyncPartialBodyWithTimeout_Success() { StaggardContent content = new StaggardContent(); string address; using (var server = Utilities.CreateHttpServer(out address)) { Task <string> responseTask = SendRequestAsync(address, content); var context = await server.AcceptAsync(Utilities.DefaultTimeout); byte[] input = new byte[10]; var cts = new CancellationTokenSource(); cts.CancelAfter(TimeSpan.FromSeconds(5)); int read = await context.Request.Body.ReadAsync(input, 0, input.Length, cts.Token); Assert.Equal(5, read); content.Block.Release(); read = await context.Request.Body.ReadAsync(input, 0, input.Length, cts.Token); Assert.Equal(5, read); context.Dispose(); string response = await responseTask; Assert.Equal(string.Empty, response); } }
public async Task RequestBody_ReadAsyncPartialBodyAndExpiredTimeout_Canceled() { StaggardContent content = new StaggardContent(); string address; using (var server = Utilities.CreateHttpServer(out address)) { Task <string> responseTask = SendRequestAsync(address, content); var context = await server.AcceptAsync(Utilities.DefaultTimeout); byte[] input = new byte[10]; var cts = new CancellationTokenSource(); int read = await context.Request.Body.ReadAsync(input, 0, input.Length, cts.Token); Assert.Equal(5, read); cts.CancelAfter(TimeSpan.FromMilliseconds(100)); var readTask = context.Request.Body.ReadAsync(input, 0, input.Length, cts.Token); Assert.False(readTask.IsCanceled); await Assert.ThrowsAsync <IOException>(async() => await readTask); content.Block.Release(); context.Dispose(); await Assert.ThrowsAsync <HttpRequestException>(async() => await responseTask); } }
public async Task Chunked_ReadSyncPartialBodyUnderLimit_ThrowsAfterLimit() { var content = new StaggardContent(); string address; using (Utilities.CreateHttpServer(out address, httpContext => { httpContext.Features.Get <IHttpBodyControlFeature>().AllowSynchronousIO = true; var feature = httpContext.Features.Get <IHttpMaxRequestBodySizeFeature>(); Assert.NotNull(feature); Assert.False(feature.IsReadOnly); Assert.Null(httpContext.Request.ContentLength); byte[] input = new byte[100]; int read = httpContext.Request.Body.Read(input, 0, input.Length); Assert.Equal(10, read); content.Block.Release(); var ex = Assert.Throws <IOException>(() => httpContext.Request.Body.Read(input, 0, input.Length)); Assert.Equal("The total number of bytes read 20 has exceeded the request body size limit 10.", ex.Message); return(Task.FromResult(0)); }, options => options.MaxRequestBodySize = 10)) { string response = await SendRequestAsync(address, content, chunked : true); Assert.Equal(string.Empty, response); } }
public async Task RequestBody_ReadAsyncPartialBody_Success() { StaggardContent content = new StaggardContent(); string address; using (var server = Utilities.CreateHttpServer(out address)) { Task <string> responseTask = SendRequestAsync(address, content); var context = await server.GetContextAsync(); byte[] input = new byte[10]; int read = await context.Request.Body.ReadAsync(input, 0, input.Length); Assert.Equal(5, read); content.Block.Release(); read = await context.Request.Body.ReadAsync(input, 0, input.Length); Assert.Equal(5, read); context.Dispose(); string response = await responseTask; Assert.Equal(string.Empty, response); } }
public async Task RequestBody_ReadAsyncPartialBody_Success() { StaggardContent content = new StaggardContent(); string address; using (Utilities.CreateHttpServer(out address, async httpContext => { byte[] input = new byte[10]; int read = await httpContext.Request.Body.ReadAsync(input, 0, input.Length); Assert.Equal(5, read); content.Block.Release(); read = await httpContext.Request.Body.ReadAsync(input, 0, input.Length); Assert.Equal(5, read); })) { string response = await SendRequestAsync(address, content); Assert.Equal(string.Empty, response); } }
public async Task RequestBody_ReadSyncPartialBody_Success() { StaggardContent content = new StaggardContent(); string address; using (Utilities.CreateHttpServer(out address, httpContext => { byte[] input = new byte[10]; httpContext.Features.Get <IHttpBodyControlFeature>().AllowSynchronousIO = true; int read = httpContext.Request.Body.Read(input, 0, input.Length); Assert.Equal(5, read); content.Block.Release(); read = httpContext.Request.Body.Read(input, 0, input.Length); Assert.Equal(5, read); return(Task.FromResult(0)); })) { string response = await SendRequestAsync(address, content); Assert.Equal(string.Empty, response); } }
public async Task RequestBody_ReadSyncPartialBody_Success() { StaggardContent content = new StaggardContent(); string address; using (var server = Utilities.CreateHttpServer(out address)) { Task <string> responseTask = SendRequestAsync(address, content); server.Options.AllowSynchronousIO = true; var context = await server.AcceptAsync(Utilities.DefaultTimeout).Before(responseTask); byte[] input = new byte[10]; int read = context.Request.Body.Read(input, 0, input.Length); Assert.Equal(5, read); content.Block.Release(); read = context.Request.Body.Read(input, 0, input.Length); Assert.Equal(5, read); context.Dispose(); string response = await responseTask; Assert.Equal(string.Empty, response); } }