Skip to content
This repository was archived by the owner on Aug 30, 2025. It is now read-only.

Commit 17a116c

Browse files
committed
test: Added tests to error 413
1 parent 0e70e87 commit 17a116c

6 files changed

Lines changed: 137 additions & 87 deletions

File tree

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ public class Startup
121121
exception.Errors.ToDictionary(
122122
k => k.Key,
123123
v => new ErrorDetails(v.Value, exception.Message)
124-
)
125-
));
124+
)));
126125
});
127126
}
128127
}
@@ -157,8 +156,7 @@ public class HomeController : ControllerBase
157156
["Property1"] = new("Error1", "Message1"),
158157
["Property2"] = new("Error2", "Message2"),
159158
["Property3"] = new("Error3", "Message3"),
160-
}
161-
);
159+
});
162160

163161
[HttpGet("call-2")]
164162
public IActionResult Call2()

samples/PowerUtils.AspNetCore.ErrorHandler.Samples/Controllers/FilesController.cs renamed to samples/PowerUtils.AspNetCore.ErrorHandler.Samples/Controllers/LargeRequestsController.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
namespace PowerUtils.AspNetCore.ErrorHandler.Samples.Controllers
55
{
66
[ApiController]
7-
[Route("files")]
8-
public class FilesController : ControllerBase
7+
[Route("large-requests")]
8+
public class LargeRequestsController : ControllerBase
99
{
10-
[HttpPost]
10+
[HttpPost("file")]
1111
[Consumes("multipart/form-data")]
1212
public IActionResult Upload([FromForm] FileRequest _)
1313
=> Ok();
14+
15+
[HttpPost("text")]
16+
public IActionResult Text(string _)
17+
=> Ok();
1418
}
1519
}

samples/PowerUtils.AspNetCore.ErrorHandler.Samples/Startup.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public void ConfigureServices(IServiceCollection services)
2626
option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
2727

2828
}).AddJwtBearer(options
29-
=> options.TokenValidationParameters = new TokenValidation()
30-
);
29+
=> options.TokenValidationParameters = new TokenValidation());
3130

3231
// Validation with basic authentication
3332
services
@@ -71,9 +70,9 @@ public void ConfigureServices(IServiceCollection services)
7170
// Configurations to payload size
7271
services.Configure<FormOptions>(options =>
7372
{
74-
options.ValueLengthLimit = 1048576; // Default value (4194304 Bytes -> 4 MB) - 1048576 = 1MB
75-
options.MultipartBodyLengthLimit = 1048576; // Default value (134217728 Bytes -> 128 MB) - 1048576 = 1MB
76-
options.MemoryBufferThreshold = 1048576; // Default value (65536 Bytes -> 128 MB) - 1048576 = 1MB
73+
options.ValueLengthLimit = 1_048_576; // Default value (4194304 Bytes -> 4 MB) - 1048576 = 1MB
74+
options.MultipartBodyLengthLimit = 1_048_576; // Default value (134217728 Bytes -> 128 MB) - 1048576 = 1MB
75+
options.MemoryBufferThreshold = 1_048_576; // Default value (65536 Bytes -> 128 MB) - 1048576 = 1MB
7776
});
7877
}
7978

@@ -92,8 +91,7 @@ public void Configure(IApplicationBuilder app)
9291
app.UseAuthorization();
9392

9493
app.UseEndpoints(endpoints
95-
=> endpoints.MapControllers() // Mapping all controller
96-
);
94+
=> endpoints.MapControllers()); // Mapping all controller
9795
}
9896
}
9997
}

