Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit979e6ac

Browse files
authored
AddContent to allHttpResponseMessages (#387)
-fix#386 - reenable `net6.0` tests disabled in#384 for this issue- also, check `Stream.CanSeek` less when calculating `HttpMessageContent.ContentLength` value - use inner `HttpContent.Headers.ContentLength` if available - this changes behaviour slightly for both requests and responses - observable as `HttpMessageContent.Headers.ContentLength!=null` for empty messages - for responses, avoids `ContentLength==null` versus `ContentLength==0` inconsistencies (depending on platform) - `ContentLength==null` may still occur in some corner cases (which existed before) - e.g. when inner `HttpContent` doesn't know its length and `ReadAsStreamAsync()` hasn't completed- main change means `HttpResponseMessage`s we expose to user code are consistent across platforms - note: user code won't see `EmptyContent` in `HttpResponseMessage`s we create
1 parent0f4c624 commit979e6ac

File tree

10 files changed

+29
-20
lines changed

10 files changed

+29
-20
lines changed

‎src/System.Net.Http.Formatting/HttpContentMessageExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ private static HttpContent CreateHeaderFields(HttpHeaders source, HttpHeaders de
451451
Contract.Assert(destination!=null,"destination headers cannot be null");
452452
Contract.Assert(contentStream!=null,"contentStream must be non null");
453453
HttpContentHeaderscontentHeaders=null;
454-
HttpContentcontent=null;
454+
HttpContentcontent;
455455

456456
// Set the header fields
457457
foreach(KeyValuePair<string,IEnumerable<string>>headerinsource)
@@ -481,6 +481,10 @@ private static HttpContent CreateHeaderFields(HttpHeaders source, HttpHeaders de
481481
content=newStreamContent(contentStream);
482482
contentHeaders.CopyTo(content.Headers);
483483
}
484+
else
485+
{
486+
content=newStreamContent(Stream.Null);
487+
}
484488

485489
returncontent;
486490
}

‎src/System.Net.Http.Formatting/HttpMessageContent.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ protected override async Task SerializeToStreamAsync(Stream stream, TransportCon
205205
protectedoverrideboolTryComputeLength(outlonglength)
206206
{
207207
// We have four states we could be in:
208+
// 0. We have content and it knows its ContentLength.
208209
// 1. We have content, but the task is still running or finished without success
209210
// 2. We have content, the task has finished successfully, and the stream came back as a null or non-seekable
210211
// 3. We have content, the task has finished successfully, and the stream is seekable, so we know its length
@@ -214,11 +215,13 @@ protected override bool TryComputeLength(out long length)
214215
// For #3, we return true & the size of our headers + the content length
215216
// For #4, we return true & the size of our headers
216217

217-
boolhasContent=_streamTask.Value!=null;
218218
length=0;
219219

220-
// Cases #1, #2, #3
221-
if(hasContent)
220+
if(Content?.Headers.ContentLengthis notnull)
221+
{
222+
length=(long)Content.Headers.ContentLength;// Case #0
223+
}
224+
elseif(_streamTask.Valueis notnull)
222225
{
223226
StreamreadStream;
224227
if(!_streamTask.Value.TryGetResult(outreadStream)// Case #1

‎src/System.Net.Http.Formatting/HttpRequestMessageExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
usingSystem.ComponentModel;
55
usingSystem.Diagnostics.CodeAnalysis;
6+
usingSystem.IO;
67
usingSystem.Web.Http;
78

89
namespaceSystem.Net.Http
@@ -29,6 +30,7 @@ public static HttpResponseMessage CreateResponse(this HttpRequestMessage request
2930

3031
returnnewHttpResponseMessage
3132
{
33+
Content=newStreamContent(Stream.Null),
3234
StatusCode=statusCode,
3335
RequestMessage=request
3436
};
@@ -49,6 +51,7 @@ public static HttpResponseMessage CreateResponse(this HttpRequestMessage request
4951

5052
returnnewHttpResponseMessage
5153
{
54+
Content=newStreamContent(Stream.Null),
5255
RequestMessage=request
5356
};
5457
}

‎test/System.Net.Http.Formatting.Test/Formatting/JsonMediaTypeFormatterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ public override async Task WriteToStreamAsync_WhenObjectIsNull_WritesDataButDoes
588588
// Arrange
589589
JsonMediaTypeFormatterformatter=CreateFormatter();
590590
Streamstream=newMemoryStream();
591-
HttpContentcontent=newStringContent(String.Empty);
591+
HttpContentcontent=newStreamContent(Stream.Null);
592592

593593
// Act
594594
awaitformatter.WriteToStreamAsync(typeof(SampleType),null,stream,content,null);

‎test/System.Net.Http.Formatting.Test/Handlers/ProgressMessageHandlerTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ public async Task SendAsync_DoesNotInsertSendProgressWithoutEntityOrHandlerPrese
5656
}
5757

5858
[Theory]
59-
#if!NET6_0_OR_GREATER// https://github.com/aspnet/AspNetWebStack/issues/386
6059
[InlineData(false,false)]
6160
[InlineData(false,true)]
62-
#endif
6361
[InlineData(true,false)]
6462
[InlineData(true,true)]
6563
publicasyncTaskSendAsync_InsertsReceiveProgressWhenResponseEntityPresent(boolinsertResponseEntity,booladdReceiveProgressHandler)
@@ -86,7 +84,8 @@ public async Task SendAsync_InsertsReceiveProgressWhenResponseEntityPresent(bool
8684
}
8785
else
8886
{
89-
Assert.Null(response.Content);
87+
Assert.NotNull(response.Content);
88+
Assert.Equal(0L,response.Content.Headers.ContentLength);
9089
}
9190
}
9291
}

‎test/System.Net.Http.Formatting.Test/Handlers/ProgressStreamTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ internal static ProgressStream CreateProgressStream(
274274
StreamiStream=innerStream??newMock<Stream>().Object;
275275
ProgressMessageHandlerpHandler=progressMessageHandler??newProgressMessageHandler();
276276
HttpRequestMessagereq=request??newHttpRequestMessage();
277-
HttpResponseMessagersp=response??newHttpResponseMessage();
277+
HttpResponseMessagersp=response??newHttpResponseMessage(){Content=newStreamContent(Stream.Null)};
278278
returnnewProgressStream(iStream,pHandler,req,rsp);
279279
}
280280

‎test/System.Net.Http.Formatting.Test/HttpMessageContentTests.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
usingSystem.IO;
45
usingSystem.Net.Http.Headers;
56
usingSystem.Threading.Tasks;
67
usingMicrosoft.TestCommon;
@@ -39,10 +40,8 @@ private static HttpResponseMessage CreateResponse(bool containsEntity)
3940
httpResponse.ReasonPhrase=ParserData.HttpReasonPhrase;
4041
httpResponse.Version=newVersion("1.2");
4142
AddMessageHeaders(httpResponse.Headers);
42-
if(containsEntity)
43-
{
44-
httpResponse.Content=newStringContent(ParserData.HttpMessageEntity);
45-
}
43+
httpResponse.Content=
44+
containsEntity?newStringContent(ParserData.HttpMessageEntity):newStreamContent(Stream.Null);
4645

4746
returnhttpResponse;
4847
}
@@ -76,6 +75,7 @@ private static async Task ValidateRequest(HttpContent content, bool containsEnti
7675
privatestaticasyncTaskValidateResponse(HttpContentcontent,boolcontainsEntity)
7776
{
7877
Assert.Equal(ParserData.HttpResponseMediaType,content.Headers.ContentType);
78+
7979
long?length=content.Headers.ContentLength;
8080
Assert.NotNull(length);
8181

@@ -164,7 +164,6 @@ public async Task SerializeRequestMultipleTimes()
164164
}
165165
}
166166

167-
#if!NET6_0_OR_GREATER// https://github.com/aspnet/AspNetWebStack/issues/386
168167
[Fact]
169168
publicasyncTaskSerializeResponse()
170169
{
@@ -186,7 +185,6 @@ public async Task SerializeResponseMultipleTimes()
186185
awaitValidateResponse(instance,false);
187186
}
188187
}
189-
#endif
190188

191189
[Fact]
192190
publicasyncTaskSerializeRequestWithEntity()
@@ -243,7 +241,6 @@ public async Task SerializeRequestAsync()
243241
}
244242
}
245243

