|
4 | 4 | using System;
|
5 | 5 | using System.IO;
|
6 | 6 | using System.Linq;
|
| 7 | +using System.Net; |
7 | 8 | using System.Net.Http;
|
| 9 | +using System.Text; |
8 | 10 | using System.Threading;
|
9 | 11 | using System.Threading.Tasks;
|
10 | 12 | using Microsoft.AspNetCore.Builder;
|
@@ -103,6 +105,51 @@ public Task UserAgentHeaderWorks()
|
103 | 105 | return httpClient.GetAsync("http://example.com");
|
104 | 106 | }
|
105 | 107 |
|
| 108 | + [Fact] |
| 109 | + public Task ContentLengthWithBodyWorks() |
| 110 | + { |
| 111 | + var contentBytes = Encoding.UTF8.GetBytes("This is a content!"); |
| 112 | + var handler = new ClientHandler(new PathString(""), new DummyApplication(context => |
| 113 | + { |
| 114 | + Assert.Equal(contentBytes.LongLength, context.Request.ContentLength); |
| 115 | + |
| 116 | + return Task.CompletedTask; |
| 117 | + })); |
| 118 | + var httpClient = new HttpClient(handler); |
| 119 | + var content = new ByteArrayContent(contentBytes); |
| 120 | + |
| 121 | + return httpClient.PostAsync("http://example.com", content); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public Task ContentLengthWithNoBodyWorks() |
| 126 | + { |
| 127 | + var handler = new ClientHandler(new PathString(""), new DummyApplication(context => |
| 128 | + { |
| 129 | + Assert.Equal(0, context.Request.ContentLength); |
| 130 | + |
| 131 | + return Task.CompletedTask; |
| 132 | + })); |
| 133 | + var httpClient = new HttpClient(handler); |
| 134 | + |
| 135 | + return httpClient.GetAsync("http://example.com"); |
| 136 | + } |
| 137 | + |
| 138 | + [Fact] |
| 139 | + public Task ContentLengthWithChunkedTransferEncodingWorks() |
| 140 | + { |
| 141 | + var handler = new ClientHandler(new PathString(""), new DummyApplication(context => |
| 142 | + { |
| 143 | + Assert.Null(context.Request.ContentLength); |
| 144 | + |
| 145 | + return Task.CompletedTask; |
| 146 | + })); |
| 147 | + |
| 148 | + var httpClient = new HttpClient(handler); |
| 149 | + |
| 150 | + return httpClient.PostAsync("http://example.com", new UnlimitedContent()); |
| 151 | + } |
| 152 | + |
106 | 153 | [Fact]
|
107 | 154 | public async Task ServerTrailersSetOnResponseAfterContentRead()
|
108 | 155 | {
|
@@ -566,5 +613,19 @@ public void Dispose()
|
566 | 613 | }
|
567 | 614 | }
|
568 | 615 | }
|
| 616 | + |
| 617 | + private class UnlimitedContent : HttpContent |
| 618 | + { |
| 619 | + protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) |
| 620 | + { |
| 621 | + return Task.CompletedTask; |
| 622 | + } |
| 623 | + |
| 624 | + protected override bool TryComputeLength(out long length) |
| 625 | + { |
| 626 | + length = -1; |
| 627 | + return false; |
| 628 | + } |
| 629 | + } |
569 | 630 | }
|
570 | 631 | }
|
0 commit comments