forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(http): [torrust#159] add tests for public http tracker
- Loading branch information
1 parent
41ad07f
commit 1a558d2
Showing
15 changed files
with
272 additions
and
46 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
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
File renamed without changes.
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod fixtures; | ||
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 |
---|---|---|
@@ -1,7 +1,15 @@ | ||
use reqwest::Response; | ||
|
||
use super::responses::Announce; | ||
|
||
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"); | ||
} | ||
|
||
pub async fn assert_announce_response(response: Response, expected_announce_response: &Announce) { | ||
assert_eq!(response.status(), 200); | ||
let announce_response: Announce = serde_bencode::from_str(&response.text().await.unwrap()).unwrap(); | ||
assert_eq!(announce_response, *expected_announce_response); | ||
} |
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 |
---|---|---|
@@ -1,35 +1,34 @@ | ||
use reqwest::Response; | ||
|
||
use super::connection_info::ConnectionInfo; | ||
use crate::common::http::{get, Query}; | ||
use super::requests::AnnounceQuery; | ||
|
||
/// 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(), | ||
} | ||
Self { connection_info } | ||
} | ||
|
||
pub async fn announce(&self, params: Query) -> Response { | ||
self.get("announce", params).await | ||
pub async fn announce(&self, query: &AnnounceQuery) -> Response { | ||
let path_with_query = format!("announce?{query}"); | ||
self.get(&path_with_query).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 | ||
pub async fn get(&self, path: &str) -> Response { | ||
reqwest::Client::builder() | ||
.build() | ||
.unwrap() | ||
.get(self.base_url(path)) | ||
.send() | ||
.await | ||
.unwrap() | ||
} | ||
|
||
fn base_url(&self, path: &str) -> String { | ||
format!("http://{}{}{path}", &self.connection_info.bind_address, &self.base_path) | ||
format!("http://{}/{path}", &self.connection_info.bind_address) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
pub mod asserts; | ||
pub mod client; | ||
pub mod connection_info; | ||
pub mod requests; | ||
pub mod responses; | ||
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,104 @@ | ||
use std::fmt; | ||
use std::net::IpAddr; | ||
|
||
use percent_encoding::NON_ALPHANUMERIC; | ||
use serde_repr::Serialize_repr; | ||
|
||
pub struct AnnounceQuery { | ||
pub info_hash: ByteArray20, | ||
pub peer_addr: IpAddr, | ||
pub downloaded: BaseTenASCII, | ||
pub uploaded: BaseTenASCII, | ||
pub peer_id: ByteArray20, | ||
pub port: PortNumber, | ||
pub left: BaseTenASCII, | ||
pub event: Option<Event>, | ||
pub compact: Option<Compact>, | ||
} | ||
|
||
impl fmt::Display for AnnounceQuery { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}", self.build()) | ||
} | ||
} | ||
|
||
/// HTTP Tracker Announce Request: | ||
/// | ||
/// <https://wiki.theory.org/BitTorrentSpecification#Tracker_HTTP.2FHTTPS_Protocol> | ||
/// | ||
/// Some parameters are not implemented yet. | ||
impl AnnounceQuery { | ||
/// It builds the URL query component for the announce request. | ||
/// | ||
/// This custom URL query params encoding is needed because `reqwest` does not allow | ||
/// bytes arrays in query parameters. More info on this issue: | ||
/// | ||
/// <https://github.com/seanmonstar/reqwest/issues/1613> | ||
pub fn build(&self) -> String { | ||
let mut params = vec![ | ||
( | ||
"info_hash", | ||
percent_encoding::percent_encode(&self.info_hash, NON_ALPHANUMERIC).to_string(), | ||
), | ||
("peer_addr", self.peer_addr.to_string()), | ||
("downloaded", self.downloaded.to_string()), | ||
("uploaded", self.uploaded.to_string()), | ||
( | ||
"peer_id", | ||
percent_encoding::percent_encode(&self.peer_id, NON_ALPHANUMERIC).to_string(), | ||
), | ||
("port", self.port.to_string()), | ||
("left", self.left.to_string()), | ||
]; | ||
|
||
if let Some(event) = &self.event { | ||
params.push(("event", event.to_string())); | ||
} | ||
|
||
if let Some(compact) = &self.compact { | ||
params.push(("compact", compact.to_string())); | ||
} | ||
|
||
params | ||
.iter() | ||
.map(|param| format!("{}={}", param.0, param.1)) | ||
.collect::<Vec<String>>() | ||
.join("&") | ||
} | ||
} | ||
|
||
pub type BaseTenASCII = u64; | ||
pub type ByteArray20 = [u8; 20]; | ||
pub type PortNumber = u16; | ||
|
||
pub enum Event { | ||
//tarted, | ||
//Stopped, | ||
Completed, | ||
} | ||
|
||
impl fmt::Display for Event { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
//Event::Started => write!(f, "started"), | ||
//Event::Stopped => write!(f, "stopped"), | ||
Event::Completed => write!(f, "completed"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize_repr, PartialEq, Debug)] | ||
#[repr(u8)] | ||
pub enum Compact { | ||
//Accepted = 1, | ||
NotAccepted = 0, | ||
} | ||
|
||
impl fmt::Display for Compact { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
//Compact::Accepted => write!(f, "1"), | ||
Compact::NotAccepted => write!(f, "0"), | ||
} | ||
} | ||
} |
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,18 @@ | ||
use serde::{self, Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
pub struct Announce { | ||
pub complete: u32, | ||
pub incomplete: u32, | ||
pub interval: u32, | ||
#[serde(rename = "min interval")] | ||
pub min_interval: u32, | ||
pub peers: Vec<DictionaryPeer>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
pub struct DictionaryPeer { | ||
pub ip: String, | ||
pub peer_id: String, | ||
pub port: u16, | ||
} |
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
Oops, something went wrong.