Repository navigation
Expand file tree
/
Copy pathRequestBodyLimitClusterTests.cs
More file actions
55 lines (52 loc) · 2.84 KB
/
Copy pathRequestBodyLimitClusterTests.cs
File metadata and controls
55 lines (52 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Net.Http.Headers;
using System.Net.Http.Json;
using Aspire.Hosting.Testing;
using KeyLoad.ClusterTests.ClientApi;
namespace KeyLoad.ClusterTests;
[NotInParallel]
internal sealed class RequestBodyLimitClusterTests
{
private const string PartitionKey = "partition";
private const string CommandIdHeader = "X-KeyLoad-Command-Id";
private const int MaximumBodyBytes = 1_024;
[Test]
public async Task OversizedDeclaredAndChunkedBodiesAreRejectedBeforeCommandsClaimTheirIds()
{
var fixture = new ClusterFixture(maximumBodyBytes: MaximumBodyBytes);
using var timeout = McpCallerDeadline.Create();
try
{
await fixture.InitializeAsync();
using var http = fixture.App.CreateHttpClient("node1", "https");
foreach (var chunked in new[] { false, true })
{
var id = Guid.NewGuid();
var payload = new DeliveryCommand(id, new(new("tenant", "database", "orders", PartitionKey), "jobs"),
new string('t', 2_000), DeliveryAction.Ack);
using var request = new HttpRequestMessage(HttpMethod.Post, "/queues/delivery")
{
Content = new ByteArrayContent(JsonDefaults.Serialize(payload))
};
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", fixture.AdminKey);
if (chunked)
{
request.Headers.TransferEncodingChunked = true;
}
using var rejected = await http.SendAsync(request, timeout.Token);
await Assert.That(rejected.StatusCode).IsEqualTo(System.Net.HttpStatusCode.TooManyRequests);
await Assert.That(rejected.Headers.TransferEncodingChunked is true).IsFalse();
await Assert.That(rejected.Content.Headers.Contains("Content-Length")).IsTrue();
var problem = await rejected.Content.ReadFromJsonAsync<ManagedCode.Communication.Problem>(JsonDefaults.Options, timeout.Token);
await Assert.That(problem!.ErrorCode).IsEqualTo(nameof(ErrorCode.ResourceExhausted));
using var control = new HttpRequestMessage(HttpMethod.Post, "/admin/dispatch?paused=false");
control.Headers.Authorization = new AuthenticationHeaderValue("Bearer", fixture.AdminKey);
control.Headers.Add(CommandIdHeader, id.ToString());
using var completed = await http.SendAsync(control, timeout.Token);
completed.EnsureSuccessStatusCode();
await Assert.That(await completed.Content.ReadFromJsonAsync<bool>(JsonDefaults.Options, timeout.Token)).IsTrue();
}
}
finally { await fixture.DisposeAsync(); }
}
}