Skip to content

Commit

Permalink
refactor(http): extract converter
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 25, 2023
1 parent 96fb56c commit 3fce688
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
11 changes: 11 additions & 0 deletions tests/http/responses.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use serde::{self, Deserialize, Serialize};
use torrust_tracker::tracker::peer::Peer;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Announce {
Expand All @@ -19,6 +20,16 @@ pub struct DictionaryPeer {
pub port: u16,
}

impl From<Peer> for DictionaryPeer {
fn from(peer: Peer) -> Self {
DictionaryPeer {
peer_id: peer.peer_id.to_string(),
ip: peer.peer_addr.ip().to_string(),
port: peer.peer_addr.port(),
}
}
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct CompactAnnounce {
pub complete: u32,
Expand Down
12 changes: 3 additions & 9 deletions tests/http_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ mod http_tracker_server {
// Add the Peer 1
http_tracker_server.add_torrent(&info_hash, &previously_announced_peer).await;

// Announce the new Peer 2
// Announce the new Peer 2. This new peer is non included on the response peer list
let response = Client::new(http_tracker_server.get_connection_info())
.announce(
&AnnounceQueryBuilder::default()
Expand All @@ -353,21 +353,15 @@ mod http_tracker_server {
)
.await;

let expected_peer = DictionaryPeer {
peer_id: previously_announced_peer.peer_id.to_string(),
ip: previously_announced_peer.peer_addr.ip().to_string(),
port: previously_announced_peer.peer_addr.port(),
};

// This new peer is non included on the response peer list
// It should only contain teh previously announced peer
assert_announce_response(
response,
&Announce {
complete: 2,
incomplete: 0,
interval: http_tracker_server.tracker.config.announce_interval,
min_interval: http_tracker_server.tracker.config.min_announce_interval,
peers: vec![expected_peer],
peers: vec![DictionaryPeer::from(previously_announced_peer)],
},
)
.await;
Expand Down

0 comments on commit 3fce688

Please sign in to comment.