diff --git a/src/web.rs b/src/web.rs index beb1a2b1..33a65610 100644 --- a/src/web.rs +++ b/src/web.rs @@ -438,26 +438,42 @@ pub mod test { uri: String, headers: http::HeaderMap, extensions: http::Extensions, + body: String, } impl TestRequest { /// Crea una petición GET. pub fn get() -> Self { - Self { - method: http::Method::GET, - uri: "/".to_owned(), - headers: http::HeaderMap::new(), - extensions: http::Extensions::new(), - } + Self::with_method(http::Method::GET) } /// Crea una petición POST. pub fn post() -> Self { + Self::with_method(http::Method::POST) + } + + /// Crea una petición PUT. + pub fn put() -> Self { + Self::with_method(http::Method::PUT) + } + + /// Crea una petición PATCH. + pub fn patch() -> Self { + Self::with_method(http::Method::PATCH) + } + + /// Crea una petición DELETE. + pub fn delete() -> Self { + Self::with_method(http::Method::DELETE) + } + + fn with_method(method: http::Method) -> Self { Self { - method: http::Method::POST, + method, uri: "/".to_owned(), headers: http::HeaderMap::new(), extensions: http::Extensions::new(), + body: String::new(), } } @@ -481,6 +497,14 @@ pub mod test { self } + /// Envía como cuerpo los campos de un formulario, ya codificados (p. ej. + /// `"title=Plant+lettuces&done=on"`), con la cabecera + /// `Content-Type: application/x-www-form-urlencoded`, como lo envía un navegador. + pub fn form(mut self, fields: impl Into) -> Self { + self.body = fields.into(); + self.header("content-type", "application/x-www-form-urlencoded") + } + /// Inserta un valor en las extensiones de la petición. /// /// Útil para simular lo que un middleware haría en producción antes de que el handler @@ -495,7 +519,7 @@ pub mod test { let mut req = http::Request::builder() .method(self.method) .uri(self.uri) - .body(Body::empty()) + .body(Body::from(self.body)) .unwrap(); *req.headers_mut() = self.headers; *req.extensions_mut() = self.extensions; diff --git a/tests/web_test_request.rs b/tests/web_test_request.rs new file mode 100644 index 00000000..014c8b7e --- /dev/null +++ b/tests/web_test_request.rs @@ -0,0 +1,72 @@ +use pagetop::prelude::*; + +use serde::Deserialize; + +// Every method of `web::test::TestRequest` reaches the route registered for it, and `form()` sends +// the fields as a browser does. + +#[derive(Deserialize)] +struct Item { + name: String, +} + +async fn echo(web::Form(item): web::Form) -> String { + item.name +} + +struct Echo; + +#[async_trait] +impl Extension for Echo { + fn configure_router(&self, router: Router) -> Router { + router.route( + "/item", + web::get(|| async { "get" }) + .post(echo) + .put(echo) + .patch(echo) + .delete(|| async { "delete" }), + ) + } +} + +async fn send(request: web::test::TestRequest) -> (web::http::StatusCode, String) { + let app = web::test::init_router(Application::prepare(&Echo).await.test()); + let response = web::test::send_request(&app, request.uri("/item").to_request()).await; + let status = response.status(); + (status, web::test::read_body_text(response).await) +} + +#[pagetop::test] +async fn get_and_delete_reach_their_routes() { + let (status, body) = send(web::test::TestRequest::get()).await; + assert_eq!((status, body.as_str()), (web::http::StatusCode::OK, "get")); + + let (status, body) = send(web::test::TestRequest::delete()).await; + assert_eq!( + (status, body.as_str()), + (web::http::StatusCode::OK, "delete") + ); +} + +#[pagetop::test] +async fn post_put_and_patch_send_the_form_fields() { + for request in [ + web::test::TestRequest::post(), + web::test::TestRequest::put(), + web::test::TestRequest::patch(), + ] { + let (status, body) = send(request.form("name=Plant+lettuces")).await; + assert_eq!( + (status, body.as_str()), + (web::http::StatusCode::OK, "Plant lettuces") + ); + } +} + +#[pagetop::test] +async fn a_form_request_without_fields_is_rejected() { + // No `form()`: no body and no form content type, so the form extractor refuses it. + let (status, _) = send(web::test::TestRequest::post()).await; + assert!(status.is_client_error()); +}