Skip to content

Commit

Permalink
refactor(http): [#159] move mods to folders
Browse files Browse the repository at this point in the history
We will use one mod per type of request and response.
  • Loading branch information
josecelano committed Jan 30, 2023
1 parent 953a100 commit d7610ef
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 67 deletions.
9 changes: 4 additions & 5 deletions tests/http/asserts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use reqwest::Response;

use super::responses::{Announce, DecodedCompactAnnounce};
use crate::http::responses::{CompactAnnounce, Error};
use super::responses::announce::{Announce, Compact, DecodedCompact, Error};

pub async fn assert_empty_announce_response(response: Response) {
assert_eq!(response.status(), 200);
Expand All @@ -22,18 +21,18 @@ pub async fn assert_announce_response(response: Response, expected_announce_resp
/// ```text
/// b"d8:intervali120e12:min intervali120e8:completei2e10:incompletei0e5:peers6:~\0\0\x01\x1f\x90e6:peers60:e"
/// ```
pub async fn assert_compact_announce_response(response: Response, expected_response: &DecodedCompactAnnounce) {
pub async fn assert_compact_announce_response(response: Response, expected_response: &DecodedCompact) {
assert_eq!(response.status(), 200);

let bytes = response.bytes().await.unwrap();

let compact_announce: CompactAnnounce = serde_bencode::from_bytes(&bytes).unwrap_or_else(|_| {
let compact_announce: Compact = serde_bencode::from_bytes(&bytes).unwrap_or_else(|_| {
panic!(
"response body should be a valid compact announce response, got \"{:?}\"",
&bytes
)
});
let actual_response = DecodedCompactAnnounce::from(compact_announce);
let actual_response = DecodedCompact::from(compact_announce);

assert_eq!(actual_response, *expected_response);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use reqwest::{Client as ReqwestClient, Response};
use torrust_tracker::tracker::auth::KeyId;

use super::connection_info::ConnectionInfo;
use super::requests::AnnounceQuery;
use super::requests::announce::Query;

/// HTTP Tracker Client
pub struct Client {
Expand Down Expand Up @@ -47,11 +47,11 @@ impl Client {
}
}

pub async fn announce(&self, query: &AnnounceQuery) -> Response {
pub async fn announce(&self, query: &Query) -> Response {
self.get(&self.build_announce_path_and_query(query)).await
}

pub async fn announce_with_header(&self, query: &AnnounceQuery, key_id: &str, value: &str) -> Response {
pub async fn announce_with_header(&self, query: &Query, key_id: &str, value: &str) -> Response {
self.get_with_header(&self.build_announce_path_and_query(query), key_id, value)
.await
}
Expand All @@ -69,7 +69,7 @@ impl Client {
.unwrap()
}

fn build_announce_path_and_query(&self, query: &AnnounceQuery) -> String {
fn build_announce_path_and_query(&self, query: &Query) -> String {
format!("{}?{query}", self.build_path("announce"))
}

Expand Down
30 changes: 15 additions & 15 deletions tests/http/requests.rs → tests/http/requests/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde_repr::Serialize_repr;
use torrust_tracker::protocol::info_hash::InfoHash;
use torrust_tracker::tracker::peer::Id;

pub struct AnnounceQuery {
pub struct Query {
pub info_hash: ByteArray20,
pub peer_addr: IpAddr,
pub downloaded: BaseTenASCII,
Expand All @@ -19,7 +19,7 @@ pub struct AnnounceQuery {
pub compact: Option<Compact>,
}

impl fmt::Display for AnnounceQuery {
impl fmt::Display for Query {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.build())
}
Expand All @@ -30,7 +30,7 @@ impl fmt::Display for AnnounceQuery {
/// <https://wiki.theory.org/BitTorrentSpecification#Tracker_HTTP.2FHTTPS_Protocol>
///
/// Some parameters in the specification are not implemented in this tracker yet.
impl AnnounceQuery {
impl Query {
/// It builds the URL query component for the announce request.
///
/// This custom URL query params encoding is needed because `reqwest` does not allow
Expand All @@ -41,8 +41,8 @@ impl AnnounceQuery {
self.params().to_string()
}

pub fn params(&self) -> AnnounceQueryParams {
AnnounceQueryParams::from(self)
pub fn params(&self) -> QueryParams {
QueryParams::from(self)
}
}

Expand Down Expand Up @@ -82,13 +82,13 @@ impl fmt::Display for Compact {
}
}

pub struct AnnounceQueryBuilder {
announce_query: AnnounceQuery,
pub struct QueryBuilder {
announce_query: Query,
}

impl AnnounceQueryBuilder {
pub fn default() -> AnnounceQueryBuilder {
let default_announce_query = AnnounceQuery {
impl QueryBuilder {
pub fn default() -> QueryBuilder {
let default_announce_query = Query {
info_hash: InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap().0,
peer_addr: IpAddr::V4(Ipv4Addr::new(192, 168, 1, 88)),
downloaded: 0,
Expand Down Expand Up @@ -129,7 +129,7 @@ impl AnnounceQueryBuilder {
self
}

pub fn query(self) -> AnnounceQuery {
pub fn query(self) -> Query {
self.announce_query
}
}
Expand All @@ -150,7 +150,7 @@ impl AnnounceQueryBuilder {
/// event=completed
/// compact=0
/// ```
pub struct AnnounceQueryParams {
pub struct QueryParams {
pub info_hash: Option<String>,
pub peer_addr: Option<String>,
pub downloaded: Option<String>,
Expand All @@ -162,7 +162,7 @@ pub struct AnnounceQueryParams {
pub compact: Option<String>,
}

impl std::fmt::Display for AnnounceQueryParams {
impl std::fmt::Display for QueryParams {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut params = vec![];

Expand Down Expand Up @@ -204,8 +204,8 @@ impl std::fmt::Display for AnnounceQueryParams {
}
}

impl AnnounceQueryParams {
pub fn from(announce_query: &AnnounceQuery) -> Self {
impl QueryParams {
pub fn from(announce_query: &Query) -> Self {
let event = announce_query.event.as_ref().map(std::string::ToString::to_string);
let compact = announce_query.compact.as_ref().map(std::string::ToString::to_string);

Expand Down
1 change: 1 addition & 0 deletions tests/http/requests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod announce;
8 changes: 4 additions & 4 deletions tests/http/responses.rs → tests/http/responses/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl From<Peer> for DictionaryPeer {
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct CompactAnnounce {
pub struct Compact {
pub complete: u32,
pub incomplete: u32,
pub interval: u32,
Expand All @@ -42,7 +42,7 @@ pub struct CompactAnnounce {
}

#[derive(Debug, PartialEq)]
pub struct DecodedCompactAnnounce {
pub struct DecodedCompact {
// code-review: there could be a way to deserialize this struct directly
// by using serde instead of doing it manually. Or at least using a custom deserializer.
pub complete: u32,
Expand Down Expand Up @@ -88,8 +88,8 @@ impl CompactPeer {
}
}

impl From<CompactAnnounce> for DecodedCompactAnnounce {
fn from(compact_announce: CompactAnnounce) -> Self {
impl From<Compact> for DecodedCompact {
fn from(compact_announce: Compact) -> Self {
let mut peers = vec![];

for peer_bytes in compact_announce.peers.chunks_exact(6) {
Expand Down
1 change: 1 addition & 0 deletions tests/http/responses/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod announce;
Loading

0 comments on commit d7610ef

Please sign in to comment.