tests/PowerUtils.AspNetCore.ErrorHandler.Tests/Tests/Controllers/FilesControllerTests.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.IO;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
8+
using FluentAssertions.Execution;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Microsoft.Extensions.Options;
11+
using PowerUtils.AspNetCore.ErrorHandler.Tests.Config;
12+
using PowerUtils.AspNetCore.ErrorHandler.Tests.Utils;
13+
using Xunit;
14+
15+
namespace PowerUtils.AspNetCore.ErrorHandler.Tests.Tests.Controllers
16+
{
17+
[Collection(nameof(IntegrationApiTestsFixtureCollection))]
18+
public class LargeRequestsControllerTests
19+
{
20+
private readonly IntegrationTestsFixture _testsFixture;
21+
22+
public LargeRequestsControllerTests(IntegrationTestsFixture testsFixture)
23+
=> _testsFixture = testsFixture;
24+
25+
26+
27+
[Fact]
28+
public async Task LargeFile_Send_413()
29+
{
30+
// Arrange
31+
var requestUri = "/large-requests/file";
32+
var options = _testsFixture.GetService<IOptions<ApiBehaviorOptions>>();
33+
34+
var file = FileUtils.LoadFile("../../../../../media/test_3_23mb.jpg");
35+
var body = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
36+
body.Add(file, "FileFake", "upload.jpg");
37+
38+
39+
// Act
40+
(var response, var content) = await _testsFixture.Client.SendPostMultipartAsync(requestUri, body);
41+
options.Value.ClientErrorMapping.TryGetValue((int)response.StatusCode, out var clientErrorData);
42+
43+
44+
// Assert
45+
using(new AssertionScope())
46+
{
47+
response.ValidateResponse(HttpStatusCode.RequestEntityTooLarge);
48+
49+
content.ValidateContent(
50+
HttpStatusCode.RequestEntityTooLarge,
51+
clientErrorData,
52+
"POST: " + requestUri,
53+
"The payload is too big.",
54+
new Dictionary<string, ErrorDetails>()
55+
{
56+
{ "payload", new("MAX:1048576", "The payload is too big.") }
57+
});
58+
}
59+
}
60+
61+
[Fact]
62+
public async Task FakeLargeFile_Send_413()
63+
{
64+
// Arrange
65+
var requestUri = "/large-requests/file";
66+
var options = _testsFixture.GetService<IOptions<ApiBehaviorOptions>>();
67+
68+
var file = new StreamContent(new MemoryStream(new byte[1_048_577]));
69+
var body = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
70+
body.Add(file, "FileFake", "upload.jpg");
71+
72+
73+
// Act
74+
(var response, var content) = await _testsFixture.Client.SendPostMultipartAsync(requestUri, body);
75+
options.Value.ClientErrorMapping.TryGetValue((int)response.StatusCode, out var clientErrorData);
76+
77+
78+
// Assert
79+
using(new AssertionScope())
80+
{
81+
response.ValidateResponse(HttpStatusCode.RequestEntityTooLarge);
82+
83+
content.ValidateContent(
84+
HttpStatusCode.RequestEntityTooLarge,
85+
clientErrorData,
86+
"POST: " + requestUri,
87+
"The payload is too big.",
88+
new Dictionary<string, ErrorDetails>()
89+
{
90+
{ "payload", new("MAX:1048576", "The payload is too big.") }
91+
});
92+
}
93+
}
94+
95+
[Fact]
96+
public async Task FakeFileWithLimitSize_Send_200()
97+
{
98+
// Arrange
99+
var requestUri = "/large-requests/file";
100+
101+
var file = new StreamContent(new MemoryStream(new byte[1_048_576]));
102+
var body = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
103+
body.Add(file, "FileFake", "upload.jpg");
104+
105+
106+
// Act
107+
(var response, _) = await _testsFixture.Client.SendPostMultipartAsync(requestUri, body);
108+
109+
110+
// Assert
111+
response.ValidateStatusCode(HttpStatusCode.OK);
112+
}
113+
}
114+
}

tests/PowerUtils.AspNetCore.ErrorHandler.Tests/Utils/HttpClientUtils.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ namespace PowerUtils.AspNetCore.ErrorHandler.Tests.Utils
1111
{
1212
internal static class HttpClientUtils
1313
{
14+
private static readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
15+
{
16+
PropertyNameCaseInsensitive = true
17+
};
18+
1419
public static async Task<(HttpResponseMessage Response, ErrorProblemDetails Content)> SendGetAsync(this HttpClient client, string endpoint, object parameters = null)
1520
{
1621
if(parameters != null)
@@ -22,8 +27,7 @@ internal static class HttpClientUtils
2227

2328
return (
2429
response,
25-
await response.DeserializeResponseAsync()
26-
);
30+
await response.DeserializeResponseAsync());
2731
}
2832

2933

@@ -34,8 +38,7 @@ await response.DeserializeResponseAsync()
3438

3539
return (
3640
response,
37-
await response.DeserializeResponseAsync()
38-
);
41+
await response.DeserializeResponseAsync());
3942
}
4043

4144
public static async Task<(HttpResponseMessage Response, ErrorProblemDetails Content)> SendPostAsync(this HttpClient client, string endpoint, object body = null)
@@ -47,8 +50,7 @@ await response.DeserializeResponseAsync()
4750

4851
return (
4952
response,
50-
await response.DeserializeResponseAsync()
51-
);
53+
await response.DeserializeResponseAsync());
5254
}
5355

5456
public static HttpRequestMessage ToPostRequest(this object body, string endpoint)
@@ -57,7 +59,6 @@ public static HttpRequestMessage ToPostRequest(this object body, string endpoint
5759
{
5860
return new HttpRequestMessage(HttpMethod.Post, endpoint)
5961
{
60-
6162
Content = JsonContent.Create(null, typeof(object), new MediaTypeHeaderValue(MediaTypeNames.Application.Json), null)
6263
};
6364
}
@@ -82,11 +83,7 @@ public static async Task<ErrorProblemDetails> DeserializeResponseAsync(this Http
8283
{
8384
return JsonSerializer.Deserialize<ErrorProblemDetails>(
8485
content,
85-
new JsonSerializerOptions
86-
{
87-
PropertyNameCaseInsensitive = true
88-
}
89-
);
86+
_jsonSerializerOptions);
9087
}
9188
catch(Exception exception)
9289
{

0 commit comments

Comments
 (0)