From 97a47dd90d3149153b30c7ace401a8a5bfa85b94 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Thu, 2 Feb 2023 16:59:41 +0000 Subject: [PATCH] refactor(http): new proposal for asserts --- tests/http_tracker.rs | 45 ++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/tests/http_tracker.rs b/tests/http_tracker.rs index 44bb8609d..5fd9e5253 100644 --- a/tests/http_tracker.rs +++ b/tests/http_tracker.rs @@ -25,7 +25,7 @@ mod http_tracker_server { use std::str::FromStr; use local_ip_address::local_ip; - use reqwest::Response; + use reqwest::{Response, StatusCode}; use torrust_tracker::protocol::info_hash::InfoHash; use torrust_tracker::tracker::peer; @@ -321,6 +321,23 @@ mod http_tracker_server { .await; } + /// A wrapper for the HTTP response with only the relevant attributes for the test + #[derive(Debug, PartialEq)] + struct HttpAnnounceResponse { + status: reqwest::StatusCode, + announce: Announce, + } + + impl HttpAnnounceResponse { + pub async fn from(response: Response) -> Self { + let status = response.status(); + let body = response.text().await.unwrap(); + let announce: Announce = serde_bencode::from_str(&body) + .unwrap_or_else(|_| panic!("response body should be a valid announce response, got \"{}\"", &body)); + Self { status, announce } + } + } + #[tokio::test] async fn should_return_the_list_of_previously_announced_peers() { let http_tracker_server = start_public_http_tracker().await; @@ -345,18 +362,20 @@ mod http_tracker_server { ) .await; - // 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![DictionaryPeer::from(previously_announced_peer)], - }, - ) - .await; + // It should only contain the previously announced peer + assert_eq!( + HttpAnnounceResponse::from(response).await, + HttpAnnounceResponse { + status: StatusCode::OK, + announce: 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![DictionaryPeer::from(previously_announced_peer)], + } + } + ); } #[tokio::test]