use crate::bindings::Trigger;
use crate::http::Body;
use crate::rpc::protocol;
use std::collections::HashMap;
#[derive(Debug)]
pub struct HttpRequest(protocol::RpcHttp);
impl HttpRequest {
pub fn method(&self) -> &str {
&self.0.method
}
pub fn url(&self) -> &str {
&self.0.url
}
pub fn headers(&self) -> &HashMap<String, String> {
&self.0.headers
}
pub fn route_params(&self) -> &HashMap<String, String> {
&self.0.params
}
pub fn query_params(&self) -> &HashMap<String, String> {
&self.0.query
}
pub fn body(&self) -> Body {
if self.0.has_body() {
Body::from(self.0.get_body())
} else {
Body::Empty
}
}
}
impl From<protocol::TypedData> for HttpRequest {
fn from(mut data: protocol::TypedData) -> Self {
if !data.has_http() {
panic!("unexpected type data for HTTP request.");
}
HttpRequest(data.take_http())
}
}
impl Trigger for HttpRequest {
fn read_metadata(&mut self, _: &mut HashMap<String, protocol::TypedData>) {}
}
#[cfg(test)]
mod tests {
use super::*;
use std::borrow::Cow;
#[test]
fn it_has_the_method() {
const METHOD: &'static str = "GET";
let mut data = protocol::TypedData::new();
let mut http = protocol::RpcHttp::new();
http.set_method(METHOD.to_string());
data.set_http(http);
let request: HttpRequest = data.into();
assert_eq!(request.method(), METHOD);
}
#[test]
fn it_has_the_url() {
const URL: &'static str = "http://example.com";
let mut data = protocol::TypedData::new();
let mut http = protocol::RpcHttp::new();
http.set_url(URL.to_string());
data.set_http(http);
let request: HttpRequest = data.into();
assert_eq!(request.url(), URL);
}
#[test]
fn it_has_a_header() {
const KEY: &'static str = "Accept";
const VALUE: &'static str = "application/json";
let mut data = protocol::TypedData::new();
let mut http = protocol::RpcHttp::new();
let mut headers = HashMap::new();
headers.insert(KEY.to_string(), VALUE.to_string());
http.set_headers(headers);
data.set_http(http);
let request: HttpRequest = data.into();
assert_eq!(request.headers().get(KEY).unwrap(), VALUE);
}
#[test]
fn it_has_a_route_parameter() {
const KEY: &'static str = "id";
const VALUE: &'static str = "12345";
let mut data = protocol::TypedData::new();
let mut http = protocol::RpcHttp::new();
let mut params = HashMap::new();
params.insert(KEY.to_string(), VALUE.to_string());
http.set_params(params);
data.set_http(http);
let request: HttpRequest = data.into();
assert_eq!(request.route_params().get(KEY).unwrap(), VALUE);
}
#[test]
fn it_has_a_query_parameter() {
const KEY: &'static str = "name";
const VALUE: &'static str = "Peter";
let mut data = protocol::TypedData::new();
let mut http = protocol::RpcHttp::new();
let mut params = HashMap::new();
params.insert(KEY.to_string(), VALUE.to_string());
http.set_query(params);
data.set_http(http);
let request: HttpRequest = data.into();
assert_eq!(request.query_params().get(KEY).unwrap(), VALUE);
}
#[test]
fn it_has_an_empty_body() {
let mut data = protocol::TypedData::new();
let http = protocol::RpcHttp::new();
data.set_http(http);
let request: HttpRequest = data.into();
assert!(matches!(request.body(), Body::Empty));
}
#[test]
fn it_has_a_string_body() {
const BODY: &'static str = "TEXT BODY";
let mut data = protocol::TypedData::new();
let mut http = protocol::RpcHttp::new();
let mut body = protocol::TypedData::new();
body.set_string(BODY.to_string());
http.set_body(body);
data.set_http(http);
let request: HttpRequest = data.into();
assert!(matches!(request.body(), Body::String(Cow::Borrowed(BODY))));
}
#[test]
fn it_has_a_json_body() {
const BODY: &'static str = r#"{ "json": "body" }"#;
let mut data = protocol::TypedData::new();
let mut http = protocol::RpcHttp::new();
let mut body = protocol::TypedData::new();
body.set_json(BODY.to_string());
http.set_body(body);
data.set_http(http);
let request: HttpRequest = data.into();
assert!(matches!(request.body(), Body::Json(Cow::Borrowed(BODY))));
}
#[test]
fn it_has_a_bytes_body() {
const BODY: &'static [u8] = &[0, 1, 2];
let mut data = protocol::TypedData::new();
let mut http = protocol::RpcHttp::new();
let mut body = protocol::TypedData::new();
body.set_bytes(BODY.to_owned());
http.set_body(body);
data.set_http(http);
let request: HttpRequest = data.into();
assert!(matches!(request.body(), Body::Bytes(Cow::Borrowed(BODY))));
}
#[test]
fn it_has_a_stream_body() {
const BODY: &'static [u8] = &[0, 1, 2];
let mut data = protocol::TypedData::new();
let mut http = protocol::RpcHttp::new();
let mut body = protocol::TypedData::new();
body.set_stream(BODY.to_owned());
http.set_body(body);
data.set_http(http);
let request: HttpRequest = data.into();
assert!(matches!(request.body(), Body::Bytes(Cow::Borrowed(BODY))));
}
}