✨ (web): Añade form() y más métodos a TestRequest
This commit is contained in:
parent
ddbc5c869f
commit
98a71b6f51
2 changed files with 104 additions and 8 deletions
40
src/web.rs
40
src/web.rs
|
|
@ -438,26 +438,42 @@ pub mod test {
|
||||||
uri: String,
|
uri: String,
|
||||||
headers: http::HeaderMap,
|
headers: http::HeaderMap,
|
||||||
extensions: http::Extensions,
|
extensions: http::Extensions,
|
||||||
|
body: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestRequest {
|
impl TestRequest {
|
||||||
/// Crea una petición GET.
|
/// Crea una petición GET.
|
||||||
pub fn get() -> Self {
|
pub fn get() -> Self {
|
||||||
Self {
|
Self::with_method(http::Method::GET)
|
||||||
method: http::Method::GET,
|
|
||||||
uri: "/".to_owned(),
|
|
||||||
headers: http::HeaderMap::new(),
|
|
||||||
extensions: http::Extensions::new(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Crea una petición POST.
|
/// Crea una petición POST.
|
||||||
pub fn post() -> Self {
|
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 {
|
Self {
|
||||||
method: http::Method::POST,
|
method,
|
||||||
uri: "/".to_owned(),
|
uri: "/".to_owned(),
|
||||||
headers: http::HeaderMap::new(),
|
headers: http::HeaderMap::new(),
|
||||||
extensions: http::Extensions::new(),
|
extensions: http::Extensions::new(),
|
||||||
|
body: String::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -481,6 +497,14 @@ pub mod test {
|
||||||
self
|
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<String>) -> Self {
|
||||||
|
self.body = fields.into();
|
||||||
|
self.header("content-type", "application/x-www-form-urlencoded")
|
||||||
|
}
|
||||||
|
|
||||||
/// Inserta un valor en las extensiones de la petición.
|
/// 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
|
/// Ú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()
|
let mut req = http::Request::builder()
|
||||||
.method(self.method)
|
.method(self.method)
|
||||||
.uri(self.uri)
|
.uri(self.uri)
|
||||||
.body(Body::empty())
|
.body(Body::from(self.body))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
*req.headers_mut() = self.headers;
|
*req.headers_mut() = self.headers;
|
||||||
*req.extensions_mut() = self.extensions;
|
*req.extensions_mut() = self.extensions;
|
||||||
|
|
|
||||||
72
tests/web_test_request.rs
Normal file
72
tests/web_test_request.rs
Normal file
|
|
@ -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<Item>) -> 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());
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue