rust 获取方法的reqwest板条箱不工作

5q4ezhmt  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(159)
pub struct TestApp {
    pub address: String,
    pub pool: PgPool,
}

async fn spawn_app() -> TestApp {
    let listener = TcpListener::bind("127.0.0.1:0").expect("failed to bind to the port");
    let port = listener.local_addr().unwrap().port();
    let address = format!("127.0.0.1:{}", port);

    let configuration = get_configuration().expect("failed to read configuration");
    let connection_pool = PgPool::connect(&configuration.database.connection_string()).await.expect("failed to connect to postgres");
    let server = run(listener, connection_pool.clone()).expect("failed to bind to address");
    let _ = tokio::spawn(server);
    TestApp{
        address,
        pool: connection_pool,
    }
}

我在遵循零到生产的原则。这个测试函数以前工作过,现在我运行cargo test,我得到以下错误。

#[tokio::test]
async fn health_check_works() {
    let app = spawn_app().await;
    let client = reqwest::Client::new();
    let response = client
        .get(&format!("{}/health_check",&app.address))
        .send()
        .await
        .expect("Failed to execute request.");
    assert!(response.status().is_success());
    assert_eq!(Some(0), response.content_length());
}
thread 'health_check_works' panicked at 'Failed to execute request.: reqwest::Error { kind: Builder, source: RelativeUrlWithoutBase }', tests/health_check.rs:36:10

问题是什么,我该如何解决?

r3i60tvu

r3i60tvu1#

URL必须以协议(http://https://)开头:

.get(&format!("http://{}/health_check",&app.address))

相关问题