Skip to content

Commit

Permalink
revamp session unit tests and remove I/O
Browse files Browse the repository at this point in the history
  • Loading branch information
ducaale committed Jun 6, 2023
1 parent 904dc61 commit 48e1b62
Showing 1 changed file with 82 additions and 148 deletions.
230 changes: 82 additions & 148 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,124 +295,69 @@ fn path_from_url(url: &Url) -> Result<String> {
mod tests {
use super::*;

use anyhow::Result;
use reqwest::header::HeaderValue;

use crate::utils::random_string;
use anyhow::Result;
fn load_session_from_str(s: &str) -> Result<Session> {
Ok(Session {
content: serde_json::from_str::<Content>(s)?.migrate(),
path: PathBuf::new(),
read_only: false,
})
}

#[test]
fn can_read_httpie_session_file() -> Result<()> {
let mut path_to_session = std::env::temp_dir();
let file_name = random_string();
path_to_session.push(file_name);
fs::write(
&path_to_session,
indoc::indoc! {r#"
{
"__meta__": {
"about": "HTTPie session file",
"help": "https://httpie.org/doc#sessions",
"httpie": "2.3.0"
},
"auth": {
"password": null,
"type": null,
"username": null
},
"cookies": {
"__cfduid": {
"expires": 1620239688,
"path": "/",
"secure": false,
"value": "d090ada9c629fc7b8bbc6dba3dde1149d1617647688"
}
},
"headers": {
"hello": "world"
}
}
"#},
)?;

let session = Session::load_session(
&Url::parse("http://localhost")?,
path_to_session.into(),
false,
)?;
fn can_parse_old_httpie_session() -> Result<()> {
let session = load_session_from_str(indoc::indoc! {r#"
{
"__meta__": {
"about": "HTTPie session file",
"help": "https://httpie.org/doc#sessions",
"httpie": "2.3.0"
},
"auth": { "password": null, "type": null, "username": null },
"cookies": {
"baz": { "expires": null, "path": "/", "secure": false, "value": "quux" }
},
"headers": { "hello": "world" }
}
"#})?;

assert_eq!(
session.headers()?.get("hello"),
Some(&HeaderValue::from_static("world")),
);
assert_eq!(session.cookies()[0].name_value(), ("baz", "quux"));
assert_eq!(session.cookies()[0].path(), Some("/"));
assert_eq!(session.cookies()[0].secure(), Some(false));
assert_eq!(session.content.auth, Auth::default());

assert_eq!(
session.content.auth,
Auth {
auth_type: None,
raw_auth: None
},
);

let expected_cookie = serde_json::from_str::<Cookie>(
r#"
{
"expires": 1620239688,
"path": "/",
"secure": false,
"value": "d090ada9c629fc7b8bbc6dba3dde1149d1617647688"
}
"#,
)?;
assert_eq!(
session.content.cookies.get("__cfduid"),
Some(&expected_cookie)
);
Ok(())
}

#[test]
fn can_read_xh_session_file() -> Result<()> {
let mut path_to_session = std::env::temp_dir();
let file_name = random_string();
path_to_session.push(file_name);
fs::write(
&path_to_session,
indoc::indoc! {r#"
{
"__meta__": {
"about": "xh session file",
"xh": "0.0.0"
},
"auth": {
"raw_auth": "secret-token",
"type": "bearer"
},
"cookies": {
"__cfduid": {
"expires": 1620239688,
"path": "/",
"secure": false,
"value": "d090ada9c629fc7b8bbc6dba3dde1149d1617647688"
}
},
"headers": {
"hello": "world"
}
}
"#},
)?;

let session = Session::load_session(
&Url::parse("http://localhost")?,
path_to_session.into(),
false,
)?;
fn can_parse_old_xh_session() -> Result<()> {
let session = load_session_from_str(indoc::indoc! {r#"
{
"__meta__": {
"about": "xh session file",
"xh": "0.0.0"
},
"auth": { "raw_auth": "secret-token", "type": "bearer" },
"cookies": {
"baz": { "expires": null, "path": "/", "secure": false, "value": "quux" }
},
"headers": { "hello": "world" }
}
"#})?;

assert_eq!(
session.headers()?.get("hello"),
Some(&HeaderValue::from_static("world")),
);

assert_eq!(session.cookies()[0].name_value(), ("baz", "quux"));
assert_eq!(session.cookies()[0].path(), Some("/"));
assert_eq!(session.cookies()[0].secure(), Some(false));
assert_eq!(
session.content.auth,
Auth {
Expand All @@ -421,64 +366,53 @@ mod tests {
},
);

let expected_cookie = serde_json::from_str::<Cookie>(
r#"
{
"expires": 1620239688,
"path": "/",
"secure": false,
"value": "d090ada9c629fc7b8bbc6dba3dde1149d1617647688"
}
"#,
)?;
assert_eq!(
session.content.cookies.get("__cfduid"),
Some(&expected_cookie)
);
Ok(())
}

#[test]
fn can_read_session_with_duplicate_keys() -> Result<()> {
let mut path_to_session = std::env::temp_dir();
let file_name = random_string();
path_to_session.push(file_name);
fs::write(
&path_to_session,
indoc::indoc! {r#"
{
"__meta__": {
"about": "xh session file",
"xh": "0.0.0"
},
"auth": {},
"cookies": {},
"headers": [
{ "name": "hello", "value": "world" },
{ "name": "hello", "value": "people" }
]
}
"#},
)?;
fn can_parse_session_with_unknown_meta() {
load_session_from_str(indoc::indoc! {r#"
{
"__meta__": {},
"auth": { "raw_auth": "secret-token", "type": "bearer" },
"cookies": {
"baz": { "expires": null, "path": "/", "secure": false, "value": "quux" }
},
"headers": { "hello": "world" }
}
"#})
.unwrap();
}

let session = Session::load_session(
&Url::parse("http://localhost")?,
path_to_session.into(),
false,
)?;
#[test]
fn can_parse_session_with_new_style_headers() -> Result<()> {
let session = load_session_from_str(indoc::indoc! {r#"
{
"__meta__": {
"about": "HTTPie session file",
"help": "https://httpie.io/docs#sessions",
"httpie": "3.0.2"
},
"auth": {},
"cookies": {},
"headers": [
{ "name": "X-Data", "value": "value" },
{ "name": "X-Foo", "value": "bar" },
{ "name": "X-Foo", "value": "baz" }
]
}
"#})?;

let headers = session.headers()?;
assert_eq!(
session
.headers()?
.get_all("hello")
.into_iter()
.collect::<Vec<_>>(),
[
&HeaderValue::from_static("world"),
&HeaderValue::from_static("people")
],
headers.get("X-Data"),
Some(&HeaderValue::from_static("value"))
);

let mut x_foo_values = headers.get_all("X-Foo").iter();
assert_eq!(x_foo_values.next(), Some(&HeaderValue::from_static("bar")));
assert_eq!(x_foo_values.next(), Some(&HeaderValue::from_static("baz")));

Ok(())
}
}

0 comments on commit 48e1b62

Please sign in to comment.