| 1 |
use axum::http::StatusCode; |
| 2 |
|
| 3 |
use crate::harness::TestHarness; |
| 4 |
|
| 5 |
#[tokio::test] |
| 6 |
async fn write_endpoints_rate_limited() { |
| 7 |
let mut h = TestHarness::new().await; |
| 8 |
let user_id = h.login_as("ratelimituser").await; |
| 9 |
let comm_id = h.create_community("RL Test", "rl-test").await; |
| 10 |
let _cat_id = h.create_category(comm_id, "General", "general").await; |
| 11 |
h.add_membership(user_id, comm_id, "member").await; |
| 12 |
|
| 13 |
|
| 14 |
h.client.get("/p/rl-test/general/new").await; |
| 15 |
|
| 16 |
|
| 17 |
for i in 0..10 { |
| 18 |
let body = format!("title=Thread+{i}&body=Body+{i}"); |
| 19 |
let resp = h.client.post_form("/p/rl-test/general/new", &body).await; |
| 20 |
assert_ne!( |
| 21 |
resp.status, |
| 22 |
StatusCode::TOO_MANY_REQUESTS, |
| 23 |
"Request {i} should not be rate limited" |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
let resp = h |
| 29 |
.client |
| 30 |
.post_form("/p/rl-test/general/new", "title=Overflow&body=Nope") |
| 31 |
.await; |
| 32 |
assert_eq!( |
| 33 |
resp.status, |
| 34 |
StatusCode::TOO_MANY_REQUESTS, |
| 35 |
"Request 11 should be rate limited (429)" |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
#[tokio::test] |
| 40 |
async fn get_endpoints_not_rate_limited() { |
| 41 |
let mut h = TestHarness::new().await; |
| 42 |
let _comm_id = h.create_community("ReadTest", "read-test").await; |
| 43 |
|
| 44 |
|
| 45 |
for i in 0..15 { |
| 46 |
let resp = h.client.get("/p/read-test").await; |
| 47 |
assert_ne!( |
| 48 |
resp.status, |
| 49 |
StatusCode::TOO_MANY_REQUESTS, |
| 50 |
"GET request {i} should never be rate limited" |
| 51 |
); |
| 52 |
} |
| 53 |
} |
| 54 |
|