actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

Actix web v4: Outdated example for test_server.

Open staninprague opened this issue 3 years ago • 1 comments

Expected Behavior

Example for the test_server needs to provide the correct way of using test_service for actix-web v4. Current example also uses App.new() when creating a test server, which is good to have an example of.

Current Behavior

Example for the test_server seems to be outdated.

Current example:

/// # Examples
/// ```no_run
/// use actix_http::HttpService;
/// use actix_http_test::test_server;
/// use actix_web::{web, App, HttpResponse, Error};
///
/// async fn my_handler() -> Result<HttpResponse, Error> {
///     Ok(HttpResponse::Ok().into())
/// }
///
/// #[actix_web::test]
/// async fn test_example() {
///     let mut srv = TestServer::start(||
///         HttpService::new(
///             App::new().service(web::resource("/").to(my_handler))
///         )
///     );
///
///     let req = srv.get("/");
///     let response = req.send().await.unwrap();
///     assert!(response.status().is_success());
/// }
/// ```

Possible Solution

Attaching file with a new test_example, not sure if it is the best solution, only compiles and works:

fix_for_actix_web_test_serverdoc_testv4.txt

Excerpt from the file, still using App.new():

    use actix_http::HttpService;
    use actix_http_test::test_server;
    use actix_service::{map_config, ServiceFactoryExt};
    use actix_web::{HttpResponse, App, web, dev::AppConfig, Error};

    async fn my_handler() -> Result<HttpResponse, Error> {
        Ok(HttpResponse::Ok().into())
    }

    #[actix_web::test]
    async fn test_example() {
        let srv = test_server(|| {
            HttpService::build()
                .h1(map_config(
                    App::new().service(web::resource("/").to(my_handler)),
                    |_| AppConfig::default(),
                ))
                .tcp()
                .map_err(|_| ())
        })
        .await;

        let req = srv.get("/");
        let response = req.send().await.unwrap();
        assert!(response.status().is_success());
    }

If this solution is wrong, or if there is better or more concise example, would love to know about it and learn.

  • Rust Version (I.e, output of rustc -V): rustc 1.57.0 (f1edd0429 2021-11-29)
  • Actix Web Version: 4.0.0-beta.13

staninprague avatar Dec 08 '21 15:12 staninprague

I'd like to do this work. Can you assign this work to me?

zenmiao7 avatar Mar 17 '22 16:03 zenmiao7

@BlackSmith96 are you still available to do this? If no, @robjtede please assign me to it

tltsutltsu avatar Mar 30 '23 05:03 tltsutltsu