Skip to content

Commit

Permalink
feat: Added basic request type
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Mar 23, 2018
1 parent de1ebb0 commit a1436e4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/protocol/v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,20 @@ pub struct User {
#[serde(default)]
pub struct Request {
/// The current URL of the request.
#[serde(with = "url_serde")]
#[serde(with = "url_serde", skip_serializing_if = "Option::is_none")]
pub url: Option<Url>,
/// The HTTP request method.
#[serde(skip_serializing_if = "Option::is_none")]
pub method: Option<String>,
/// Optionally some associated request data (human readable)
// XXX: this makes absolutely no sense because of unicode
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<String>,
/// Optionally the encoded query string.
#[serde(skip_serializing_if = "Option::is_none")]
pub query_string: Option<String>,
/// An encoded cookie string if available.
#[serde(skip_serializing_if = "Option::is_none")]
pub cookies: Option<String>,
/// HTTP request headers.
#[serde(skip_serializing_if = "HashMap::is_empty")]
Expand Down
34 changes: 34 additions & 0 deletions tests/test_protocol_v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,40 @@ fn test_user() {
);
}

#[test]
fn test_request() {
let event = v7::Event {
request: Some(v7::Request {
url: "https://www.example.invalid/bar".parse().ok(),
method: Some("GET".into()),
data: Some("{}".into()),
query_string: Some("foo=bar&blub=blah".into()),
cookies: Some("dummy=42".into()),
headers: {
let mut hm = HashMap::new();
hm.insert("Content-Type".into(), "text/plain".into());
hm
},
env: {
let mut env = HashMap::new();
env.insert("PATH_INFO".into(), "/bar".into());
env
},
..Default::default()
}),
..Default::default()
};

assert_eq!(
serde_json::to_string(&event).unwrap(),
"{\"request\":{\"url\":\"https://www.example.invalid/bar\",\
\"method\":\"GET\",\"data\":\"{}\",\"query_string\":\
\"foo=bar&blub=blah\",\"cookies\":\"dummy=42\",\"headers\":\
{\"Content-Type\":\"text/plain\"},\"env\":\
{\"PATH_INFO\":\"/bar\"}}}"
);
}

#[test]
fn test_canonical_exception() {
let mut event: v7::Event = Default::default();
Expand Down

0 comments on commit a1436e4

Please sign in to comment.