diff --git a/tests/collections.rs b/tests/collections.rs new file mode 100644 index 0000000000..5442e3e478 --- /dev/null +++ b/tests/collections.rs @@ -0,0 +1,51 @@ +//! Custom collections used in our tests. +//! +//! Ideally, we would eventually move these to their own crate. +//! The only reason we have this in the `tests` directory for now, +//! is that it is only used in tests, so should not be compiled into +//! production code. + +use std::collections::HashMap; +use std::hash::Hash; + +/// An unordered collection of items that allows duplicates. +/// This is implemented as a `HashMap` where the keys are the items +/// and the values are the counts of the items. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct HashMultiSet +where + T: Hash + Eq, +{ + pub inner: HashMap, +} + +impl HashMultiSet +where + T: Hash + Eq, +{ + pub fn new() -> Self { + Self { + inner: HashMap::new(), + } + } + + pub fn insert(&mut self, item: T) { + *self.inner.entry(item).or_default() += 1; + } +} + +impl FromIterator for HashMultiSet +where + T: Hash + Eq, +{ + fn from_iter(iter: I) -> Self + where + I: IntoIterator, + { + let mut set = Self::new(); + for item in iter { + set.insert(item); + } + set + } +} diff --git a/tests/integration/test_utils/chunk_upload.rs b/tests/integration/test_utils/chunk_upload.rs index 273da24a9a..532e947d11 100644 --- a/tests/integration/test_utils/chunk_upload.rs +++ b/tests/integration/test_utils/chunk_upload.rs @@ -1,5 +1,4 @@ //! Utilities for chunk upload tests. -use std::collections::HashSet; use std::error::Error; use std::str; use std::sync::LazyLock; @@ -7,6 +6,8 @@ use std::sync::LazyLock; use mockito::Request; use regex::bytes::Regex; +use crate::collections::HashMultiSet; + /// This regex is used to extract the boundary from the content-type header. /// We need to match the boundary, since it changes with each request. /// The regex matches the format as specified in @@ -41,7 +42,7 @@ impl HeaderContainer for Request { pub fn split_chunk_body<'b>( body: &'b [u8], boundary: &str, -) -> Result, Box> { +) -> Result, Box> { let escaped_boundary = regex::escape(boundary); let inner_body = entire_body_regex(&escaped_boundary) diff --git a/tests/mod.rs b/tests/mod.rs index 6d3bbe6043..3e639b7c3a 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1 +1,2 @@ +mod collections; mod integration;