(web): Añade cabeceras HTTP a TestRequest

This commit is contained in:
Manuel Cillero 2026-07-26 10:03:54 +02:00
parent 39f494539e
commit 65c8a788c2

View file

@ -347,6 +347,7 @@ pub mod test {
pub struct TestRequest { pub struct TestRequest {
method: http::Method, method: http::Method,
uri: String, uri: String,
headers: http::HeaderMap,
extensions: http::Extensions, extensions: http::Extensions,
} }
@ -356,6 +357,7 @@ pub mod test {
Self { Self {
method: http::Method::GET, method: http::Method::GET,
uri: "/".to_owned(), uri: "/".to_owned(),
headers: http::HeaderMap::new(),
extensions: http::Extensions::new(), extensions: http::Extensions::new(),
} }
} }
@ -365,6 +367,7 @@ pub mod test {
Self { Self {
method: http::Method::POST, method: http::Method::POST,
uri: "/".to_owned(), uri: "/".to_owned(),
headers: http::HeaderMap::new(),
extensions: http::Extensions::new(), extensions: http::Extensions::new(),
} }
} }
@ -375,6 +378,20 @@ pub mod test {
self self
} }
/// Añade una cabecera a la petición.
///
/// Si `name` o `value` no son válidos como cabecera HTTP, se descarta en silencio en lugar
/// de entrar en pánico.
pub fn header(mut self, name: impl AsRef<str>, value: impl AsRef<str>) -> Self {
if let (Ok(n), Ok(v)) = (
http::HeaderName::from_bytes(name.as_ref().as_bytes()),
http::HeaderValue::from_str(value.as_ref()),
) {
self.headers.insert(n, v);
}
self
}
/// 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
@ -391,6 +408,7 @@ pub mod test {
.uri(self.uri) .uri(self.uri)
.body(Body::empty()) .body(Body::empty())
.unwrap(); .unwrap();
*req.headers_mut() = self.headers;
*req.extensions_mut() = self.extensions; *req.extensions_mut() = self.extensions;
req req
} }
@ -401,7 +419,7 @@ pub mod test {
let uri = self.uri.parse().unwrap(); let uri = self.uri.parse().unwrap();
super::HttpRequest { super::HttpRequest {
uri, uri,
headers: axum::http::HeaderMap::new(), headers: self.headers,
extensions: std::sync::Arc::new(self.extensions), extensions: std::sync::Arc::new(self.extensions),
} }
} }