1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-19 22:44:38 +00:00
deprecated-gotham-restful/src/response/success.rs

129 lines
3.2 KiB
Rust
Raw Normal View History

2021-02-27 15:40:34 +00:00
use super::IntoResponse;
#[cfg(feature = "openapi")]
2021-02-27 15:40:34 +00:00
use crate::{OpenapiSchema, ResponseSchema};
use crate::{Response, ResponseBody};
2021-02-27 15:40:34 +00:00
use futures_util::future::{self, FutureExt};
use gotham::hyper::{
header::{HeaderMap, HeaderValue, IntoHeaderName},
StatusCode
};
2021-02-27 15:40:34 +00:00
use mime::{Mime, APPLICATION_JSON};
use std::{fmt::Debug, future::Future, pin::Pin};
/**
2021-02-27 15:40:34 +00:00
This can be returned from a resource when there is no cause of an error.
Usage example:
```
# #[macro_use] extern crate gotham_restful_derive;
# mod doc_tests_are_broken {
# use gotham::state::State;
# use gotham_restful::*;
# use serde::{Deserialize, Serialize};
#
# #[derive(Resource)]
2020-05-04 21:34:20 +02:00
# #[resource(read_all)]
# struct MyResource;
#
#[derive(Deserialize, Serialize)]
2020-05-16 01:01:20 +02:00
# #[cfg_attr(feature = "openapi", derive(OpenapiType))]
struct MyResponse {
message: &'static str
}
#[read_all]
fn read_all() -> Success<MyResponse> {
let res = MyResponse { message: "I'm always happy" };
res.into()
}
# }
```
*/
2021-02-27 15:40:34 +00:00
#[derive(Clone, Debug, Default)]
pub struct Success<T> {
value: T,
headers: HeaderMap
}
impl<T> From<T> for Success<T> {
fn from(t: T) -> Self {
2021-02-27 15:40:34 +00:00
Self {
value: t,
headers: HeaderMap::new()
}
}
}
2021-02-27 15:40:34 +00:00
impl<T> Success<T> {
/// Set a custom HTTP header. If a header with this name was set before, its value is being updated.
pub fn header<K: IntoHeaderName>(&mut self, name: K, value: HeaderValue) {
self.headers.insert(name, value);
}
2021-02-27 15:40:34 +00:00
/// Allow manipulating HTTP headers.
pub fn headers_mut(&mut self) -> &mut HeaderMap {
&mut self.headers
}
}
2021-02-27 15:40:34 +00:00
impl<T: ResponseBody> IntoResponse for Success<T> {
type Err = serde_json::Error;
fn into_response(self) -> Pin<Box<dyn Future<Output = Result<Response, Self::Err>> + Send>> {
2021-02-27 15:40:34 +00:00
let res =
serde_json::to_string(&self.value).map(|body| Response::json(StatusCode::OK, body).with_headers(self.headers));
future::ready(res).boxed()
}
fn accepted_types() -> Option<Vec<Mime>> {
Some(vec![APPLICATION_JSON])
}
2021-02-21 18:21:09 +00:00
}
2021-02-21 18:21:09 +00:00
#[cfg(feature = "openapi")]
2021-02-27 15:40:34 +00:00
impl<T: ResponseBody> ResponseSchema for Success<T> {
fn schema() -> OpenapiSchema {
T::schema()
}
}
#[cfg(test)]
mod test {
use super::*;
2021-02-27 15:40:34 +00:00
use crate::response::OrAllTypes;
use futures_executor::block_on;
2021-02-27 15:40:34 +00:00
use gotham::hyper::header::ACCESS_CONTROL_ALLOW_ORIGIN;
#[derive(Debug, Default, Serialize)]
#[cfg_attr(feature = "openapi", derive(crate::OpenapiType))]
struct Msg {
msg: String
}
#[test]
fn success_always_successfull() {
let success: Success<Msg> = Msg::default().into();
let res = block_on(success.into_response()).expect("didn't expect error response");
assert_eq!(res.status, StatusCode::OK);
assert_eq!(res.mime, Some(APPLICATION_JSON));
2021-01-01 18:03:31 +01:00
assert_eq!(res.full_body().unwrap(), br#"{"msg":""}"#);
2021-02-27 15:40:34 +00:00
#[cfg(feature = "openapi")]
assert_eq!(<Success<Msg>>::default_status(), StatusCode::OK);
}
#[test]
fn success_custom_headers() {
let mut success: Success<Msg> = Msg::default().into();
success.header(ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static("*"));
let res = block_on(success.into_response()).expect("didn't expect error response");
let cors = res.headers.get(ACCESS_CONTROL_ALLOW_ORIGIN);
assert_eq!(cors.map(|value| value.to_str().unwrap()), Some("*"));
}
#[test]
fn success_accepts_json() {
assert!(<Success<Msg>>::accepted_types().or_all_types().contains(&APPLICATION_JSON))
}
}