Skip to content

Commit c4ac5da

Browse files
committed
fix error message on invalid header-only component usage
fixes #101
1 parent 0a93d81 commit c4ac5da

File tree

8 files changed

+55
-10
lines changed

8 files changed

+55
-10
lines changed

src/render.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,14 @@ impl<W: std::io::Write> RenderContext<W> {
338338
format!("Unable to render dynamic component with properties {data}")
339339
})?;
340340
}
341-
(_, Some("http_header")) => {
342-
bail!("The http_header component can not be used in the body of the page, only as the very first component in the page. \
341+
(
342+
_,
343+
Some(
344+
component_name @ ("status_code" | "http_header" | "redirect" | "json"
345+
| "cookie" | "authentication"),
346+
),
347+
) => {
348+
bail!("The {component_name} component cannot be used in the body of the page, only as the very first component in the page. \
343349
The HTTP headers have already be sent for the current page, they cannot be changed now.");
344350
}
345351
(_current_component, Some(new_component)) => {

tests/index.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,44 @@ async fn test_404() {
4444
}
4545

4646
#[actix_web::test]
47-
async fn test_files_it_works() {
47+
async fn test_files() {
4848
// Iterate over all the sql test files in the tests/ directory
49-
for entry in std::fs::read_dir("tests").unwrap() {
49+
let path = std::path::Path::new("tests/sql_test_files");
50+
for entry in std::fs::read_dir(path).unwrap() {
5051
let entry = entry.unwrap();
51-
let path = entry.path();
52-
if path.extension().unwrap_or_default() != "sql" {
52+
let test_file_path = entry.path();
53+
let stem = test_file_path.file_stem().unwrap().to_str().unwrap();
54+
if test_file_path.extension().unwrap_or_default() != "sql" {
5355
continue;
5456
}
55-
let path = format!("/{}?x=1", path.display());
56-
let resp = req_path(&path).await.unwrap();
57+
let req_str = format!("/{}?x=1", test_file_path.display());
58+
let resp = req_path(&req_str).await.unwrap();
5759
let body = test::read_body(resp).await;
5860
assert!(body.starts_with(b"<!DOCTYPE html>"));
5961
// the body should contain the strint "It works!" and should not contain the string "error"
6062
let body = String::from_utf8(body.to_vec()).unwrap();
61-
assert!(body.contains("It works !"), "{path}: {body}");
62-
assert!(!body.contains("error"), "{body}");
63+
let lowercase_body = body.to_lowercase();
64+
if stem.starts_with("it_works") {
65+
assert!(
66+
body.contains("It works !"),
67+
"{req_str}\n{body}\nexpected to contain: It works !"
68+
);
69+
assert!(
70+
!lowercase_body.contains("error"),
71+
"{body}\nexpected to not contain: error"
72+
);
73+
} else if stem.starts_with("error_") {
74+
let rest = stem.strip_prefix("error_").unwrap();
75+
let expected_str = rest.replace("_", " ");
76+
assert!(
77+
lowercase_body.contains(&expected_str),
78+
"{req_str}\n{body}\nexpected to contain: {expected_str}"
79+
);
80+
assert!(
81+
lowercase_body.contains("error"),
82+
"{req_str}\n{body}\nexpected to contain: error"
83+
);
84+
}
6385
}
6486
}
6587

tests/sql_test_files/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
The sql files in this folder are all tested automatically.
2+
3+
## `it_works_` files
4+
5+
Files with names starting with `it_works` should all
6+
return a page that contains the text "It works !" and does not contain the
7+
text "error" (case insensitive) when executed.
8+
9+
## `error_` files
10+
11+
Files with names starting with `error` should all return a page that contains
12+
the text "error" and the rest of the file name when executed.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
select 'text' as component;
2+
select 'hello world' as contents;
3+
select 'cookie' as component,
4+
'username' as name,
5+
'John Doe' as value;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)