Skip to content

Commit a59f23e

Browse files
committed
test: dispatch integration helpers through the production app
Use create_app inside the shared request helper so existing concise test calls exercise production routes and middleware. Update the prefix redirect assertion to check the actual 308 response and preserved request path.
1 parent 49be0a0 commit a59f23e

3 files changed

Lines changed: 13 additions & 25 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ cargo test
9999
Use `common::response_for(path)` for ordinary integration requests and
100100
`common::response_with(path, config)` for custom configuration. Custom requests use
101101
`common::response_from(request)`; tests sharing database or cache state use `common::send_request`.
102-
These helpers dispatch through an Actix test service whose destructor drains its request pool.
102+
These helpers use the production `create_app` routes and middleware through an Actix test service,
103+
whose destructor drains its request pool.
103104
Keep `#[actix_web::test]`; standalone `TestRequest::to_srv_request()` retains application state.
104105
When testing Oracle locally, use `cargo test -- --test-threads=2` to avoid overwhelming the listener.
105106

‎tests/common/mod.rs‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use sqlpage::{
1414
AppState,
1515
app_config::{AppConfig, test_database_url},
1616
telemetry,
17-
webserver::http::{form_config, main_handler, payload_config},
17+
webserver::http::create_app,
1818
};
1919
use tokio::sync::oneshot;
2020
use tokio::task::JoinHandle;
@@ -71,27 +71,21 @@ pub(crate) async fn response_with_data(
7171
send_request(request_for(path), data).await
7272
}
7373

74-
/// Runs a request through an Actix service so its request pool has an owner.
74+
/// Runs a request through the production app, including its routes and middleware.
7575
/// Dropping the service disables and drains the pool, including on error or panic.
7676
/// Standalone `TestRequest::to_srv_request()` would instead leak the application state.
7777
pub(crate) async fn send_request(
7878
request: TestRequest,
7979
data: Data<AppState>,
8080
) -> actix_web::Result<ServiceResponse> {
81-
let app = test::init_service(
82-
App::new()
83-
.app_data(payload_config(&data))
84-
.app_data(form_config(&data))
85-
.app_data(data)
86-
.default_service(fn_service(main_handler)),
87-
)
88-
.await;
81+
let app = test::init_service(create_app(data)).await;
8982
tokio::time::timeout(
9083
Duration::from_secs(8),
9184
test::try_call_service(&app, request.to_request()),
9285
)
9386
.await
9487
.map_err(actix_web::error::ErrorGatewayTimeout)?
88+
.map(ServiceResponse::map_into_boxed_body)
9589
}
9690

9791
/// Reads a whole response body as a UTF-8 string, failing the test otherwise.

‎tests/core/mod.rs‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,10 @@ async fn test_routing_with_prefix() {
148148
let mut config = test_config();
149149
config.site_prefix = "/prefix/".to_string();
150150
let app_data = make_app_data_from_config(config).await.unwrap();
151-
let resp = response_with_data(
152-
"/prefix/tests/sql_test_files/component_rendering/simple.sql",
153-
app_data.clone(),
154-
)
155-
.await
156-
.unwrap();
151+
let path = "/tests/sql_test_files/component_rendering/simple.sql";
152+
let resp = response_with_data(format!("/prefix{path}"), app_data.clone())
153+
.await
154+
.unwrap();
157155
assert_eq!(resp.status(), StatusCode::OK);
158156
let body_str = crate::common::read_body_string(resp).await;
159157
assert!(
@@ -181,18 +179,13 @@ async fn test_routing_with_prefix() {
181179
.status_code();
182180
assert_eq!(resp, StatusCode::FORBIDDEN);
183181

184-
let resp = response_with_data(
185-
"/tests/sql_test_files/component_rendering/simple.sql",
186-
app_data,
187-
)
188-
.await
189-
.unwrap();
190-
assert_eq!(resp.status(), StatusCode::MOVED_PERMANENTLY);
182+
let resp = response_with_data(path, app_data).await.unwrap();
183+
assert_eq!(resp.status(), StatusCode::PERMANENT_REDIRECT);
191184
let location = resp
192185
.headers()
193186
.get("location")
194187
.expect("location header should be present");
195-
assert_eq!(location.to_str().unwrap(), "/prefix/");
188+
assert_eq!(location.to_str().unwrap(), format!("/prefix{path}"));
196189
}
197190

198191
#[actix_web::test]

0 commit comments

Comments
 (0)