-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1191 from cabinetoffice/DP-1024-uploading-25md-fi…
…le-error DP-1024 - Uploading a file over 25mb results in error
- Loading branch information
Showing
4 changed files
with
129 additions
and
7 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
Frontend/CO.CDP.OrganisationApp.Tests/FileUploadMiddlewareTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using CO.CDP.OrganisationApp.Pages.Forms; | ||
using Microsoft.AspNetCore.Http; | ||
using Moq; | ||
using System.Net; | ||
|
||
namespace CO.CDP.OrganisationApp.Tests; | ||
|
||
public class FileUploadMiddlewareTests | ||
{ | ||
public class FileUploadMiddlewareTest : FileUploadMiddleware | ||
{ | ||
public FileUploadMiddlewareTest(RequestDelegate next) : base(next) | ||
{ | ||
} | ||
|
||
protected override async Task WriteAsync(HttpContext context) | ||
{ | ||
var writer = new StreamWriter(context.Response.Body); | ||
|
||
await writer.WriteAsync($"The uploaded file is too large. Maximum allowed size is {FormElementFileUploadModel.AllowedMaxFileSizeMB} MB."); | ||
writer.Flush(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task InvokeAsync_ShouldWriteResponse_WhenStatusCodeIs400AndContentTypeIsMultipartFormData() | ||
{ | ||
var responseBody = new MemoryStream(); | ||
var httpContextMock = new Mock<HttpContext>(); | ||
var middleware = SetUp(HttpStatusCode.BadRequest, "multipart/form-data;", httpContextMock, responseBody); | ||
|
||
await middleware.InvokeAsync(httpContextMock.Object); | ||
|
||
responseBody.Seek(0, SeekOrigin.Begin); | ||
var reader = new StreamReader(responseBody); | ||
var responseBodyText = await reader.ReadToEndAsync(); | ||
Assert.Contains("The uploaded file is too large. Maximum allowed size is", responseBodyText); | ||
} | ||
|
||
[Fact] | ||
public async Task InvokeAsync_ShouldNotWriteResponse_WhenStatusCodeIsNot400() | ||
{ | ||
var responseBody = new MemoryStream(); | ||
var httpContextMock = new Mock<HttpContext>(); | ||
var middleware = SetUp(HttpStatusCode.OK, "multipart/form-data;", httpContextMock, responseBody); | ||
|
||
await middleware.InvokeAsync(httpContextMock.Object); | ||
|
||
responseBody.Seek(0, SeekOrigin.Begin); | ||
var reader = new StreamReader(responseBody); | ||
var responseBodyText = await reader.ReadToEndAsync(); | ||
Assert.Contains(string.Empty, responseBodyText); | ||
} | ||
|
||
[Fact] | ||
public async Task InvokeAsync_ShouldNotWriteResponse_WhenContentTypeIsNotMultipartFormData() | ||
{ | ||
var responseBody = new MemoryStream(); | ||
var httpContextMock = new Mock<HttpContext>(); | ||
var middleware = SetUp(HttpStatusCode.BadRequest, "application/json", httpContextMock, responseBody); | ||
|
||
await middleware.InvokeAsync(httpContextMock.Object); | ||
|
||
responseBody.Seek(0, SeekOrigin.Begin); | ||
var reader = new StreamReader(responseBody); | ||
var responseBodyText = await reader.ReadToEndAsync(); | ||
Assert.Contains(string.Empty, responseBodyText); | ||
} | ||
|
||
private FileUploadMiddleware SetUp(HttpStatusCode statusCode, | ||
string contentType, | ||
Mock<HttpContext> httpContextMock, | ||
MemoryStream responseBody) | ||
{ | ||
var requestMock = new Mock<HttpRequest>(); | ||
var responseMock = new Mock<HttpResponse>(); | ||
var requestDelegateMock = new Mock<RequestDelegate>(); | ||
|
||
responseMock.Setup(r => r.StatusCode).Returns((int)statusCode); | ||
requestMock.Setup(h => h.ContentType).Returns(contentType); | ||
|
||
httpContextMock.Setup(h => h.Request).Returns(requestMock.Object); | ||
httpContextMock.Setup(h => h.Response).Returns(responseMock.Object); | ||
responseMock.Setup(res => res.Body).Returns(responseBody); | ||
|
||
return new FileUploadMiddlewareTest(requestDelegateMock.Object); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using CO.CDP.OrganisationApp.Pages.Forms; | ||
using System.Net; | ||
|
||
namespace CO.CDP.OrganisationApp; | ||
|
||
public class FileUploadMiddleware(RequestDelegate next) | ||
{ | ||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
await next(context); | ||
|
||
string contentType = context.Request.ContentType ?? string.Empty; | ||
bool isMultipartFormData = contentType.Contains("multipart/form-data;"); | ||
|
||
if ((context.Response.StatusCode == (int)HttpStatusCode.BadRequest) && (isMultipartFormData)) | ||
{ | ||
await WriteAsync(context); | ||
} | ||
} | ||
|
||
protected virtual async Task WriteAsync(HttpContext context) | ||
{ | ||
await context.Response.WriteAsync($"The uploaded file is too large. Maximum allowed size is {FormElementFileUploadModel.AllowedMaxFileSizeMB} MB."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters