Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit cdceb59

Browse files
committed
Add check for Content-Length in the tests
1 parent b4eca8d commit cdceb59

File tree

2 files changed

+57
-51
lines changed

2 files changed

+57
-51
lines changed

src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/FileResultExecutorBase.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,16 @@ protected virtual (RangeItemHeaderValue range, long rangeLength, bool serveBody)
6464
var response = context.HttpContext.Response;
6565
SetLastModifiedAndEtagHeaders(response, lastModified, etag);
6666

67-
var serveBody = !HttpMethods.IsHead(request.Method);
68-
6967
// Short circuit if the preconditional headers process to 304 (NotModified) or 412 (PreconditionFailed)
7068
if (preconditionState == PreconditionState.NotModified)
7169
{
72-
serveBody = false;
7370
response.StatusCode = StatusCodes.Status304NotModified;
74-
return (range: null, rangeLength: 0, serveBody);
71+
return (range: null, rangeLength: 0, serveBody: false);
7572
}
7673
else if (preconditionState == PreconditionState.PreconditionFailed)
7774
{
78-
serveBody = false;
7975
response.StatusCode = StatusCodes.Status412PreconditionFailed;
80-
return (range: null, rangeLength: 0, serveBody);
76+
return (range: null, rangeLength: 0, serveBody: false);
8177
}
8278

8379
if (fileLength.HasValue)
@@ -86,10 +82,8 @@ protected virtual (RangeItemHeaderValue range, long rangeLength, bool serveBody)
8682
// the length of the entire file.
8783
// If the request is a valid range request, this header is overwritten with the length of the range as part of the
8884
// range processing (see method SetContentLength).
89-
if (serveBody)
90-
{
91-
response.ContentLength = fileLength.Value;
92-
}
85+
86+
response.ContentLength = fileLength.Value;
9387

9488
// Handle range request
9589
if (enableRangeProcessing)
@@ -111,7 +105,7 @@ protected virtual (RangeItemHeaderValue range, long rangeLength, bool serveBody)
111105
}
112106
}
113107

114-
return (range: null, rangeLength: 0, serveBody);
108+
return (range: null, rangeLength: 0, serveBody: !HttpMethods.IsHead(request.Method));
115109
}
116110

117111
private static void SetContentType(ActionContext context, FileResult result)

test/Microsoft.AspNetCore.Mvc.FunctionalTests/FileResultTests.cs

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,16 @@ public async Task FileFromDisk_ReturnsFileWithFileName_IfRangeHeaderInvalid_Rang
247247
}
248248

249249
[Theory]
250-
[InlineData("", HttpStatusCode.OK)]
251-
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent)]
252-
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent)]
253-
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent)]
254-
[InlineData("0-6", HttpStatusCode.OK)]
255-
[InlineData("bytes = ", HttpStatusCode.OK)]
256-
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK)]
257-
[InlineData("bytes = 35-36", HttpStatusCode.RequestedRangeNotSatisfiable)]
258-
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable)]
259-
public async Task FileFromDisk_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest_WithLastModifiedAndEtag(string rangeString, HttpStatusCode httpStatusCode)
250+
[InlineData("", HttpStatusCode.OK, 26)]
251+
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent, 7)]
252+
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent, 9)]
253+
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent, 26)]
254+
[InlineData("0-6", HttpStatusCode.OK, 26)]
255+
[InlineData("bytes = ", HttpStatusCode.OK, 26)]
256+
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK, 26)]
257+
[InlineData("bytes = 35-36", HttpStatusCode.RequestedRangeNotSatisfiable, 26)]
258+
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable, 26)]
259+
public async Task FileFromDisk_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest_WithLastModifiedAndEtag(string rangeString, HttpStatusCode httpStatusCode, int expectedContentLength)
260260
{
261261
// Arrange
262262
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, "http://localhost/DownloadFiles/DownloadFromDiskWithFileName_WithLastModifiedAndEtag");
@@ -276,6 +276,9 @@ public async Task FileFromDisk_ReturnsFileWithFileName_DoesNotServeBody_ForHeadR
276276
Assert.NotNull(body);
277277
Assert.Equal(string.Empty, body);
278278

279+
var contentLength = response.Content.Headers.ContentLength;
280+
Assert.Equal(expectedContentLength, contentLength);
281+
279282
var contentDisposition = response.Content.Headers.ContentDisposition.ToString();
280283
Assert.NotNull(contentDisposition);
281284
Assert.Equal("attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt", contentDisposition);
@@ -440,16 +443,16 @@ public async Task FileFromStream_ReturnsFileWithFileName_IfRangeHeaderInvalid_Ra
440443
}
441444

