Skip to content

Commit

Permalink
feat: add agent
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 30, 2022
1 parent 6f8d73d commit e4493fd
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions fkl_cli/src/mock/fake_user_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,38 +85,24 @@ static BORWSER_WEIGHTED: phf::Map<&'static str, f32> = phf_map! {
};

pub struct UserAgent {
pub os: String,
pub os_version: String,
pub browser: String,
pub browser_version: String,
pub device: String,
pub device_version: String,
pub engine: String,
pub engine_version: String,
// pub os: String,
// pub browser: String,
// pub version: String,
}

impl UserAgent {
// pub fn new() -> Self {
// let os = Self::browser();
// let os_version = Self::os_version(&os);
// let browser = Self::browser();
// let browser_version = Self::browser_version(&browser);
// let device = Self::device();
// let device_version = Self::device_version(&device);
// let engine = Self::engine();
// let engine_version = Self::engine_version(&engine);
//
// Self {
// os,
// os_version,
// browser,
// browser_version,
// device,
// device_version,
// engine,
// engine_version,
// }
// }
pub fn random() -> String {
let browser = UserAgent::browser();
let os = UserAgent::os(&browser);

match &*browser {
"chrome" => UserAgent::chrome(&os),
"firefox" => UserAgent::firefox(&os),
"opera" => UserAgent::opera(&os),
"safari" => UserAgent::safari(&os),
_ => UserAgent::chrome(&os),
}
}

pub fn weighted_key_from_object(obj: &phf::Map<&str, f32>) -> String {
let mut rng = rand::thread_rng();
Expand All @@ -140,7 +126,7 @@ impl UserAgent {
browser
}

pub fn os_version(browser: &str) -> String {
pub fn os(browser: &str) -> String {
let os_version = match browser {
"chrome" => Self::weighted_key_from_object(&CHROME_OS_WEIGHTED),
"firefox" => Self::weighted_key_from_object(&FIREFOX_OS_WEIGHTED),
Expand Down Expand Up @@ -211,7 +197,7 @@ impl UserAgent {
)
}

pub fn safari(os: &str) -> String {
pub fn safari(os: &str) -> String {
let safari = VersionString::safari();
let ver = format!(
"{}.{}.{}",
Expand Down Expand Up @@ -426,7 +412,7 @@ mod tests {

#[test]
fn test_os_version() {
let os_version = UserAgent::os_version("chrome");
let os_version = UserAgent::os("chrome");
assert!(os_version == "win" || os_version == "mac" || os_version == "lin");
}

Expand Down Expand Up @@ -459,4 +445,11 @@ mod tests {
let opera = UserAgent::opera("win");
assert!(opera.contains("Opera"));
}

#[test]
fn test_random() {
let random_agent = UserAgent::random();
println!("{}", random_agent);
assert!(random_agent.contains("Mozilla/5.0 "));
}
}

0 comments on commit e4493fd

Please sign in to comment.