Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions crates/s3s/src/dto/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,73 @@ mod tests {
}
}
}

#[test]
fn to_header_string_int_inclusive() {
let range = range_int_inclusive(0, 499);
assert_eq!(range.to_header_string(), "bytes=0-499");
}

#[test]
fn to_header_string_int_from() {
let range = range_int_from(9500);
assert_eq!(range.to_header_string(), "bytes=9500-");
}

#[test]
fn to_header_string_suffix() {
let range = range_suffix(500);
assert_eq!(range.to_header_string(), "bytes=-500");
}

#[test]
fn to_header_string_roundtrip() {
let cases = [range_int_inclusive(0, 499), range_int_from(9500), range_suffix(500)];
for range in &cases {
let header = range.to_header_string();
let parsed = Range::parse(&header).unwrap();
assert_eq!(*range, parsed);
}
}

#[test]
fn try_from_header_value() {
use crate::http::TryFromHeaderValue;
let hv = http::HeaderValue::from_static("bytes=0-499");
let range = Range::try_from_header_value(&hv).unwrap();
assert_eq!(range, range_int_inclusive(0, 499));

let hv = http::HeaderValue::from_static("bytes=100-");
let range = Range::try_from_header_value(&hv).unwrap();
assert_eq!(range, range_int_from(100));

let hv = http::HeaderValue::from_static("bytes=-500");
let range = Range::try_from_header_value(&hv).unwrap();
assert_eq!(range, range_suffix(500));
}

#[test]
fn range_not_satisfiable_to_s3_error() {
let err = RangeNotSatisfiable { _priv: () };
let s3_err: S3Error = err.into();
let code = s3_err.code();
assert_eq!(code, &S3ErrorCode::InvalidRange);
}

#[test]
fn parse_first_exceeds_i64_max() {
let big = format!("bytes={}-", i64::MAX as u64 + 1);
assert!(Range::parse(&big).is_err());
}

#[test]
fn parse_last_exceeds_i64_max() {
let big = format!("bytes=0-{}", i64::MAX as u64 + 1);
assert!(Range::parse(&big).is_err());
}

#[test]
fn parse_first_greater_than_last() {
assert!(Range::parse("bytes=500-100").is_err());
}
}
89 changes: 89 additions & 0 deletions crates/s3s/src/dto/streaming_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,92 @@ where
{
Box::pin(StreamWrapper { inner })
}

#[cfg(test)]
mod tests {
use super::*;
use futures::StreamExt;
use http_body::Body as HttpBody;

#[tokio::test]
async fn streaming_blob_new_and_poll() {
let body = Body::from(Bytes::from_static(b"hello world"));
let mut blob = StreamingBlob::new(body);
let mut collected = Vec::new();
while let Some(chunk) = blob.next().await {
collected.push(chunk.unwrap());
}
assert_eq!(collected, vec![Bytes::from_static(b"hello world")]);
}

#[tokio::test]
async fn streaming_blob_wrap() {
let data = vec![
Ok::<_, std::io::Error>(Bytes::from_static(b"abc")),
Ok(Bytes::from_static(b"def")),
];
let stream = futures::stream::iter(data);
let mut blob = StreamingBlob::wrap(stream);
let mut collected = Vec::new();
while let Some(chunk) = blob.next().await {
collected.push(chunk.unwrap());
}
assert_eq!(collected, vec![Bytes::from_static(b"abc"), Bytes::from_static(b"def")]);
}

#[test]
fn streaming_blob_debug() {
let body = Body::from(Bytes::from_static(b"test"));
let blob = StreamingBlob::new(body);
let debug = format!("{blob:?}");
assert!(debug.contains("StreamingBlob"));
assert!(debug.contains("remaining_length"));
}

#[test]
fn streaming_blob_remaining_length() {
let body = Body::from(Bytes::from_static(b"hello"));
let blob = StreamingBlob::new(body);
let rl = blob.remaining_length();
assert_eq!(rl.exact(), Some(5));
}

#[test]
fn streaming_blob_from_body_roundtrip() {
let body = Body::from(Bytes::from_static(b"data"));
let blob = StreamingBlob::from(body);
let body_back: Body = Body::from(blob);
assert!(!HttpBody::is_end_stream(&body_back));
}

#[test]
fn streaming_blob_into_dyn_byte_stream() {
let body = Body::from(Bytes::from_static(b"test"));
let blob = StreamingBlob::new(body);
let _dyn_stream: DynByteStream = blob.into();
}

#[test]
fn streaming_blob_from_dyn_byte_stream() {
let body = Body::from(Bytes::from_static(b"test"));
let dyn_stream: DynByteStream = Box::pin(body);
let _blob = StreamingBlob::from(dyn_stream);
}

#[test]
fn streaming_blob_size_hint() {
let body = Body::from(Bytes::from_static(b"12345"));
let blob = StreamingBlob::new(body);
let (lower, _upper) = blob.size_hint();
// Body::Once with 5 bytes - size_hint is from DynByteStream
let _ = lower;
}

#[tokio::test]
async fn streaming_blob_empty() {
let body = Body::empty();
let mut blob = StreamingBlob::new(body);
let next = blob.next().await;
assert!(next.is_none());
}
}
159 changes: 159 additions & 0 deletions crates/s3s/src/dto/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,163 @@ mod tests {
assert_eq!(expected, text);
}
}

