Skip to content

Commit 1aef0c1

Browse files
committed
refactor: [torrust#1316] move HTTP tracker integration tests to pkg
1 parent 92505b9 commit 1aef0c1

File tree

25 files changed

+172
-103
lines changed

25 files changed

+172
-103
lines changed

Cargo.lock

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-8
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ version = "3.0.0-develop"
3434

3535
[dependencies]
3636
anyhow = "1"
37-
aquatic_udp_protocol = "0"
3837
axum-server = { version = "0", features = ["tls-rustls-no-provider"] }
3938
bittorrent-http-tracker-core = { version = "3.0.0-develop", path = "packages/http-tracker-core" }
40-
bittorrent-primitives = "0.1.0"
4139
bittorrent-tracker-core = { version = "3.0.0-develop", path = "packages/tracker-core" }
4240
bittorrent-udp-tracker-core = { version = "3.0.0-develop", path = "packages/udp-tracker-core" }
4341
chrono = { version = "0", default-features = false, features = ["clock"] }
@@ -47,18 +45,15 @@ dashmap = "6"
4745
figment = "0"
4846
futures = "0"
4947
parking_lot = "0"
50-
percent-encoding = "2"
5148
r2d2 = "0"
5249
r2d2_mysql = "25"
5350
r2d2_sqlite = { version = "0", features = ["bundled"] }
5451
rand = "0"
5552
regex = "1"
5653
reqwest = { version = "0", features = ["json"] }
5754
serde = { version = "1", features = ["derive"] }
58-
serde_bencode = "0"
5955
serde_bytes = "0"
6056
serde_json = { version = "1", features = ["preserve_order"] }
61-
serde_repr = "0"
6257
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
6358
torrust-axum-health-check-api-server = { version = "3.0.0-develop", path = "packages/axum-health-check-api-server" }
6459
torrust-axum-http-tracker-server = { version = "3.0.0-develop", path = "packages/axum-http-tracker-server" }
@@ -68,13 +63,10 @@ torrust-server-lib = { version = "3.0.0-develop", path = "packages/server-lib" }
6863
torrust-tracker-api-core = { version = "3.0.0-develop", path = "packages/tracker-api-core" }
6964
torrust-tracker-clock = { version = "3.0.0-develop", path = "packages/clock" }
7065
torrust-tracker-configuration = { version = "3.0.0-develop", path = "packages/configuration" }
71-
torrust-tracker-primitives = { version = "3.0.0-develop", path = "packages/primitives" }
7266
torrust-tracker-torrent-repository = { version = "3.0.0-develop", path = "packages/torrent-repository" }
7367
torrust-udp-tracker-server = { version = "3.0.0-develop", path = "packages/udp-tracker-server" }
7468
tracing = "0"
7569
tracing-subscriber = { version = "0", features = ["json"] }
76-
uuid = { version = "1", features = ["v4"] }
77-
zerocopy = "0.7"
7870

7971
[package.metadata.cargo-machete]
8072
ignored = [

packages/axum-http-tracker-server/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,21 @@ serde = { version = "1", features = ["derive"] }
3030
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
3131
torrust-axum-server = { version = "3.0.0-develop", path = "../axum-server" }
3232
torrust-server-lib = { version = "3.0.0-develop", path = "../server-lib" }
33+
torrust-tracker-clock = { version = "3.0.0-develop", path = "../clock" }
3334
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
3435
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
3536
tower = { version = "0", features = ["timeout"] }
3637
tower-http = { version = "0", features = ["compression-full", "cors", "propagate-header", "request-id", "trace"] }
3738
tracing = "0"
3839

3940
[dev-dependencies]
41+
local-ip-address = "0"
42+
percent-encoding = "2"
43+
rand = "0"
44+
serde_bencode = "0"
45+
serde_bytes = "0"
46+
serde_repr = "0"
4047
torrust-tracker-clock = { version = "3.0.0-develop", path = "../clock" }
4148
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-helpers" }
49+
uuid = { version = "1", features = ["v4"] }
50+
zerocopy = "0.7"

tests/servers/http/environment.rs packages/axum-http-tracker-server/src/environment.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ use bittorrent_http_tracker_core::container::HttpTrackerCoreContainer;
44
use bittorrent_primitives::info_hash::InfoHash;
55
use bittorrent_tracker_core::container::TrackerCoreContainer;
66
use futures::executor::block_on;
7-
use torrust_axum_http_tracker_server::server::{HttpServer, Launcher, Running, Stopped};
87
use torrust_axum_server::tsl::make_rust_tls;
98
use torrust_server_lib::registar::Registar;
109
use torrust_tracker_configuration::{logging, Configuration};
1110
use torrust_tracker_primitives::peer;
1211

12+
use crate::server::{HttpServer, Launcher, Running, Stopped};
13+
14+
pub type Started = Environment<Running>;
15+
1316
pub struct Environment<S> {
1417
pub container: Arc<EnvContainer>,
1518
pub registar: Registar,
@@ -28,7 +31,11 @@ impl<S> Environment<S> {
2831
}
2932

3033
impl Environment<Stopped> {
34+
/// # Panics
35+
///
36+
/// Will panic if it fails to make the TSL config from the configuration.
3137
#[allow(dead_code)]
38+
#[must_use]
3239
pub fn new(configuration: &Arc<Configuration>) -> Self {
3340
initialize_global_services(configuration);
3441

@@ -50,6 +57,9 @@ impl Environment<Stopped> {
5057
}
5158
}
5259

60+
/// # Panics
61+
///
62+
/// Will panic if the server fails to start.
5363
#[allow(dead_code)]
5464
pub async fn start(self) -> Environment<Running> {
5565
Environment {
@@ -69,6 +79,9 @@ impl Environment<Running> {
6979
Environment::<Stopped>::new(configuration).start().await
7080
}
7181

82+
/// # Panics
83+
///
84+
/// Will panic if the server fails to stop.
7285
pub async fn stop(self) -> Environment<Stopped> {
7386
Environment {
7487
container: self.container,
@@ -77,6 +90,7 @@ impl Environment<Running> {
7790
}
7891
}
7992

93+
#[must_use]
8094
pub fn bind_address(&self) -> &std::net::SocketAddr {
8195
&self.server.state.binding
8296
}
@@ -88,6 +102,10 @@ pub struct EnvContainer {
88102
}
89103

90104
impl EnvContainer {
105+
/// # Panics
106+
///
107+
/// Will panic if the configuration is missing the HTTP tracker configuration.
108+
#[must_use]
91109
pub fn initialize(configuration: &Configuration) -> Self {
92110
let core_config = Arc::new(configuration.core.clone());
93111
let http_tracker_config = configuration

packages/axum-http-tracker-server/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,12 @@
303303
//!
304304
//! - [Bencode](https://en.wikipedia.org/wiki/Bencode).
305305
//! - [Bencode to Json Online converter](https://chocobo1.github.io/bencode_online).
306-
use serde::{Deserialize, Serialize};
307-
306+
pub mod environment;
308307
pub mod server;
309308
pub mod v1;
310309

310+
use serde::{Deserialize, Serialize};
311+
311312
pub const HTTP_TRACKER_LOG_TARGET: &str = "HTTP TRACKER";
312313

313314
/// The version of the HTTP tracker.

tests/common/fixtures.rs packages/axum-http-tracker-server/tests/common/fixtures.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bittorrent_primitives::info_hash::InfoHash;
2+
use rand::prelude::*;
23

3-
#[allow(dead_code)]
44
pub fn invalid_info_hashes() -> Vec<String> {
55
[
66
"0".to_string(),
@@ -15,8 +15,8 @@ pub fn invalid_info_hashes() -> Vec<String> {
1515

1616
/// Returns a random info hash.
1717
pub fn random_info_hash() -> InfoHash {
18-
let mut rng = rand::rng();
19-
let random_bytes: [u8; 20] = rand::Rng::random(&mut rng);
18+
let mut rng = rand::thread_rng();
19+
let random_bytes: [u8; 20] = rng.gen();
2020

2121
InfoHash::from_bytes(&random_bytes)
2222
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
pub type ReqwestQuery = Vec<ReqwestQueryParam>;
2+
pub type ReqwestQueryParam = (String, String);
3+
4+
/// URL Query component
5+
#[derive(Default, Debug)]
6+
pub struct Query {
7+
params: Vec<QueryParam>,
8+
}
9+
10+
impl Query {
11+
pub fn empty() -> Self {
12+
Self { params: vec![] }
13+
}
14+
15+
pub fn params(params: Vec<QueryParam>) -> Self {
16+
Self { params }
17+
}
18+
19+
pub fn add_param(&mut self, param: QueryParam) {
20+
self.params.push(param);
21+
}
22+
}
23+
24+
impl From<Query> for ReqwestQuery {
25+
fn from(url_search_params: Query) -> Self {
26+
url_search_params
27+
.params
28+
.iter()
29+
.map(|param| ReqwestQueryParam::from((*param).clone()))
30+
.collect()
31+
}
32+
}
33+
34+
/// URL query param
35+
#[derive(Clone, Debug)]
36+
pub struct QueryParam {
37+
name: String,
38+
value: String,
39+
}
40+
41+
impl QueryParam {
42+
pub fn new(name: &str, value: &str) -> Self {
43+
Self {
44+
name: name.to_string(),
45+
value: value.to_string(),
46+
}
47+
}
48+
}
49+
50+
impl From<QueryParam> for ReqwestQueryParam {
51+
fn from(param: QueryParam) -> Self {
52+
(param.name, param.value)
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod fixtures;
2+
pub mod http;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Integration tests.
2+
//!
3+
//! ```text
4+
//! cargo test --test integration
5+
//! ```
6+
mod common;
7+
mod server;
8+
9+
use torrust_tracker_clock::clock;
10+
11+
/// This code needs to be copied into each crate.
12+
/// Working version, for production.
13+
#[cfg(not(test))]
14+
#[allow(dead_code)]
15+
pub(crate) type CurrentClock = clock::Working;
16+
17+
/// Stopped version, for testing.
18+
#[cfg(test)]
19+
#[allow(dead_code)]
20+
pub(crate) type CurrentClock = clock::Stopped;

tests/servers/http/asserts.rs packages/axum-http-tracker-server/tests/server/asserts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use reqwest::Response;
44

55
use super::responses::announce::{Announce, Compact, DeserializedCompact};
66
use super::responses::scrape;
7-
use crate::servers::http::responses::error::Error;
7+
use crate::server::responses::error::Error;
88

99
pub fn assert_bencoded_error(response_text: &String, expected_failure_reason: &str, location: &'static Location<'static>) {
1010
let error_failure_reason = serde_bencode::from_str::<Error>(response_text)

tests/servers/http/mod.rs packages/axum-http-tracker-server/tests/server/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
pub mod asserts;
22
pub mod client;
3-
pub mod environment;
43
pub mod requests;
54
pub mod responses;
65
pub mod v1;
76

87
use percent_encoding::NON_ALPHANUMERIC;
9-
use torrust_axum_http_tracker_server::server;
10-
11-
pub type Started = environment::Environment<server::Running>;
128

139
pub type ByteArray20 = [u8; 20];
1410

tests/servers/http/requests/announce.rs packages/axum-http-tracker-server/tests/server/requests/announce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use aquatic_udp_protocol::PeerId;
66
use bittorrent_primitives::info_hash::InfoHash;
77
use serde_repr::Serialize_repr;
88

9-
use crate::servers::http::{percent_encode_byte_array, ByteArray20};
9+
use crate::server::{percent_encode_byte_array, ByteArray20};
1010

1111
pub struct Query {
1212
pub info_hash: ByteArray20,

tests/servers/http/requests/scrape.rs packages/axum-http-tracker-server/tests/server/requests/scrape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::str::FromStr;
33

44
use bittorrent_primitives::info_hash::InfoHash;
55

6-
use crate::servers::http::{percent_encode_byte_array, ByteArray20};
6+
use crate::server::{percent_encode_byte_array, ByteArray20};
77

88
pub struct Query {
99
pub info_hash: Vec<ByteArray20>,

tests/servers/http/responses/scrape.rs packages/axum-http-tracker-server/tests/server/responses/scrape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::str;
44
use serde::{Deserialize, Serialize};
55
use serde_bencode::value::Value;
66

7-
use crate::servers::http::{ByteArray20, InfoHash};
7+
use crate::server::{ByteArray20, InfoHash};
88

99
#[derive(Debug, PartialEq, Default)]
1010
pub struct Response {

0 commit comments

Comments
 (0)