442445
[Theory]
443-
[InlineData("", HttpStatusCode.OK)]
444-
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent)]
445-
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent)]
446-
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent)]
447-
[InlineData("0-6", HttpStatusCode.OK)]
448-
[InlineData("bytes = ", HttpStatusCode.OK)]
449-
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK)]
450-
[InlineData("bytes = 35-36", HttpStatusCode.RequestedRangeNotSatisfiable)]
451-
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable)]
452-
public async Task FileFromStream_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(string rangeString, HttpStatusCode httpStatusCode)
446+
[InlineData("", HttpStatusCode.OK, 33)]
447+
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent, 7)]
448+
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent, 9)]
449+
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent, 33)]
450+
[InlineData("0-6", HttpStatusCode.OK, 33)]
451+
[InlineData("bytes = ", HttpStatusCode.OK, 33)]
452+
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK, 33)]
453+
[InlineData("bytes = 35-36", HttpStatusCode.RequestedRangeNotSatisfiable, 33)]
454+
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable, 33)]
455+
public async Task FileFromStream_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(string rangeString, HttpStatusCode httpStatusCode, int expectedContentLength)
453456
{
454457
// Arrange
455458
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, "http://localhost/DownloadFiles/DownloadFromStreamWithFileName_WithEtag");
@@ -469,6 +472,9 @@ public async Task FileFromStream_ReturnsFileWithFileName_DoesNotServeBody_ForHea
469472
Assert.NotNull(body);
470473
Assert.Equal(string.Empty, body);
471474

475+
var contentLength = response.Content.Headers.ContentLength;
476+
Assert.Equal(expectedContentLength, contentLength);
477+
472478
var contentDisposition = response.Content.Headers.ContentDisposition.ToString();
473479
Assert.NotNull(contentDisposition);
474480
Assert.Equal("attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt", contentDisposition);
@@ -638,16 +644,16 @@ public async Task FileFromBinaryData_ReturnsFileWithFileName_IfRangeHeaderInvali
638644
}
639645

640646
[Theory]
641-
[InlineData("", HttpStatusCode.OK)]
642-
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent)]
643-
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent)]
644-
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent)]
645-
[InlineData("0-6", HttpStatusCode.OK)]
646-
[InlineData("bytes = ", HttpStatusCode.OK)]
647-
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK)]
648-
[InlineData("bytes = 45-46", HttpStatusCode.RequestedRangeNotSatisfiable)]
649-
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable)]
650-
public async Task FileFromBinaryData_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(string rangeString, HttpStatusCode httpStatusCode)
647+
[InlineData("", HttpStatusCode.OK, 41)]
648+
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent, 7)]
649+
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent, 9)]
650+
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent, 41)]
651+
[InlineData("0-6", HttpStatusCode.OK, 41)]
652+
[InlineData("bytes = ", HttpStatusCode.OK, 41)]
653+
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK, 41)]
654+
[InlineData("bytes = 45-46", HttpStatusCode.RequestedRangeNotSatisfiable, 41)]
655+
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable, 41)]
656+
public async Task FileFromBinaryData_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(string rangeString, HttpStatusCode httpStatusCode, int expectedContentLength)
651657
{
652658
// Arrange
653659
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, "http://localhost/DownloadFiles/DownloadFromBinaryDataWithFileName_WithEtag");
@@ -667,6 +673,9 @@ public async Task FileFromBinaryData_ReturnsFileWithFileName_DoesNotServeBody_Fo
667673
Assert.NotNull(body);
668674
Assert.Equal(string.Empty, body);
669675

676+
var contentLength = response.Content.Headers.ContentLength;
677+
Assert.Equal(expectedContentLength, contentLength);
678+
670679
var contentDisposition = response.Content.Headers.ContentDisposition.ToString();
671680
Assert.NotNull(contentDisposition);
672681
Assert.Equal("attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt", contentDisposition);
@@ -834,16 +843,16 @@ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeRequest
834843
}
835844

836845
[Theory]
837-
[InlineData("", HttpStatusCode.OK)]
838-
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent)]
839-
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent)]
840-
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent)]
841-
[InlineData("0-6", HttpStatusCode.OK)]
842-
[InlineData("bytes = ", HttpStatusCode.OK)]
843-
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK)]
844-
[InlineData("bytes = 45-46", HttpStatusCode.RequestedRangeNotSatisfiable)]
845-
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable)]
846-
public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(string rangeString, HttpStatusCode httpStatusCode)
846+
[InlineData("", HttpStatusCode.OK, 38)]
847+
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent, 7)]
848+
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent, 9)]
849+
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent, 38)]
850+
[InlineData("0-6", HttpStatusCode.OK, 38)]
851+
[InlineData("bytes = ", HttpStatusCode.OK, 38)]
852+
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK, 38)]
853+
[InlineData("bytes = 45-46", HttpStatusCode.RequestedRangeNotSatisfiable, 38)]
854+
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable, 38)]
855+
public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(string rangeString, HttpStatusCode httpStatusCode, int expectedContentLength)
847856
{
848857
// Arrange
849858
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, "http://localhost/EmbeddedFiles/DownloadFileWithFileName");
@@ -863,6 +872,9 @@ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_DoesNotServe
863872
Assert.NotNull(body);
864873
Assert.Equal(string.Empty, body);
865874

875+
var contentLength = response.Content.Headers.ContentLength;
876+
Assert.Equal(expectedContentLength, contentLength);
877+
866878
var contentDisposition = response.Content.Headers.ContentDisposition.ToString();
867879
Assert.NotNull(contentDisposition);
868880
Assert.Equal("attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt", contentDisposition);

0 commit comments

Comments
 (0)