246-
#if!NET6_0_OR_GREATER// https://github.com/aspnet/AspNetWebStack/issues/386
247244
[Fact]
248245
publicasyncTaskSerializeResponseAsync()
249246
{
@@ -254,7 +251,6 @@ public async Task SerializeResponseAsync()
254251
awaitValidateResponse(instance,false);
255252
}
256253
}
257-
#endif
258254

259255
[Fact]
260256
publicasyncTaskSerializeRequestWithPortAndQueryAsync()

‎test/System.Net.Http.Formatting.Test/ParserData.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,15 @@ public static TheoryDataSet<string> InvalidStatusCodes
198198
((int)HttpStatus).ToString()+
199199
" "+
200200
HttpReasonPhrase+
201-
"\r\nN1: V1a, V1b, V1c, V1d, V1e\r\nN2: V2\r\n\r\n";
201+
"\r\nN1: V1a, V1b, V1c, V1d, V1e\r\nN2: V2\r\nContent-Length: 0\r\n\r\n";
202202

203203
publicstaticreadonlystringHttpRequestWithEntity=
204204
HttpMethod+
205205
" /some/path HTTP/1.2\r\nHost: "+
206206
HttpHostName+
207207
"\r\nN1: V1a, V1b, V1c, V1d, V1e\r\nN2: V2\r\nContent-Type: "+
208208
TextContentType+
209+
"\r\nContent-Length: 100"+
209210
"\r\n\r\n"+
210211
HttpMessageEntity;
211212

@@ -216,6 +217,7 @@ public static TheoryDataSet<string> InvalidStatusCodes
216217
HttpReasonPhrase+
217218
"\r\nN1: V1a, V1b, V1c, V1d, V1e\r\nN2: V2\r\nContent-Type: "+
218219
TextContentType+
220+
"\r\nContent-Length: 100"+
219221
"\r\n\r\n"+
220222
HttpMessageEntity;
221223
}

‎test/System.Web.Http.Test/Controllers/VoidResultConverterTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public void Convert_ReturnsResponseMessageWithRequestAssignedAndNoContentToRefle
3131
varresult=_converter.Convert(_context,null);
3232

3333
Assert.Equal(HttpStatusCode.NoContent,result.StatusCode);
34-
Assert.Null(result.Content);
34+
Assert.NotNull(result.Content);
35+
Assert.Equal(0L,result.Content.Headers.ContentLength);
3536
Assert.Same(_request,result.RequestMessage);
3637
}
3738
}

‎test/System.Web.Http.WebHost.Test/WebHostExceptionHandlerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ public async Task HandleAsync_IfCatchBlockIsWebHostBufferedContent_WithCreateExc
220220
{
221221
Assert.NotNull(response);
222222
Assert.Equal(HttpStatusCode.InternalServerError,response.StatusCode);
223-
Assert.Null(response.Content);
223+
Assert.NotNull(response.Content);
224+
Assert.Equal(0L,response.Content.Headers.ContentLength);
224225
Assert.Same(expectedRequest,response.RequestMessage);
225226
}
226227
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp