Skip to content

Commit c46e417

Browse files
committed
test(http): [#159] add more tests for scrape request
1 parent c24d744 commit c46e417

File tree

5 files changed

+391
-15
lines changed

5 files changed

+391
-15
lines changed

tests/common/fixtures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ impl PeerBuilder {
2727
self
2828
}
2929

30+
#[allow(dead_code)]
31+
pub fn with_no_bytes_pending_to_download(mut self) -> Self {
32+
self.peer.left = NumberOfBytes(0);
33+
self
34+
}
35+
3036
#[allow(dead_code)]
3137
pub fn build(self) -> Peer {
3238
self.into()

tests/http/asserts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub async fn assert_compact_announce_response(response: Response, expected_respo
4848
pub async fn assert_scrape_response(response: Response, expected_response: &scrape::Response) {
4949
assert_eq!(response.status(), 200);
5050

51-
let scrape_response = scrape::Response::try_from_bytes(&response.bytes().await.unwrap()).unwrap();
51+
let scrape_response = scrape::Response::try_from_bencoded(&response.bytes().await.unwrap()).unwrap();
5252

5353
assert_eq!(scrape_response, *expected_response);
5454
}

tests/http/requests/scrape.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ impl QueryBuilder {
5454
self
5555
}
5656

57+
pub fn add_info_hash(mut self, info_hash: &InfoHash) -> Self {
58+
self.scrape_query.info_hash.push(info_hash.0);
59+
self
60+
}
61+
5762
pub fn query(self) -> Query {
5863
self.scrape_query
5964
}
@@ -82,6 +87,12 @@ pub struct QueryParams {
8287
pub info_hash: Vec<String>,
8388
}
8489

90+
impl QueryParams {
91+
pub fn set_one_info_hash_param(&mut self, info_hash: &str) {
92+
self.info_hash = vec![info_hash.to_string()];
93+
}
94+
}
95+
8596
impl std::fmt::Display for QueryParams {
8697
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
8798
let query = self

tests/http/responses/scrape.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,31 @@ pub struct Response {
1212
}
1313

1414
impl Response {
15-
pub fn try_from_bytes(bytes: &[u8]) -> Result<Self, BencodeParseError> {
16-
let scrape_response: DeserializedResponse = serde_bencode::from_bytes(bytes).unwrap();
17-
Self::try_from(scrape_response)
15+
pub fn with_one_file(info_hash_bytes: ByteArray20, file: File) -> Self {
16+
let mut files: HashMap<ByteArray20, File> = HashMap::new();
17+
files.insert(info_hash_bytes, file);
18+
Self { files }
1819
}
1920

20-
pub fn empty() -> Self {
21-
Self::default()
21+
pub fn try_from_bencoded(bytes: &[u8]) -> Result<Self, BencodeParseError> {
22+
let scrape_response: DeserializedResponse = serde_bencode::from_bytes(bytes).unwrap();
23+
Self::try_from(scrape_response)
2224
}
2325
}
2426

25-
#[derive(Serialize, Deserialize, Debug, PartialEq)]
27+
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
2628
pub struct File {
2729
pub complete: i64, // The number of active peers that have completed downloading
2830
pub downloaded: i64, // The number of peers that have ever completed downloading
2931
pub incomplete: i64, // The number of active peers that have not completed downloading
3032
}
3133

34+
impl File {
35+
pub fn zeroed() -> Self {
36+
Self::default()
37+
}
38+
}
39+
3240
impl TryFrom<DeserializedResponse> for Response {
3341
type Error = BencodeParseError;
3442

@@ -49,7 +57,7 @@ pub struct ResponseBuilder {
4957
impl ResponseBuilder {
5058
pub fn default() -> Self {
5159
Self {
52-
response: Response::empty(),
60+
response: Response::default(),
5361
}
5462
}
5563

0 commit comments

Comments
 (0)