reqwest库的get方法不起作用。

huangapple go评论54阅读模式
英文:

get method of the reqwest crate not working

问题

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,
    }
}
#[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());
}

The code has been translated.

英文:
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,
    }
}

I am following the zero to production book.
This test function used to work before and now i run cargo test i get the following error.

#[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

What is the issue and how do i solve it?

答案1

得分: 2

The URL must start with a protocol (http:// or https://):

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

The URL must start with a protocol (http:// or https://):

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

huangapple
  • 本文由 发表于 2023年5月24日 19:40:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定