2021-02-03 23:43:56 +00:00
|
|
|
use http::Method;
|
2021-01-31 03:11:54 +00:00
|
|
|
use http::StatusCode;
|
2021-04-04 00:36:30 +00:00
|
|
|
use raspa::request::{ParameterType, Request, RequestBase};
|
2023-08-06 06:36:50 +00:00
|
|
|
use raspa::response::ResponseBase;
|
2021-01-31 03:11:54 +00:00
|
|
|
use raspa::selector::{Selector, SelectorBase};
|
2021-02-03 23:43:56 +00:00
|
|
|
use serde_json::Value;
|
2021-03-15 01:38:41 +00:00
|
|
|
// use std::collections::HashMap;
|
2021-01-31 03:11:54 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn plain_text_selector() {
|
|
|
|
let html = "
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<h1>hello world</h1>
|
|
|
|
<p id='text'>good bye</p>
|
|
|
|
<a>simple text</a>
|
|
|
|
</body>
|
|
|
|
</html>
|
2021-03-21 07:47:11 +00:00
|
|
|
";
|
2021-01-31 03:11:54 +00:00
|
|
|
let sel = Selector::from_html(html);
|
2021-03-15 01:38:41 +00:00
|
|
|
assert_eq!(sel.css("h1")[0].html(), "<h1>hello world</h1>");
|
|
|
|
assert_eq!(sel.css("#text")[0].content(), "good bye");
|
|
|
|
assert_eq!(sel.css_once("body > a").unwrap().content(), "simple text");
|
2021-01-31 03:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_request() {
|
|
|
|
let req: Request = RequestBase::new("https://httpbin.org/").unwrap();
|
|
|
|
let resp = req.launch();
|
|
|
|
assert_eq!(resp.status_code, StatusCode::OK);
|
2021-03-15 01:38:41 +00:00
|
|
|
assert!(resp.css("h2")[0].html().contains("httpbin.org"));
|
2021-01-31 03:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complex_selectors() {
|
|
|
|
let html = "
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<p id='text'>good bye</p>
|
|
|
|
<a href='http://google.com'>simple text</a>
|
|
|
|
<ul>
|
|
|
|
<li class='item' id='item-1'>1</li>
|
|
|
|
<li class='item' id='item-2'>2</li>
|
|
|
|
<li class='item' id='item-3'>3</li>
|
|
|
|
</ul>
|
2021-01-31 03:20:00 +00:00
|
|
|
<div>
|
|
|
|
<a href='#'>non link</a>
|
|
|
|
<a href='http://localhost'>link</a>
|
|
|
|
<a href='#'>non link</a>
|
|
|
|
</div>
|
2021-01-31 03:11:54 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
2021-03-21 07:47:11 +00:00
|
|
|
";
|
2021-01-31 03:11:54 +00:00
|
|
|
let sel = Selector::from_html(html);
|
2021-03-15 01:38:41 +00:00
|
|
|
assert_eq!(sel.css_once("p").unwrap().attr("id").unwrap(), "text");
|
|
|
|
assert_eq!(sel.css("a")[0].attr("href").unwrap(), "http://google.com");
|
|
|
|
for node in sel.css("ul li").iter() {
|
2021-01-31 03:11:54 +00:00
|
|
|
let text = node.content();
|
|
|
|
assert_eq!(node.attr("class").unwrap(), "item");
|
|
|
|
assert!(node.attr("id").unwrap().contains(&text));
|
|
|
|
}
|
2021-01-31 03:20:00 +00:00
|
|
|
|
2021-03-15 01:38:41 +00:00
|
|
|
let div = sel.css_once("div").unwrap();
|
|
|
|
for node in div.css("a").iter() {
|
2021-01-31 03:20:00 +00:00
|
|
|
if node.attr("href").unwrap() == "#" {
|
|
|
|
assert_eq!(node.content(), "non link");
|
|
|
|
} else {
|
|
|
|
assert_eq!(node.content(), "link");
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 01:38:41 +00:00
|
|
|
assert!(sel.css_once("h1").is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn xpath_test() {
|
|
|
|
let html = "
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<p id='text'>good bye</p>
|
|
|
|
<a href='http://google.com'>simple text</a>
|
|
|
|
<div class='container'>
|
|
|
|
<a href='#'>first text</a>
|
|
|
|
<a href='http://localhost'>link</a>
|
|
|
|
<a href='#'>non link</a>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
2021-03-21 07:47:11 +00:00
|
|
|
";
|
2021-03-15 01:38:41 +00:00
|
|
|
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!(
|
2021-03-17 03:20:50 +00:00
|
|
|
sel.xpath("//a[contains(@href, 'localhost')]")[0]
|
|
|
|
.attr("href")
|
|
|
|
.unwrap(),
|
|
|
|
"http://localhost"
|
2021-03-15 01:38:41 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
sel.xpath_once("//div[@class='container']/a[3]")
|
|
|
|
.unwrap()
|
|
|
|
.content(),
|
|
|
|
"non link"
|
|
|
|
);
|
2021-02-03 21:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_json_test() {
|
2021-02-03 23:43:56 +00:00
|
|
|
let req = Request::new("https://httpbin.org/json").unwrap();
|
|
|
|
let resp: Value = req.launch().to_json().expect("cannot parse json");
|
2021-02-03 21:26:12 +00:00
|
|
|
|
|
|
|
assert_eq!(resp["slideshow"]["title"], "Sample Slide Show");
|
2021-01-31 03:11:54 +00:00
|
|
|
}
|
2021-02-03 23:43:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_post_request() {
|
2021-03-15 01:38:41 +00:00
|
|
|
let resp: Value = Request::new("https://httpbin.org/post")
|
|
|
|
.unwrap()
|
|
|
|
.method(Method::POST)
|
|
|
|
.launch()
|
|
|
|
.to_json()
|
|
|
|
.expect("cannot parse json");
|
2021-02-03 23:43:56 +00:00
|
|
|
assert_eq!(resp["url"].as_str().unwrap(), "https://httpbin.org/post");
|
|
|
|
}
|
2021-03-15 01:38:41 +00:00
|
|
|
|
2021-04-04 00:36:30 +00:00
|
|
|
#[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"));
|
|
|
|
}
|
2021-08-30 19:29:46 +00:00
|
|
|
|
|
|
|
#[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")
|
|
|
|
);
|
|
|
|
}
|
2023-08-06 06:36:50 +00:00
|
|
|
|
|
|
|
#[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"));
|
|
|
|
}
|