Skip to content

Commit e99957b

Browse files
authored
fix(request_body) :: return NULL when the request has no body' (#1392)
1 parent 6859b56 commit e99957b

3 files changed

Lines changed: 68 additions & 51 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- `column` charts now display vertical bars instead of nothing at all.
1414
- `stacked` is now ignored on chart types that cannot stack, instead of displaying an empty chart.
1515
- Screen readers now announce the title of the modal component instead of an unnamed dialog.
16+
- `sqlpage.request_body` and `sqlpage.request_body_base64` now return NULL when the request has no body. A body that cannot be read, such as one exceeding the payload limit, is now reported as an error instead of being silently replaced with an empty body.
1617

1718
## v0.45
1819

src/webserver/http_request_info.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,13 @@ async fn extract_post_data(
183183
} else {
184184
let body = actix_web::web::Bytes::from_request(http_req, payload)
185185
.await
186-
.map(|bytes| bytes.to_vec())
187-
.unwrap_or_default();
188-
Ok((Vec::new(), Vec::new(), Some(body)))
186+
.with_actix_error_status()
187+
.context("could not read the request body")?;
188+
Ok((
189+
Vec::new(),
190+
Vec::new(),
191+
(!body.is_empty()).then(|| body.to_vec()),
192+
))
189193
}
190194
}
191195

tests/requests/mod.rs

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,46 @@ use sqlpage::webserver::http::main_handler;
44

55
use crate::common::get_request_to;
66

7-
#[actix_web::test]
8-
async fn test_request_body() -> actix_web::Result<()> {
9-
let req = get_request_to("/tests/requests/request_body_test.sql")
10-
.await?
11-
.insert_header(("content-type", "text/plain"))
12-
.set_payload("Hello, world!")
13-
.to_srv_request();
7+
async fn rendered_page(req: actix_web::dev::ServiceRequest) -> actix_web::Result<String> {
148
let resp = main_handler(req).await?;
15-
169
assert_eq!(resp.status(), StatusCode::OK);
17-
let body = test::read_body(resp).await;
18-
let body_str = String::from_utf8(body.to_vec()).unwrap();
10+
Ok(String::from_utf8(test::read_body(resp).await.to_vec()).unwrap())
11+
}
12+
13+
#[actix_web::test]
14+
async fn test_request_body() -> actix_web::Result<()> {
15+
let page = rendered_page(
16+
get_request_to("/tests/requests/request_body_test.sql")
17+
.await?
18+
.insert_header(("content-type", "text/plain"))
19+
.set_payload("Hello, world!")
20+
.to_srv_request(),
21+
)
22+
.await?;
1923
assert!(
20-
body_str.contains("Hello, world!"),
21-
"{body_str}\nexpected to contain: Hello, world!"
24+
page.contains("Hello, world!"),
25+
"{page}\nexpected to contain: Hello, world!"
2226
);
2327

24-
// Test with form data - should return NULL
25-
let req = get_request_to("/tests/requests/request_body_test.sql")
26-
.await?
27-
.insert_header(("content-type", "application/x-www-form-urlencoded"))
28-
.set_payload("key=value")
29-
.to_srv_request();
30-
let resp = main_handler(req).await?;
28+
let page = rendered_page(
29+
get_request_to("/tests/requests/request_body_test.sql")
30+
.await?
31+
.insert_header(("content-type", "application/x-www-form-urlencoded"))
32+
.set_payload("key=value")
33+
.to_srv_request(),
34+
)
35+
.await?;
36+
assert!(page.contains("NULL"), "{page}\nexpected NULL for form data");
3137

32-
assert_eq!(resp.status(), StatusCode::OK);
33-
let body = test::read_body(resp).await;
34-
let body_str = String::from_utf8(body.to_vec()).unwrap();
38+
let page = rendered_page(
39+
get_request_to("/tests/requests/request_body_test.sql")
40+
.await?
41+
.to_srv_request(),
42+
)
43+
.await?;
3544
assert!(
36-
body_str.contains("NULL"),
37-
"{body_str}\nexpected NULL for form data"
45+
page.contains("NULL"),
46+
"{page}\nexpected NULL when the request has no body"
3847
);
3948
Ok(())
4049
}
@@ -45,35 +54,38 @@ async fn test_request_body_base64() -> actix_web::Result<()> {
4554
let expected_base64 =
4655
base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &binary_data);
4756

48-
let req = get_request_to("/tests/requests/request_body_base64_test.sql")
49-
.await?
50-
.insert_header(("content-type", "application/octet-stream"))
51-
.set_payload(binary_data)
52-
.to_srv_request();
53-
let resp = main_handler(req).await?;
54-
55-
assert_eq!(resp.status(), StatusCode::OK);
56-
let body = test::read_body(resp).await;
57-
let body_str = String::from_utf8(body.to_vec()).unwrap();
57+
let page = rendered_page(
58+
get_request_to("/tests/requests/request_body_base64_test.sql")
59+
.await?
60+
.insert_header(("content-type", "application/octet-stream"))
61+
.set_payload(binary_data)
62+
.to_srv_request(),
63+
)
64+
.await?;
5865
assert!(
59-
body_str.contains(&expected_base64),
60-
"{body_str}\nexpected to contain base64: {expected_base64}"
66+
page.contains(&expected_base64),
67+
"{page}\nexpected to contain base64: {expected_base64}"
6168
);
6269

63-
// Test with form data - should return NULL
64-
let req = get_request_to("/tests/requests/request_body_base64_test.sql")
65-
.await?
66-
.insert_header(("content-type", "application/x-www-form-urlencoded"))
67-
.set_payload("key=value")
68-
.to_srv_request();
69-
let resp = main_handler(req).await?;
70+
let page = rendered_page(
71+
get_request_to("/tests/requests/request_body_base64_test.sql")
72+
.await?
73+
.insert_header(("content-type", "application/x-www-form-urlencoded"))
74+
.set_payload("key=value")
75+
.to_srv_request(),
76+
)
77+
.await?;
78+
assert!(page.contains("NULL"), "{page}\nexpected NULL for form data");
7079

71-
assert_eq!(resp.status(), StatusCode::OK);
72-
let body = test::read_body(resp).await;
73-
let body_str = String::from_utf8(body.to_vec()).unwrap();
80+
let page = rendered_page(
81+
get_request_to("/tests/requests/request_body_base64_test.sql")
82+
.await?
83+
.to_srv_request(),
84+
)
85+
.await?;
7486
assert!(
75-
body_str.contains("NULL"),
76-
"{body_str}\nexpected NULL for form data"
87+
page.contains("NULL"),
88+
"{page}\nexpected NULL when the request has no body"
7789
);
7890
Ok(())
7991
}

0 commit comments

Comments
 (0)