|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
| 4 | +using System; |
| 5 | +using System.Net.Http; |
4 | 6 | using System.Threading.Tasks; |
5 | 7 | using Microsoft.AspNetCore.Builder; |
6 | 8 | using Microsoft.AspNetCore.Hosting; |
@@ -46,6 +48,80 @@ public async Task BodyWriter_StartAsyncGetMemoryAdvance_AutoCompleted() |
46 | 48 | Assert.Equal(length, bytes.Length); |
47 | 49 | } |
48 | 50 |
|
| 51 | + [Fact] |
| 52 | + public async Task BodyStream_SyncDisabled_WriteThrows() |
| 53 | + { |
| 54 | + var contentBytes = new byte[] {32}; |
| 55 | + using var host = await CreateHost(async httpContext => |
| 56 | + { |
| 57 | + await httpContext.Response.StartAsync(); |
| 58 | + httpContext.Response.Body.Write(contentBytes, 0, contentBytes.Length); |
| 59 | + await httpContext.Response.CompleteAsync(); |
| 60 | + }); |
| 61 | + |
| 62 | + var client = host.GetTestServer().CreateClient(); |
| 63 | + var ex = await Assert.ThrowsAsync<InvalidOperationException>(()=> client.GetAsync("/")); |
| 64 | + Assert.Contains("Synchronous operations are disallowed.", ex.Message); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public async Task BodyStream_SyncEnabled_WriteSucceeds() |
| 69 | + { |
| 70 | + var contentBytes = new byte[] {32}; |
| 71 | + using var host = await CreateHost(async httpContext => |
| 72 | + { |
| 73 | + await httpContext.Response.StartAsync(); |
| 74 | + httpContext.Response.Body.Write(contentBytes, 0, contentBytes.Length); |
| 75 | + await httpContext.Response.CompleteAsync(); |
| 76 | + }); |
| 77 | + |
| 78 | + host.GetTestServer().AllowSynchronousIO = true; |
| 79 | + |
| 80 | + var client = host.GetTestServer().CreateClient(); |
| 81 | + var response = await client.GetAsync("/"); |
| 82 | + var responseBytes = await response.Content.ReadAsByteArrayAsync(); |
| 83 | + Assert.Equal(contentBytes, responseBytes); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public async Task BodyStream_SyncDisabled_FlushThrows() |
| 88 | + { |
| 89 | + var contentBytes = new byte[] {32}; |
| 90 | + using var host = await CreateHost(async httpContext => |
| 91 | + { |
| 92 | + await httpContext.Response.StartAsync(); |
| 93 | + await httpContext.Response.Body.WriteAsync(contentBytes, 0, contentBytes.Length); |
| 94 | + httpContext.Response.Body.Flush(); |
| 95 | + await httpContext.Response.CompleteAsync(); |
| 96 | + }); |
| 97 | + |
| 98 | + var client = host.GetTestServer().CreateClient(); |
| 99 | + var requestException = await Assert.ThrowsAsync<HttpRequestException>(()=> client.GetAsync("/")); |
| 100 | + var ex = (InvalidOperationException) requestException?.InnerException?.InnerException; |
| 101 | + Assert.NotNull(ex); |
| 102 | + Assert.Contains("Synchronous operations are disallowed.", ex.Message); |
| 103 | + } |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public async Task BodyStream_SyncEnabled_FlushSucceeds() |
| 107 | + { |
| 108 | + var contentBytes = new byte[] {32}; |
| 109 | + using var host = await CreateHost(async httpContext => |
| 110 | + { |
| 111 | + await httpContext.Response.StartAsync(); |
| 112 | + await httpContext.Response.Body.WriteAsync(contentBytes, 0, contentBytes.Length); |
| 113 | + httpContext.Response.Body.Flush(); |
| 114 | + await httpContext.Response.CompleteAsync(); |
| 115 | + }); |
| 116 | + |
| 117 | + host.GetTestServer().AllowSynchronousIO = true; |
| 118 | + |
| 119 | + var client = host.GetTestServer().CreateClient(); |
| 120 | + var response = await client.GetAsync("/"); |
| 121 | + var responseBytes = await response.Content.ReadAsByteArrayAsync(); |
| 122 | + Assert.Equal(contentBytes, responseBytes); |
| 123 | + } |
| 124 | + |
49 | 125 | private Task<IHost> CreateHost(RequestDelegate appDelegate) |
50 | 126 | { |
51 | 127 | return new HostBuilder() |
|
0 commit comments