✨ (web): Añade cabeceras HTTP a TestRequest
This commit is contained in:
parent
39f494539e
commit
65c8a788c2
1 changed files with 19 additions and 1 deletions
20
src/web.rs
20
src/web.rs
|
|
@ -347,6 +347,7 @@ pub mod test {
|
|||
pub struct TestRequest {
|
||||
method: http::Method,
|
||||
uri: String,
|
||||
headers: http::HeaderMap,
|
||||
extensions: http::Extensions,
|
||||
}
|
||||
|
||||
|
|
@ -356,6 +357,7 @@ pub mod test {
|
|||
Self {
|
||||
method: http::Method::GET,
|
||||
uri: "/".to_owned(),
|
||||
headers: http::HeaderMap::new(),
|
||||
extensions: http::Extensions::new(),
|
||||
}
|
||||
}
|
||||
|
|
@ -365,6 +367,7 @@ pub mod test {
|
|||
Self {
|
||||
method: http::Method::POST,
|
||||
uri: "/".to_owned(),
|
||||
headers: http::HeaderMap::new(),
|
||||
extensions: http::Extensions::new(),
|
||||
}
|
||||
}
|
||||
|
|
@ -375,6 +378,20 @@ pub mod test {
|
|||
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.
|
||||
///
|
||||
/// Ú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)
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
*req.headers_mut() = self.headers;
|
||||
*req.extensions_mut() = self.extensions;
|
||||
req
|
||||
}
|
||||
|
|
@ -401,7 +419,7 @@ pub mod test {
|
|||
let uri = self.uri.parse().unwrap();
|
||||
super::HttpRequest {
|
||||
uri,
|
||||
headers: axum::http::HeaderMap::new(),
|
||||
headers: self.headers,
|
||||
extensions: std::sync::Arc::new(self.extensions),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue