Skip to main content

server_with

Function server_with 

Source
pub fn server_with<F, I, Sf>(cfg: TestServerConfig, factory: F) -> TestServer
where F: AsyncFn(&()) -> I + Send + Clone + 'static, I: IntoServiceFactory<Sf, (), Request>, Sf: ServiceFactory<(), Request> + 'static, Sf::Res: Into<Response>, Sf::Error: ResponseError + 'static, Sf::InitError: Debug,
Expand description

Start test server with custom configuration

Test server could be configured in different ways, for details check TestServerConfig docs.

ยงExamples

use ntex::web::{self, test, App, HttpResponse};

async fn my_handler() -> HttpResponse {
    HttpResponse::Ok().into()
}

#[ntex::test]
async fn test_example() {
    let srv = test::server_with(test::config().h1(), async |_|
        App::new().service(web::resource("/").to(my_handler))
    );

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