use super::super::*;
use crate::metalink::resource::ResourceType;
#[test]
fn test_drop_unsupported_resources() {
let mut file = MetalinkFile::new("test.bin");
file.urls
.push(UrlEntry::new("http://a.com/f").with_resource_type(ResourceType::Http));
file.urls
.push(UrlEntry::new("https://b.com/f").with_resource_type(ResourceType::Https));
file.urls
.push(UrlEntry::new("ftp://c.com/f").with_resource_type(ResourceType::Ftp));
file.urls.push(
UrlEntry::new("magnet:?xt=urn:btih:abc").with_resource_type(ResourceType::BitTorrent),
);
file.urls
.push(UrlEntry::new("http://d.com/f").with_resource_type(ResourceType::NotSupported));
file.urls
.push(UrlEntry::new("http://e.com/f").with_resource_type(ResourceType::Unknown));
file.drop_unsupported_resources();
assert_eq!(file.urls.len(), 4);
assert!(file.urls.iter().all(|u| u.is_supported()));
}
#[test]
fn test_set_location_priority() {
let mut file = MetalinkFile::new("test.bin");
file.urls.push(
UrlEntry::new("http://a.com/f")
.with_location("cn")
.with_priority(10),
);
file.urls.push(
UrlEntry::new("http://b.com/f")
.with_location("us")
.with_priority(10),
);
file.urls.push(
UrlEntry::new("http://c.com/f")
.with_location("jp")
.with_priority(10),
);
file.urls.push(UrlEntry::new("http://d.com/f"));
file.set_location_priority(&["cn", "jp"], -999999);
assert_eq!(file.urls[0].priority, 10 - 999999);
assert_eq!(file.urls[1].priority, 10); assert_eq!(file.urls[2].priority, 10 - 999999);
assert_eq!(file.urls[3].priority, 999999); }
#[test]
fn test_set_protocol_priority() {
let mut file = MetalinkFile::new("test.bin");
file.urls.push(
UrlEntry::new("http://a.com/f")
.with_resource_type(ResourceType::Http)
.with_priority(10),
);
file.urls.push(
UrlEntry::new("https://b.com/f")
.with_resource_type(ResourceType::Https)
.with_priority(10),
);
file.urls.push(
UrlEntry::new("ftp://c.com/f")
.with_resource_type(ResourceType::Ftp)
.with_priority(10),
);
file.set_protocol_priority("https", -999999);
assert_eq!(file.urls[0].priority, 10);
assert_eq!(file.urls[1].priority, 10 - 999999);
assert_eq!(file.urls[2].priority, 10);
}
#[test]
fn test_reorder_resources_by_priority() {
let mut file = MetalinkFile::new("test.bin");
file.urls
.push(UrlEntry::new("http://a.com/f").with_priority(30));
file.urls
.push(UrlEntry::new("http://b.com/f").with_priority(10));
file.urls
.push(UrlEntry::new("http://c.com/f").with_priority(20));
file.reorder_resources_by_priority();
assert_eq!(file.urls[0].priority, 10);
assert_eq!(file.urls[1].priority, 20);
assert_eq!(file.urls[2].priority, 30);
}
#[test]
fn test_reorder_metaurls_by_priority() {
let mut file = MetalinkFile::new("test.bin");
file.meta_urls
.push(MetaUrlEntry::new("http://a.com/torrent", MediaType::Torrent).with_priority(30));
file.meta_urls
.push(MetaUrlEntry::new("http://b.com/torrent", MediaType::Torrent).with_priority(10));
file.meta_urls
.push(MetaUrlEntry::new("http://c.com/torrent", MediaType::Torrent).with_priority(20));
file.reorder_metaurls_by_priority();
assert_eq!(file.meta_urls[0].priority, 10);
assert_eq!(file.meta_urls[1].priority, 20);
assert_eq!(file.meta_urls[2].priority, 30);
}
#[test]
fn test_group_entry_no_metaurls() {
let mut f1 = MetalinkFile::new("a.bin");
f1.size = Some(100);
f1.size_known = true;
let mut f2 = MetalinkFile::new("b.bin");
f2.size = Some(200);
f2.size_known = true;
let groups = group_entry_by_metaurl_name(&[f1, f2]);
assert_eq!(groups.len(), 2);
assert_eq!(groups[0].0, "");
assert_eq!(groups[0].1, vec![0]);
assert_eq!(groups[1].0, "");
assert_eq!(groups[1].1, vec![1]);
}
#[test]
fn test_group_entry_same_metaurl_merges() {
let mut f1 = MetalinkFile::new("a.bin");
f1.size = Some(100);
f1.size_known = true;
f1.meta_urls.push(
MetaUrlEntry::new(
"http://torrent.example.com/meta.torrent",
MediaType::Torrent,
)
.with_name("a.bin")
.with_priority(1),
);
let mut f2 = MetalinkFile::new("b.bin");
f2.size = Some(200);
f2.size_known = true;
f2.meta_urls.push(
MetaUrlEntry::new(
"http://torrent.example.com/meta.torrent",
MediaType::Torrent,
)
.with_name("b.bin")
.with_priority(1),
);
let groups = group_entry_by_metaurl_name(&[f1, f2]);
assert_eq!(groups.len(), 1);
assert_eq!(groups[0].0, "http://torrent.example.com/meta.torrent");
assert_eq!(groups[0].1, vec![0, 1]);
}
#[test]
fn test_group_entry_empty_name_no_merge() {
let mut f1 = MetalinkFile::new("a.bin");
f1.size = Some(100);
f1.size_known = true;
f1.meta_urls.push(
MetaUrlEntry::new(
"http://torrent.example.com/meta.torrent",
MediaType::Torrent,
)
.with_priority(1),
);
let mut f2 = MetalinkFile::new("b.bin");
f2.size = Some(200);
f2.size_known = true;
f2.meta_urls.push(
MetaUrlEntry::new(
"http://torrent.example.com/meta.torrent",
MediaType::Torrent,
)
.with_name("b.bin")
.with_priority(1),
);
let groups = group_entry_by_metaurl_name(&[f1, f2]);
assert_eq!(groups.len(), 2);
}
#[test]
fn test_group_entry_size_unknown_no_merge() {
let mut f1 = MetalinkFile::new("a.bin");
f1.size_known = false; f1.meta_urls.push(
MetaUrlEntry::new(
"http://torrent.example.com/meta.torrent",
MediaType::Torrent,
)
.with_name("a.bin")
.with_priority(1),
);
let mut f2 = MetalinkFile::new("b.bin");
f2.size = Some(200);
f2.size_known = true;
f2.meta_urls.push(
MetaUrlEntry::new(
"http://torrent.example.com/meta.torrent",
MediaType::Torrent,
)
.with_name("b.bin")
.with_priority(1),
);
let groups = group_entry_by_metaurl_name(&[f1, f2]);
assert_eq!(groups.len(), 1);
assert_eq!(groups[0].1, vec![0, 1]);
}
#[test]
fn test_group_entry_size_unknown_and_no_name() {
let mut f1 = MetalinkFile::new("a.bin");
f1.size_known = false;
f1.meta_urls.push(
MetaUrlEntry::new(
"http://torrent.example.com/meta.torrent",
MediaType::Torrent,
)
.with_priority(1), );
let mut f2 = MetalinkFile::new("b.bin");
f2.size = Some(200);
f2.size_known = true;
f2.meta_urls.push(
MetaUrlEntry::new(
"http://torrent.example.com/meta.torrent",
MediaType::Torrent,
)
.with_name("b.bin")
.with_priority(1),
);
let groups = group_entry_by_metaurl_name(&[f1, f2]);
assert_eq!(groups.len(), 2);
}
#[test]
fn test_group_entry_different_metaurl_no_merge() {
let mut f1 = MetalinkFile::new("a.bin");
f1.size = Some(100);
f1.size_known = true;
f1.meta_urls.push(
MetaUrlEntry::new("http://a.com/meta.torrent", MediaType::Torrent)
.with_name("a.bin")
.with_priority(1),
);
let mut f2 = MetalinkFile::new("b.bin");
f2.size = Some(200);
f2.size_known = true;
f2.meta_urls.push(
MetaUrlEntry::new("http://b.com/meta.torrent", MediaType::Torrent)
.with_name("b.bin")
.with_priority(1),
);
let groups = group_entry_by_metaurl_name(&[f1, f2]);
assert_eq!(groups.len(), 2);
}