Repository navigation
Expand file tree
/
Copy pathmod.rs
More file actions
192 lines (174 loc) · 6.2 KB
/
Copy pathmod.rs
File metadata and controls
192 lines (174 loc) · 6.2 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
use std::fmt::Write as _;
use std::time::Duration;
use actix_web::{
App, HttpResponse, HttpServer,
dev::{ServiceRequest, ServiceResponse, fn_service},
http::header,
http::header::ContentType,
test::{self, TestRequest},
web,
web::Data,
};
use sqlpage::{
AppState,
app_config::{AppConfig, test_database_url},
telemetry,
webserver::http::create_app,
};
use tokio::sync::oneshot;
use tokio::task::JoinHandle;
/// Builds a GET request with the defaults used by the SQL fixtures.
pub(crate) fn request_for(path: impl AsRef<str>) -> TestRequest {
TestRequest::get()
.uri(path.as_ref())
.insert_header(ContentType::plaintext())
.insert_header(header::Accept::html())
}
pub(crate) fn multipart_request(path: &str, body: impl Into<web::Bytes>) -> TestRequest {
request_for(path)
.insert_header(("content-type", "multipart/form-data; boundary=1234567890"))
.set_payload(body)
}
pub(crate) async fn make_app_data_from_config(
config: AppConfig,
) -> actix_web::Result<Data<AppState>> {
init_log();
AppState::init(&config)
.await
.map(Data::new)
.map_err(actix_web::error::ErrorInternalServerError)
}
/// Sends a GET request with the default test configuration, failing the test on error.
pub(crate) async fn response_for(path: impl AsRef<str>) -> ServiceResponse {
response_with(path.as_ref(), test_config())
.await
.unwrap_or_else(|err| panic!("Request to {} failed: {err:#}", path.as_ref()))
}
/// Sends a GET request with custom application configuration.
pub(crate) async fn response_with(
path: impl AsRef<str>,
config: AppConfig,
) -> actix_web::Result<ServiceResponse> {
send_request(request_for(path), make_app_data_from_config(config).await?).await
}
/// Sends a custom request with the default test configuration.
pub(crate) async fn response_from(request: TestRequest) -> actix_web::Result<ServiceResponse> {
send_request(request, make_app_data_from_config(test_config()).await?).await
}
/// Sends a GET request using existing state, for tests that share a database or cache.
pub(crate) async fn response_with_data(
path: impl AsRef<str>,
data: Data<AppState>,
) -> actix_web::Result<ServiceResponse> {
send_request(request_for(path), data).await
}
/// Runs a request through the production app, including its routes and middleware.
/// Dropping the service disables and drains the pool, including on error or panic.
/// Standalone `TestRequest::to_srv_request()` would instead leak the application state.
pub(crate) async fn send_request(
request: TestRequest,
data: Data<AppState>,
) -> actix_web::Result<ServiceResponse> {
let app = test::init_service(create_app(data)).await;
tokio::time::timeout(
Duration::from_secs(8),
test::try_call_service(&app, request.to_request()),
)
.await
.map_err(actix_web::error::ErrorGatewayTimeout)?
.map(ServiceResponse::map_into_boxed_body)
}
/// Reads a whole response body as a UTF-8 string, failing the test otherwise.
pub(crate) async fn read_body_string<B>(resp: ServiceResponse<B>) -> String
where
B: actix_web::body::MessageBody,
{
String::from_utf8(test::read_body(resp).await.to_vec()).unwrap()
}
/// Whether the database engine is one of `kinds`.
/// Tests that only run on some engines return early otherwise.
pub(crate) fn supports_database(
db: &sqlpage::webserver::database::Database,
kinds: &[sqlpage::webserver::database::SupportedDatabase],
) -> bool {
kinds.contains(&db.info.database_type)
}
pub(crate) async fn make_app_data() -> Data<AppState> {
make_app_data_from_config(test_config()).await.unwrap()
}
pub(crate) fn test_config() -> AppConfig {
serde_json::from_value(serde_json::json!({
"database_url": test_database_url(),
"max_database_pool_connections": 1,
"database_connection_retries": 3,
"database_connection_acquire_timeout_seconds": 15,
"allow_exec": true,
"max_uploaded_file_size": 123_456,
"listen_on": "111.111.111.111:1",
"system_root_ca_certificates": false
}))
.unwrap()
}
pub(crate) fn init_log() {
telemetry::init_test_logging();
}
fn format_request_line_and_headers(req: &ServiceRequest) -> String {
let mut out = format!("{} {}", req.method(), req.uri());
let mut headers: Vec<_> = req.headers().iter().collect();
headers.sort_by_key(|(k, _)| k.as_str());
for (k, v) in headers {
if k.as_str().eq_ignore_ascii_case("date") {
continue;
}
write!(out, "|{k}: {}", v.to_str().unwrap_or("?")).unwrap();
}
out
}
async fn format_body(req: &mut ServiceRequest) -> Vec<u8> {
req.extract::<web::Bytes>()
.await
.map(|b| b.to_vec())
.unwrap_or_default()
}
fn build_echo_response(body: &[u8], meta: String) -> HttpResponse {
let mut resp = meta.into_bytes();
resp.push(b'|');
resp.extend_from_slice(body);
HttpResponse::Ok()
.insert_header((header::DATE, "Mon, 24 Feb 2025 12:00:00 GMT"))
.insert_header((header::CONTENT_TYPE, "text/plain"))
.body(resp)
}
pub(crate) fn start_echo_server(shutdown: oneshot::Receiver<()>) -> (JoinHandle<()>, u16) {
let listener = std::net::TcpListener::bind("localhost:0").unwrap();
let port = listener.local_addr().unwrap().port();
let server = HttpServer::new(|| {
App::new()
.route(
"/json",
web::to(|body: web::Bytes| async move {
HttpResponse::Ok()
.insert_header((header::CONTENT_TYPE, "application/json"))
.body(body)
}),
)
.default_service(fn_service(|mut req: ServiceRequest| async move {
let meta = format_request_line_and_headers(&req);
let body = format_body(&mut req).await;
let resp = build_echo_response(&body, meta);
Ok(req.into_response(resp))
}))
})
.workers(1)
.listen(listener)
.unwrap()
.shutdown_timeout(1)
.run();
let handle = tokio::spawn(async move {
tokio::select! {
_ = server => {},
_ = shutdown => {},
}
});
(handle, port)
}