#[test]
fn parse_epoch_seconds_integer() {
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "0").unwrap();
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt, time::OffsetDateTime::UNIX_EPOCH);
}

#[test]
fn parse_epoch_seconds_negative() {
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "-1").unwrap();
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt.unix_timestamp(), -1);
}

#[test]
fn parse_epoch_seconds_fractional_lengths() {
// 1 digit fractional
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "100.5").unwrap();
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt.unix_timestamp(), 100);
assert_eq!(dt.nanosecond(), 500_000_000);

// 3 digits
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "100.123").unwrap();
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt.nanosecond(), 123_000_000);

// 6 digits
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "100.123456").unwrap();
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt.nanosecond(), 123_456_000);

// 9 digits
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "100.123456789").unwrap();
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt.nanosecond(), 123_456_789);
}

#[test]
fn parse_epoch_seconds_overflow_fractional() {
// 10 digits should fail
let result = Timestamp::parse(TimestampFormat::EpochSeconds, "100.1234567890");
assert!(result.is_err());
}

#[test]
fn parse_datetime_invalid() {
let result = Timestamp::parse(TimestampFormat::DateTime, "not-a-date");
assert!(result.is_err());
}

#[test]
fn parse_http_date_invalid() {
let result = Timestamp::parse(TimestampFormat::HttpDate, "not-a-date");
assert!(result.is_err());
}

#[test]
fn parse_epoch_seconds_invalid() {
let result = Timestamp::parse(TimestampFormat::EpochSeconds, "abc");
assert!(result.is_err());
}

#[test]
fn from_system_time() {
let st = SystemTime::UNIX_EPOCH;
let ts = Timestamp::from(st);
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt, time::OffsetDateTime::UNIX_EPOCH);
}

#[test]
fn from_offset_datetime() {
let odt = time::OffsetDateTime::UNIX_EPOCH;
let ts = Timestamp::from(odt);
let back: time::OffsetDateTime = ts.into();
assert_eq!(back, time::OffsetDateTime::UNIX_EPOCH);
}

#[test]
fn serde_roundtrip() {
let ts = Timestamp::parse(TimestampFormat::DateTime, "1985-04-12T23:20:50.520Z").unwrap();
let json = serde_json::to_string(&ts).unwrap();
assert!(json.contains("1985-04-12"));
let parsed: Timestamp = serde_json::from_str(&json).unwrap();
assert_eq!(ts, parsed);
}

#[test]
fn serde_deserialize_invalid() {
let result: Result<Timestamp, _> = serde_json::from_str("\"not-a-date\"");
assert!(result.is_err());
}

#[test]
fn format_epoch_seconds() {
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "0").unwrap();
let mut buf = Vec::new();
ts.format(TimestampFormat::EpochSeconds, &mut buf).unwrap();
let text = String::from_utf8(buf).unwrap();
assert_eq!(text, "0");
}

#[test]
fn timestamp_ord() {
let ts1 = Timestamp::parse(TimestampFormat::EpochSeconds, "100").unwrap();
let ts2 = Timestamp::parse(TimestampFormat::EpochSeconds, "200").unwrap();
assert!(ts1 < ts2);
assert_eq!(ts1, ts1.clone());
}

#[test]
fn timestamp_hash() {
use std::collections::HashSet;
let ts1 = Timestamp::parse(TimestampFormat::EpochSeconds, "100").unwrap();
let ts2 = Timestamp::parse(TimestampFormat::EpochSeconds, "100").unwrap();
let mut set = HashSet::new();
set.insert(ts1);
set.insert(ts2);
assert_eq!(set.len(), 1);
}

#[test]
fn parse_epoch_seconds_all_frac_lengths() {
// 2 digit fractional
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "100.12").unwrap();
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt.nanosecond(), 120_000_000);

// 4 digits
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "100.1234").unwrap();
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt.nanosecond(), 123_400_000);

// 5 digits
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "100.12345").unwrap();
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt.nanosecond(), 123_450_000);

// 7 digits
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "100.1234567").unwrap();
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt.nanosecond(), 123_456_700);

// 8 digits
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "100.12345678").unwrap();
let dt: time::OffsetDateTime = ts.into();
assert_eq!(dt.nanosecond(), 123_456_780);
}

#[test]
fn parse_epoch_negative_with_frac() {
// "-1.5" means -1 seconds + 0.5 fractional = -0.5 seconds
let ts = Timestamp::parse(TimestampFormat::EpochSeconds, "-1.5").unwrap();
let dt: time::OffsetDateTime = ts.into();
let nanos = dt.unix_timestamp_nanos();
assert_eq!(nanos, -500_000_000);
}
}
Loading