Testing 使用actix web的工作POST路由进行测试时超时

Testing 使用actix web的工作POST路由进行测试时超时,testing,rust,rust-actix,Testing,Rust,Rust Actix,我已成功地将web应用程序从迁移到。通过使用postman,我已经验证了相同的请求会产生相同的响应。然而,迁移测试被证明有点棘手 我的帖子在运行cargo test时请求超时,即使应用程序在运行cargo run时只是接受并返回预期的响应 #[cfg(test)] mod test { use actix_web::{client, http, test, App}; #[test] fn main() { let mut ts = TestSuite:

我已成功地将web应用程序从迁移到。通过使用postman,我已经验证了相同的请求会产生相同的响应。然而,迁移测试被证明有点棘手

我的帖子在运行
cargo test
时请求超时,即使应用程序在运行
cargo run
时只是接受并返回预期的响应

#[cfg(test)]
mod test {
    use actix_web::{client, http, test, App};

    #[test]
    fn main() {
        let mut ts = TestSuite::new();

        assert!(ts.get_request("/health").status().is_success()); // works

        let filter_test_body =
            r#"{"key_1":[{"id":"a string"},{"id":"another string"}],"int_1":100}"#;
        assert!(
            ts.post_request("/post_route", filter_test_body)
                .status()
                .is_success()
        ); // times out while actual app returns success with same payload within a few ms
    }

    fn create_app() -> App<project_name::AppState> {
        // {...} some initialization and defining app_state

        return App::with_state(&app_state)
            .resource("/health", |r| {
                r.method(http::Method::GET).f(project_name::health)
            })
            .resource("/post_route", |r| {
                r.method(http::Method::POST)
                    .with(project_name::post_route_handler)
            });
    }

    struct TestSuite {
        server: test::TestServer,
    }

    impl TestSuite {
        pub fn new() -> TestSuite {
            TestSuite {
                server: test::TestServer::with_factory(create_app),
            }
        }
        pub fn get_request(&mut self, endpoint: &str) -> client::ClientResponse {
            let request = self.server
                .client(http::Method::GET, endpoint)
                .finish()
                .unwrap();

            self.server.execute(request.send()).unwrap()
        }
        pub fn post_request(&mut self, endpoint: &str, body: &str) -> client::ClientResponse {
            let request_payload: other_lib::request::RequestPayload =
                serde_json::from_str(body).unwrap();
            let request = self.server
                .client(http::Method::POST, endpoint)
                .header(http::header::CONTENT_TYPE, "application/json")
                .json(request_payload)
                //.body(body.to_string())
                .unwrap();

            self.server.execute(request.send()).unwrap() // thread 'test::main' panicked at 'called `Result::unwrap()` on an `Err` value: Timeout'
        }
    }
}

解决办法很简单。事实证明,请求确实在
货物测试时超时。一个可能的解决方案是将
timeout
方法加入到链中,或者在我的例子中更简单,通过传递
--release
标志来启用运行应用程序时使用的相同优化

#[test]
fn test_filters() {
    let client = mount_test_rocket(); // getting access to the test server (rocket::local::Client)
    let mut response = client.post("/post_route").header(ContentType::JSON)
        .body(r#"{"key_1":[{"id":"a string"},{"id":"another string"}],"int_1":100}"#).dispatch();
    assert_eq!(response.status(), Status::Ok);
    // + more assertions looking into the actual response body
}