Skip to content
This repository has been archived by the owner on Feb 10, 2019. It is now read-only.

Commit

Permalink
deserialize http_method directly to http::Method
Browse files Browse the repository at this point in the history
  • Loading branch information
softprops committed Nov 25, 2018
1 parent 8ac183f commit fdc4fa8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
12 changes: 4 additions & 8 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,9 @@ mod tests {
query.insert("foo".to_owned(), "bar".to_owned());
let gwr: GatewayRequest = GatewayRequest {
path: "/foo".into(),
http_method: "GET".into(),
headers,
query_string_parameters: StrMap(query.clone().into()),
..Default::default()
..GatewayRequest::default()
};
let actual = HttpRequest::from(gwr);
assert_eq!(
Expand All @@ -193,10 +192,9 @@ mod tests {
}
let gwr: GatewayRequest = GatewayRequest {
path: "/foo".into(),
http_method: "GET".into(),
headers,
body: Some("foo=bar&baz=2".into()),
..Default::default()
..GatewayRequest::default()
};
let actual = HttpRequest::from(gwr);
let payload: Option<Payload> = actual.payload().unwrap_or_else(|_| None);
Expand All @@ -219,10 +217,9 @@ mod tests {
);
let gwr: GatewayRequest = GatewayRequest {
path: "/foo".into(),
http_method: "GET".into(),
headers,
body: Some("foo=bar&baz=2".into()),
..Default::default()
..GatewayRequest::default()
};
let actual = HttpRequest::from(gwr);
let mut expected = HashMap::new();
Expand All @@ -244,10 +241,9 @@ mod tests {
}
let gwr: GatewayRequest = GatewayRequest {
path: "/foo".into(),
http_method: "GET".into(),
headers,
body: Some(r#"{"foo":"bar", "baz": 2}"#.into()),
..Default::default()
..GatewayRequest::default()
};
let actual = HttpRequest::from(gwr);
let payload: Option<Payload> = actual.payload().unwrap_or_else(|_| None);
Expand Down
37 changes: 31 additions & 6 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::mem;
// Third Party
use http::header::{HeaderValue, HOST};
use http::Request as HttpRequest;
use http::{self, HeaderMap};
use http::{self, HeaderMap, Method};
use serde::{de::Error as DeError, de::MapAccess, de::Visitor, Deserialize, Deserializer};

use body::Body;
Expand All @@ -25,7 +25,8 @@ use strmap::StrMap;
pub struct GatewayRequest<'a> {
//pub resDeserializeHeadersource: String,
pub(crate) path: Cow<'a, str>,
pub(crate) http_method: Cow<'a, str>,
#[serde(deserialize_with = "deserialize_method")]
pub(crate) http_method: Method,
#[serde(deserialize_with = "deserialize_headers")]
pub(crate) headers: HeaderMap<HeaderValue>,
#[serde(deserialize_with = "nullable_default")]
Expand Down Expand Up @@ -73,6 +74,30 @@ pub struct Identity {
pub user_arn: Option<String>,
}

fn deserialize_method<'de, D>(deserializer: D) -> Result<Method, D::Error>
where
D: Deserializer<'de>,
{
struct MethodVisitor;

impl<'de> Visitor<'de> for MethodVisitor {
type Value = Method;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a Method")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: DeError,
{
v.parse().map_err(E::custom)
}
}

deserializer.deserialize_str(MethodVisitor)
}

fn deserialize_headers<'de, D>(deserializer: D) -> Result<HeaderMap<HeaderValue>, D::Error>
where
D: Deserializer<'de>,
Expand Down Expand Up @@ -134,7 +159,7 @@ impl<'a> From<GatewayRequest<'a>> for HttpRequest<Body> {

// build an http::Request<lando::Body> from a lando::GatewayRequest
let mut builder = HttpRequest::builder();
builder.method(http_method.as_ref());
builder.method(http_method);
builder.uri({
format!(
"https://{}{}",
Expand Down Expand Up @@ -184,14 +209,14 @@ mod tests {
headers.insert("Host", "www.rust-lang.org".parse().unwrap());
let gwr: GatewayRequest = GatewayRequest {
path: "/foo".into(),
http_method: "GET".into(),
headers,
..Default::default()
..GatewayRequest::default()
};
let expected = HttpRequest::get("https://www.rust-lang.org/foo")
.body(())
.unwrap();
let actual = HttpRequest::from(gwr);
assert_eq!(expected.method(), actual.method());
assert_eq!(expected.uri(), actual.uri());
assert_eq!(expected.method(), actual.method());
}
Expand All @@ -209,7 +234,7 @@ mod tests {
assert_eq!(
GatewayRequest {
path: "/foo".into(),
..Default::default()
..GatewayRequest::default()
}
.path,
"/foo"
Expand Down

0 comments on commit fdc4fa8

Please sign in to comment.