-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(http): [#159] HTTP tracker tests scaffolding
- Loading branch information
1 parent
c26b356
commit 3449202
Showing
11 changed files
with
276 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use reqwest::Response; | ||
|
||
pub type ReqwestQuery = Vec<ReqwestQueryParam>; | ||
pub type ReqwestQueryParam = (String, String); | ||
|
||
pub async fn get(path: &str, query: Option<Query>) -> Response { | ||
match query { | ||
Some(params) => reqwest::Client::builder() | ||
.build() | ||
.unwrap() | ||
.get(path) | ||
.query(&ReqwestQuery::from(params)) | ||
.send() | ||
.await | ||
.unwrap(), | ||
None => reqwest::Client::builder().build().unwrap().get(path).send().await.unwrap(), | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct ConnectionInfo { | ||
pub bind_address: String, | ||
} | ||
|
||
/// URL Query component | ||
#[derive(Default, Debug)] | ||
pub struct Query { | ||
params: Vec<QueryParam>, | ||
} | ||
|
||
impl Query { | ||
pub fn empty() -> Self { | ||
Self { params: vec![] } | ||
} | ||
|
||
pub fn params(params: Vec<QueryParam>) -> Self { | ||
Self { params } | ||
} | ||
|
||
pub fn add_param(&mut self, param: QueryParam) { | ||
self.params.push(param); | ||
} | ||
} | ||
|
||
impl From<Query> for ReqwestQuery { | ||
fn from(url_search_params: Query) -> Self { | ||
url_search_params | ||
.params | ||
.iter() | ||
.map(|param| ReqwestQueryParam::from((*param).clone())) | ||
.collect() | ||
} | ||
} | ||
|
||
/// URL query param | ||
#[derive(Clone, Debug)] | ||
pub struct QueryParam { | ||
name: String, | ||
value: String, | ||
} | ||
|
||
impl QueryParam { | ||
pub fn new(name: &str, value: &str) -> Self { | ||
Self { | ||
name: name.to_string(), | ||
value: value.to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl From<QueryParam> for ReqwestQueryParam { | ||
fn from(param: QueryParam) -> Self { | ||
(param.name, param.value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod http; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use reqwest::Response; | ||
|
||
pub async fn assert_internal_server_error(response: Response) { | ||
assert_eq!(response.status(), 200); | ||
/* cspell:disable-next-line */ | ||
assert_eq!(response.text().await.unwrap(), "d14:failure reason21:internal server errore"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use reqwest::Response; | ||
|
||
use super::connection_info::ConnectionInfo; | ||
use crate::common::http::{get, Query}; | ||
|
||
/// HTTP Tracker Client | ||
pub struct Client { | ||
connection_info: ConnectionInfo, | ||
base_path: String, | ||
} | ||
|
||
impl Client { | ||
pub fn new(connection_info: ConnectionInfo) -> Self { | ||
Self { | ||
connection_info, | ||
base_path: "/".to_string(), | ||
} | ||
} | ||
|
||
pub async fn announce(&self, params: Query) -> Response { | ||
self.get("announce", params).await | ||
} | ||
|
||
pub async fn scrape(&self, params: Query) -> Response { | ||
self.get("scrape", params).await | ||
} | ||
|
||
async fn get(&self, path: &str, params: Query) -> Response { | ||
get(&self.base_url(path), Some(params)).await | ||
} | ||
|
||
fn base_url(&self, path: &str) -> String { | ||
format!("http://{}{}{path}", &self.connection_info.bind_address, &self.base_path) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use torrust_tracker::tracker::auth::Key; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct ConnectionInfo { | ||
pub bind_address: String, | ||
pub aut_key: Option<Key>, | ||
} | ||
|
||
impl ConnectionInfo { | ||
pub fn anonymous(bind_address: &str) -> Self { | ||
Self { | ||
bind_address: bind_address.to_string(), | ||
aut_key: None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod asserts; | ||
pub mod client; | ||
pub mod connection_info; | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use core::panic; | ||
use std::sync::Arc; | ||
|
||
use torrust_tracker::config::{ephemeral_configuration, Configuration}; | ||
use torrust_tracker::jobs::http_tracker; | ||
use torrust_tracker::tracker::statistics::Keeper; | ||
use torrust_tracker::{ephemeral_instance_keys, logging, static_time, tracker}; | ||
|
||
use super::connection_info::ConnectionInfo; | ||
|
||
pub fn tracker_configuration() -> Arc<Configuration> { | ||
Arc::new(ephemeral_configuration()) | ||
} | ||
|
||
pub async fn start_default_http_tracker() -> Server { | ||
let configuration = tracker_configuration(); | ||
start_custom_http_tracker(configuration.clone()).await | ||
} | ||
|
||
pub async fn start_custom_http_tracker(configuration: Arc<Configuration>) -> Server { | ||
let server = start(&configuration); | ||
http_tracker::start_job(&configuration.http_trackers[0], server.tracker.clone()).await; | ||
server | ||
} | ||
|
||
fn start(configuration: &Arc<Configuration>) -> Server { | ||
let connection_info = ConnectionInfo::anonymous(&configuration.http_trackers[0].bind_address.clone()); | ||
|
||
// Set the time of Torrust app starting | ||
lazy_static::initialize(&static_time::TIME_AT_APP_START); | ||
|
||
// Initialize the Ephemeral Instance Random Seed | ||
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_SEED); | ||
|
||
// Initialize stats tracker | ||
let (stats_event_sender, stats_repository) = Keeper::new_active_instance(); | ||
|
||
// Initialize Torrust tracker | ||
let tracker = match tracker::Tracker::new(configuration, Some(stats_event_sender), stats_repository) { | ||
Ok(tracker) => Arc::new(tracker), | ||
Err(error) => { | ||
panic!("{}", error) | ||
} | ||
}; | ||
|
||
// Initialize logging | ||
logging::setup(configuration); | ||
|
||
Server { | ||
tracker, | ||
connection_info, | ||
} | ||
} | ||
|
||
pub struct Server { | ||
pub tracker: Arc<tracker::Tracker>, | ||
pub connection_info: ConnectionInfo, | ||
} | ||
|
||
impl Server { | ||
pub fn get_connection_info(&self) -> ConnectionInfo { | ||
self.connection_info.clone() | ||
} | ||
} |
Oops, something went wrong.