reqwest库的get方法不起作用。

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

get method of the reqwest crate not working

问题

  1. pub struct TestApp {
  2. pub address: String,
  3. pub pool: PgPool,
  4. }
  5. async fn spawn_app() -> TestApp {
  6. let listener = TcpListener::bind("127.0.0.1:0").expect("failed to bind to the port");
  7. let port = listener.local_addr().unwrap().port();
  8. let address = format!("127.0.0.1:{}", port);
  9. let configuration = get_configuration().expect("failed to read configuration");
  10. let connection_pool = PgPool::connect(&configuration.database.connection_string())
  11. .await
  12. .expect("failed to connect to postgres");
  13. let server = run(listener, connection_pool.clone()).expect("failed to bind to address");
  14. let _ = tokio::spawn(server);
  15. TestApp {
  16. address,
  17. pool: connection_pool,
  18. }
  19. }
  1. #[tokio::test]
  2. async fn health_check_works() {
  3. let app = spawn_app().await;
  4. let client = reqwest::Client::new();
  5. let response = client
  6. .get(&format!("{}/health_check", &app.address))
  7. .send()
  8. .await
  9. .expect("Failed to execute request.");
  10. assert!(response.status().is_success());
  11. assert_eq!(Some(0), response.content_length());
  12. }

The code has been translated.

英文:
  1. pub struct TestApp {
  2. pub address: String,
  3. pub pool: PgPool,
  4. }
  5. async fn spawn_app() -> TestApp {
  6. let listener = TcpListener::bind("127.0.0.1:0").expect("failed to bind to the port");
  7. let port = listener.local_addr().unwrap().port();
  8. let address = format!("127.0.0.1:{}", port);
  9. let configuration = get_configuration().expect("failed to read configuration");
  10. let connection_pool = PgPool::connect(&configuration.database.connection_string()).await.expect("failed to connect to postgres");
  11. let server = run(listener, connection_pool.clone()).expect("failed to bind to address");
  12. let _ = tokio::spawn(server);
  13. TestApp{
  14. address,
  15. pool: connection_pool,
  16. }
  17. }

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.

  1. #[tokio::test]
  2. async fn health_check_works() {
  3. let app = spawn_app().await;
  4. let client = reqwest::Client::new();
  5. let response = client
  6. .get(&format!("{}/health_check",&app.address))
  7. .send()
  8. .await
  9. .expect("Failed to execute request.");
  10. assert!(response.status().is_success());
  11. assert_eq!(Some(0), response.content_length());
  12. }
  1. 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://):

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

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

  1. .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:

确定