use http::Method; use http::StatusCode; use raspa::request::{ParameterType, Request, RequestBase}; use raspa::response::ResponseBase; use raspa::selector::{Selector, SelectorBase}; use serde_json::Value; // use std::collections::HashMap; #[test] fn plain_text_selector() { let html = "
good bye
simple text "; let sel = Selector::from_html(html); assert_eq!(sel.css("h1")[0].html(), "good bye
simple textgood bye
simple text "; let sel = Selector::from_html(html); assert_eq!( sel.xpath_once("//div/a[1]").unwrap().content(), "first text" ); assert_eq!(sel.xpath("//*[@id='text']")[0].content(), "good bye"); assert_eq!( sel.xpath("//a[contains(@href, 'localhost')]")[0] .attr("href") .unwrap(), "http://localhost" ); assert_eq!( sel.xpath_once("//div[@class='container']/a[3]") .unwrap() .content(), "non link" ); } #[test] fn simple_json_test() { let req = Request::new("https://httpbin.org/json").unwrap(); let resp: Value = req.launch().to_json().expect("cannot parse json"); assert_eq!(resp["slideshow"]["title"], "Sample Slide Show"); } #[test] fn simple_post_request() { let resp: Value = Request::new("https://httpbin.org/post") .unwrap() .method(Method::POST) .launch() .to_json() .expect("cannot parse json"); assert_eq!(resp["url"].as_str().unwrap(), "https://httpbin.org/post"); } #[test] fn parameter_get_request() { use ParameterType::*; let mut req = Request::new("https://httpbin.org/get").unwrap().add_params( [ ("param_str", Text("hello".to_string())), ("param_num", Number(42)), ("param_bool", Boolean(true)), ] .to_vec(), ); req.add_param("param_float", 3.14159); let resp: Value = req.launch().to_json().expect("cannot parse json"); println!("{}", resp); assert_eq!(resp["args"]["param_str"].as_str(), Some("hello")); assert_eq!(resp["args"]["param_num"].as_str(), Some("42")); assert_eq!(resp["args"]["param_bool"].as_str(), Some("true")); assert_eq!(resp["args"]["param_float"].as_str(), Some("3.14159")); } #[test] fn default_user_agent() { let resp: Value = Request::new("https://httpbin.org/get") .unwrap() .launch() .to_json() .expect("Weird JSON parsed fail"); assert_eq!( resp["headers"]["User-Agent"].as_str().unwrap(), std::env::var("DEFAULT_USER_AGENT").unwrap() ); } #[test] fn custom_user_agent() { let resp: Value = Request::new("https://httpbin.org/get") .unwrap() .set_user_agent("test user agent") .launch() .to_json() .expect("Weird JSON parsed fail"); assert_eq!( resp["headers"]["User-Agent"].as_str(), Some("test user agent") ); } #[test] fn simple_cookie_test() { let mut req = Request::new("https://httpbin.org/cookies").unwrap(); req.add_cookie("test_cookie", "100"); let resp: Value = req.launch().to_json().expect("Weird JSON parsed fail"); assert_eq!(resp["cookies"]["test_cookie"].as_str(), Some("100")); } #[test] fn multiple_cookies_test() { let resp: Value = Request::new("https://httpbin.org/cookies") .unwrap() .add_cookies(vec![ ("cookie_1", 100), ("cookie_2", 200), ("cookie_3", 300), ]) .launch() .to_json() .expect("Weird JSON parsed fail"); assert_eq!(resp["cookies"]["cookie_1"].as_str(), Some("100")); assert_eq!(resp["cookies"]["cookie_2"].as_str(), Some("200")); assert_eq!(resp["cookies"]["cookie_3"].as_str(), Some("300")); }