-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathmod.rs
More file actions
194 lines (180 loc) · 5.92 KB
/
mod.rs
File metadata and controls
194 lines (180 loc) · 5.92 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use actix_web::{
http::{header, StatusCode},
test::{self, TestRequest},
};
use sqlpage::webserver::http::main_handler;
use crate::common::{get_request_to, make_app_data};
async fn req_with_accept(
path: &str,
accept: &str,
) -> actix_web::Result<actix_web::dev::ServiceResponse> {
let app_data = make_app_data().await;
let req = TestRequest::get()
.uri(path)
.insert_header((header::ACCEPT, accept))
.app_data(app_data)
.to_srv_request();
main_handler(req).await
}
#[actix_web::test]
async fn test_json_body() -> actix_web::Result<()> {
let req = get_request_to("/tests/data_formats/json_data.sql")
.await?
.to_srv_request();
let resp = main_handler(req).await?;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),
"application/json"
);
let body_json: serde_json::Value = test::read_body_json(resp).await;
assert_eq!(
body_json,
serde_json::json!([{"message": "It works!"}, {"cool": "cool"}])
);
Ok(())
}
#[actix_web::test]
async fn test_csv_body() -> actix_web::Result<()> {
let app_data = make_app_data().await;
if matches!(
app_data.db.info.database_type,
sqlpage::webserver::database::SupportedDatabase::Oracle
) {
return Ok(());
}
let req = crate::common::get_request_to_with_data("/tests/data_formats/csv_data.sql", app_data)
.await?
.to_srv_request();
let resp = main_handler(req).await?;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),
"text/csv; charset=utf-8"
);
let body = test::read_body(resp).await;
let body_str = String::from_utf8(body.to_vec()).unwrap();
assert_eq!(
body_str,
"id;msg\n0;Hello World !\n1;\"Tu gères ';' et '\"\"' ?\"\n"
);
Ok(())
}
#[actix_web::test]
async fn test_json_columns() {
let app_data = crate::common::make_app_data().await;
if !matches!(
app_data.db.to_string().to_lowercase().as_str(),
"postgres" | "sqlite"
) {
log::info!("Skipping test_json_columns on database {}", app_data.db);
return;
}
let resp_result = crate::common::req_path("/tests/data_formats/json_columns.sql").await;
let resp = resp_result.expect("Failed to request /tests/data_formats/json_columns.sql");
assert_eq!(resp.status(), StatusCode::OK);
let body = test::read_body(resp).await;
let body_str = String::from_utf8(body.to_vec()).unwrap();
let body_html_escaped = body_str.replace(""", "\"");
assert!(
!body_html_escaped.contains("error"),
"the request should not have failed, in: {body_html_escaped}"
);
assert!(body_html_escaped.contains("1GB Database"));
assert!(body_html_escaped.contains("Priority Support"));
assert!(
!body_html_escaped.contains("\"description\""),
"the json should have been parsed, not returned as a string, in: {body_html_escaped}"
);
assert!(
!body_html_escaped.contains("{"),
"the json should have been parsed, not returned as a string, in: {body_html_escaped}"
);
}
#[actix_web::test]
async fn test_accept_json_returns_json_array() -> actix_web::Result<()> {
let resp = req_with_accept(
"/tests/sql_test_files/component_rendering/simple.sql",
"application/json",
)
.await?;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),
"application/json"
);
let body_json: serde_json::Value = test::read_body_json(resp).await;
assert!(body_json.is_array());
let arr = body_json.as_array().unwrap();
assert!(arr.len() >= 2);
assert_eq!(arr[0]["component"], "shell");
assert_eq!(arr[1]["component"], "text");
Ok(())
}
#[actix_web::test]
async fn test_accept_ndjson_returns_jsonlines() -> actix_web::Result<()> {
let resp = req_with_accept(
"/tests/sql_test_files/component_rendering/simple.sql",
"application/x-ndjson",
)
.await?;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),
"application/x-ndjson"
);
let body = test::read_body(resp).await;
let body_str = String::from_utf8(body.to_vec()).unwrap();
let lines: Vec<&str> = body_str.trim().lines().collect();
assert!(lines.len() >= 2);
assert_eq!(
serde_json::from_str::<serde_json::Value>(lines[0]).unwrap()["component"],
"shell"
);
assert_eq!(
serde_json::from_str::<serde_json::Value>(lines[1]).unwrap()["component"],
"text"
);
Ok(())
}
#[actix_web::test]
async fn test_accept_html_returns_html() -> actix_web::Result<()> {
let resp = req_with_accept(
"/tests/sql_test_files/component_rendering/simple.sql",
"text/html",
)
.await?;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),
"text/html; charset=utf-8"
);
let body = test::read_body(resp).await;
assert!(body.starts_with(b"<!DOCTYPE html>"));
Ok(())
}
#[actix_web::test]
async fn test_accept_wildcard_returns_html() -> actix_web::Result<()> {
let resp = req_with_accept(
"/tests/sql_test_files/component_rendering/simple.sql",
"*/*",
)
.await?;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),
"text/html; charset=utf-8"
);
Ok(())
}
#[actix_web::test]
async fn test_accept_json_redirect_still_works() -> actix_web::Result<()> {
let resp =
req_with_accept("/tests/server_timing/redirect_test.sql", "application/json").await?;
assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!(
resp.headers().get(header::LOCATION).unwrap(),
"/destination.sql"
);
Ok(())
}