Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #24

Merged
merged 4 commits into from
Sep 17, 2024
Merged

Dev #24

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ strum = "0.26.3"
strum_macros = "0.26.4"

[dev-dependencies]
automod = "1.0.14"
stubr = "0.6.2"
18 changes: 12 additions & 6 deletions tests/cache_test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
mod common;

use common::constants::TEST_URL;
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_LENGTH, USER_AGENT};
use reqwest::header::{HeaderMap, CONTENT_LENGTH, USER_AGENT};
use std::time::Duration;
use strum::IntoEnumIterator;

mod constants {
use reqwest::header::HeaderValue;

pub const TEST_URL: &str = "http://foo.com";
pub const USER_AGENT_VALUE: HeaderValue = HeaderValue::from_static("x-api-key");
pub const CONTENT_LENGTH_VALUE: HeaderValue = HeaderValue::from_static("10000");
}

#[cfg(test)]
mod requesters {
use super::constants::{CONTENT_LENGTH_VALUE, TEST_URL, USER_AGENT_VALUE};
use super::*;
use subscan::{
cache, enums::RequesterType, interfaces::requester::RequesterInterface,
Expand All @@ -27,8 +33,8 @@ mod requesters {
let new_config = RequesterConfig {
timeout: Duration::from_secs(120),
headers: HeaderMap::from_iter([
(USER_AGENT, HeaderValue::from_static("x-api-key")),
(CONTENT_LENGTH, HeaderValue::from_static("10000")),
(USER_AGENT, USER_AGENT_VALUE),
(CONTENT_LENGTH, CONTENT_LENGTH_VALUE),
]),
proxy: Some(TEST_URL.to_string()),
};
Expand Down
20 changes: 20 additions & 0 deletions tests/extractors/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub mod constants {
pub const TEST_DOMAIN: &str = "foo.com";
pub const TEST_BAR_SUBDOMAIN: &str = "bar.foo.com";
pub const TEST_BAZ_SUBDOMAIN: &str = "baz.foo.com";
pub const READ_ERROR: &str = "Cannot read file!";
}

pub mod funcs {
use super::constants::READ_ERROR;
use std::fs;
use std::path::{Path, PathBuf};

fn testdata_path() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("testing/testdata")
}

pub fn read_testdata(path: &str) -> String {
fs::read_to_string(testdata_path().join(path)).expect(READ_ERROR)
}
}
33 changes: 33 additions & 0 deletions tests/extractors/html_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::common::{
constants::{TEST_BAR_SUBDOMAIN, TEST_BAZ_SUBDOMAIN, TEST_DOMAIN},
funcs::read_testdata,
};
use subscan::extractors::html::HTMLExtractor;
use subscan::interfaces::extractor::SubdomainExtractorInterface;

#[tokio::test]
async fn extract_without_removes() {
let html = read_testdata("html/subdomains.html");

let selector = String::from("article > div > a > span:first-child");
let extractor = HTMLExtractor::new(selector, vec![]);
let result = extractor.extract(html, TEST_DOMAIN.to_string()).await;

assert_eq!(result, [TEST_BAR_SUBDOMAIN.to_string()].into());
}

#[tokio::test]
async fn extract_with_removes() {
let html = read_testdata("html/subdomains-with-removes.html");

let selector = String::from("article > div > a > span");
let extractor = HTMLExtractor::new(selector, vec!["<br>".to_string()]);
let result = extractor.extract(html, TEST_DOMAIN.to_string()).await;

let expected = [
TEST_BAR_SUBDOMAIN.to_string(),
TEST_BAZ_SUBDOMAIN.to_string(),
];

assert_eq!(result, expected.into());
}
1 change: 1 addition & 0 deletions tests/extractors/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
automod::dir!("tests/extractors");
30 changes: 30 additions & 0 deletions tests/extractors/regex_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::common::constants::{TEST_BAR_SUBDOMAIN, TEST_BAZ_SUBDOMAIN, TEST_DOMAIN};
use subscan::extractors::regex::RegexExtractor;
use subscan::interfaces::extractor::SubdomainExtractorInterface;

#[tokio::test]
async fn extract_one_test() {
let target = String::from(TEST_DOMAIN);
let extractor = RegexExtractor::default();

let matches = String::from(TEST_BAR_SUBDOMAIN);
let no_matches = String::from("foobarbaz");

assert!(extractor.extract_one(matches, target.clone()).is_some());
assert!(extractor.extract_one(no_matches, target).is_none());
}

#[tokio::test]
async fn extract_test() {
let content = String::from("bar.foo.com\nbaz.foo.com");

let extractor = RegexExtractor::default();
let result = extractor.extract(content, TEST_DOMAIN.to_string()).await;

let expected = [
TEST_BAR_SUBDOMAIN.to_string(),
TEST_BAZ_SUBDOMAIN.to_string(),
];

assert_eq!(result, expected.into());
}
82 changes: 0 additions & 82 deletions tests/extractors_test.rs

This file was deleted.

66 changes: 0 additions & 66 deletions tests/module_engines_test.rs

This file was deleted.

31 changes: 0 additions & 31 deletions tests/module_generics_test.rs

This file was deleted.

23 changes: 2 additions & 21 deletions tests/common/mod.rs → tests/modules/common.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
pub mod constants {
#![allow(dead_code)]
pub const TEST_DOMAIN: &str = "foo.com";
pub const TEST_BAR_SUBDOMAIN: &str = "bar.foo.com";
pub const TEST_BAZ_SUBDOMAIN: &str = "baz.foo.com";
pub const TEST_MODULE_NAME: &str = "foo-module";
pub const TEST_URL: &str = "http://foo.com";
}

pub mod funcs {
#![allow(dead_code)]
use std::fs;
use std::path::{Path, PathBuf};

const READ_ERROR: &str = "Cannot read file!";

fn testdata_path() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("testing/testdata")
}

pub fn read_testdata(path: &str) -> String {
fs::read_to_string(testdata_path().join(path)).expect(READ_ERROR)
}
pub const TEST_DOMAIN: &str = "foo.com";
pub const TEST_BAR_SUBDOMAIN: &str = "bar.foo.com";
}

pub mod mocks {
#![allow(dead_code)]
use super::constants::TEST_MODULE_NAME;
use reqwest::Url;
use subscan::{
Expand Down
16 changes: 16 additions & 0 deletions tests/modules/engines/bing_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::common::constants::{TEST_BAR_SUBDOMAIN, TEST_DOMAIN};
use reqwest::Url;
use subscan::{interfaces::module::SubscanModuleInterface, modules::engines::bing};

#[tokio::test]
#[stubr::mock("module/engines/bing.json")]
async fn bing_run_test() {
let mut bing = bing::Bing::new();

bing.url = Url::parse(stubr.path("/search").as_str()).unwrap();

let result = bing.run(TEST_DOMAIN.to_string()).await;

assert_eq!(bing.name().await, "Bing");
assert_eq!(result, [TEST_BAR_SUBDOMAIN.to_string()].into());
}
20 changes: 20 additions & 0 deletions tests/modules/engines/duckduckgo_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::common::constants::{TEST_BAR_SUBDOMAIN, TEST_DOMAIN};
use reqwest::Url;
use subscan::{
cache::requesters, enums::RequesterType, interfaces::module::SubscanModuleInterface,
modules::engines::duckduckgo,
};

#[tokio::test]
#[stubr::mock("module/engines/duckduckgo.json")]
async fn duckduckgo_run_test() {
let mut duckduckgo = duckduckgo::DuckDuckGo::new();

duckduckgo.requester = requesters::get_by_type(&RequesterType::HTTPClient);
duckduckgo.url = Url::parse(stubr.uri().as_str()).unwrap();

let result = duckduckgo.run(TEST_DOMAIN.to_string()).await;

assert_eq!(duckduckgo.name().await, "DuckDuckGo");
assert_eq!(result, [TEST_BAR_SUBDOMAIN.to_string()].into());
}
16 changes: 16 additions & 0 deletions tests/modules/engines/google_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::common::constants::{TEST_BAR_SUBDOMAIN, TEST_DOMAIN};
use reqwest::Url;
use subscan::{interfaces::module::SubscanModuleInterface, modules::engines::google};

#[tokio::test]
#[stubr::mock("module/engines/google.json")]
async fn foo_test() {
let mut google = google::Google::new();

google.url = Url::parse(stubr.path("/search").as_str()).unwrap();

let result = google.run(TEST_DOMAIN.to_string()).await;

assert_eq!(google.name().await, "Google");
assert_eq!(result, [TEST_BAR_SUBDOMAIN.to_string()].into());
}
1 change: 1 addition & 0 deletions tests/modules/engines/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
automod::dir!("tests/modules/engines");
Loading