Skip to content

Commit 6c79bbf

Browse files
committed
test(decompression-plz): moved utils to new shared crate tests-utils
1 parent dc06222 commit 6c79bbf

File tree

13 files changed

+170
-155
lines changed

13 files changed

+170
-155
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

decompression-plz/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ zstd = "0.13.3"
2121
bytes = { workspace = true }
2222
thiserror = { workspace = true }
2323
tracing = { workspace = true }
24+
25+
26+
[dev-dependencies]
27+
tests-utils = { path = "tests-utils" }

decompression-plz/src/decompression/dstruct.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,10 @@ impl<'a> DecompressionStruct<'a> {
196196
#[cfg(test)]
197197
mod tests {
198198
use bytes::BufMut;
199+
use tests_utils::*;
199200

200201
use crate::decompression::multi::error::MultiDecompressErrorReason;
201-
use crate::{
202-
decompression::single::error::DecompressError,
203-
tests::{
204-
INPUT, all_encoding_info_multi_header,
205-
all_encoding_info_single_header, compress_brotli,
206-
compress_deflate, compress_gzip, compress_zstd,
207-
},
208-
};
202+
use crate::decompression::single::error::DecompressError;
209203

210204
use super::*;
211205

decompression-plz/src/decompression/magic_bytes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ pub fn is_compressed(input: &[u8], encoding: &ContentEncoding) -> bool {
4444
#[cfg(test)]
4545
mod tests {
4646
use header_plz::body_headers::content_encoding::ContentEncoding;
47+
use tests_utils::*;
4748

48-
use crate::{decompression::magic_bytes::is_compressed, tests::*};
49+
use crate::decompression::magic_bytes::is_compressed;
4950

5051
#[test]
5152
fn test_magic_bytes_deflate() {

decompression-plz/src/decompression/multi/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ where
5757
mod tests {
5858

5959
use super::*;
60-
use crate::tests::*;
6160
use bytes::BufMut;
6261
use header_plz::body_headers::content_encoding::ContentEncoding;
62+
use tests_utils::*;
6363

6464
#[test]
6565
fn test_decompress_multi_single_header() {

decompression-plz/src/decompression/single/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ pub mod tests {
9292
use bytes::{BufMut, BytesMut};
9393

9494
use header_plz::body_headers::content_encoding::ContentEncoding;
95+
use tests_utils::*;
9596

96-
use crate::{
97-
decompression::single::{decompress_single, error::DecompressError},
98-
tests::*,
97+
use crate::decompression::single::{
98+
decompress_single, error::DecompressError,
9999
};
100100

101101
fn test_decompress(

decompression-plz/src/decompression/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ mod tests {
222222
use bytes::BytesMut;
223223
use header_plz::body_headers::content_encoding::ContentEncoding;
224224

225-
use crate::tests::*;
225+
use tests_utils::*;
226226

227227
// ----- Main
228228
fn assert_main_only_finish_flow(

decompression-plz/src/lib.rs

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -29,100 +29,3 @@ where
2929

3030
Ok(())
3131
}
32-
33-
// helper function for tests
34-
#[cfg(test)]
35-
pub mod tests {
36-
use crate::DecompressTrait;
37-
use body_plz::variants::Body;
38-
use bytes::BytesMut;
39-
use flate2::Compression;
40-
use header_plz::{
41-
InfoLine,
42-
body_headers::{
43-
content_encoding::ContentEncoding, encoding_info::EncodingInfo,
44-
},
45-
};
46-
use std::io::Write;
47-
pub const INPUT: &[u8] = b"hello world";
48-
49-
pub fn all_compressed_data() -> Vec<u8> {
50-
let brotli_compressed = compress_brotli(INPUT);
51-
let deflate_compressed = compress_deflate(&brotli_compressed);
52-
let gzip_compressed = compress_gzip(&deflate_compressed);
53-
compress_zstd(&gzip_compressed)
54-
}
55-
56-
pub fn single_compression(encoding: &ContentEncoding) -> Vec<u8> {
57-
match encoding {
58-
ContentEncoding::Brotli => compress_brotli(INPUT),
59-
ContentEncoding::Deflate => compress_deflate(INPUT),
60-
ContentEncoding::Gzip => compress_gzip(INPUT),
61-
ContentEncoding::Zstd | ContentEncoding::Compress => {
62-
compress_zstd(INPUT)
63-
}
64-
ContentEncoding::Identity => INPUT.to_vec(),
65-
_ => panic!(),
66-
}
67-
}
68-
69-
pub fn all_encoding_info_multi_header() -> Vec<EncodingInfo> {
70-
vec![
71-
EncodingInfo::new(0, vec![ContentEncoding::Brotli]),
72-
EncodingInfo::new(1, vec![ContentEncoding::Deflate]),
73-
EncodingInfo::new(2, vec![ContentEncoding::Identity]),
74-
EncodingInfo::new(3, vec![ContentEncoding::Gzip]),
75-
EncodingInfo::new(4, vec![ContentEncoding::Zstd]),
76-
]
77-
}
78-
79-
pub fn all_encoding_info_single_header() -> Vec<EncodingInfo> {
80-
vec![EncodingInfo::new(
81-
0,
82-
vec![
83-
ContentEncoding::Brotli,
84-
ContentEncoding::Deflate,
85-
ContentEncoding::Identity,
86-
ContentEncoding::Gzip,
87-
ContentEncoding::Zstd,
88-
],
89-
)]
90-
}
91-
92-
pub fn compress_brotli(data: &[u8]) -> Vec<u8> {
93-
let mut compressed = Vec::new();
94-
{
95-
let mut writer =
96-
brotli::CompressorWriter::new(&mut compressed, 4096, 0, 22);
97-
writer.write_all(data).unwrap();
98-
writer.flush().unwrap();
99-
}
100-
compressed
101-
}
102-
103-
pub fn compress_deflate(data: &[u8]) -> Vec<u8> {
104-
let mut compressed = Vec::new();
105-
let mut encoder = flate2::write::ZlibEncoder::new(
106-
&mut compressed,
107-
Compression::fast(),
108-
);
109-
encoder.write_all(data).unwrap();
110-
encoder.finish().unwrap();
111-
compressed
112-
}
113-
114-
pub fn compress_gzip(data: &[u8]) -> Vec<u8> {
115-
let mut compressed = Vec::new();
116-
let mut encoder = flate2::write::GzEncoder::new(
117-
&mut compressed,
118-
Compression::fast(),
119-
);
120-
encoder.write_all(data).unwrap();
121-
encoder.finish().unwrap();
122-
compressed
123-
}
124-
125-
pub fn compress_zstd(data: &[u8]) -> Vec<u8> {
126-
zstd::encode_all(data, 1).unwrap()
127-
}
128-
}

decompression-plz/src/state.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -326,49 +326,5 @@ mod tests {
326326
assert_decode_state_te_only_single(ContentEncoding::Zstd);
327327
}
328328
329-
fn run_case(case: &Case, content_encoding: ContentEncoding) {
330-
let body = single_compression(&content_encoding);
331-
let headers = format!(
332-
"Host: example.com\r\n\
333-
Content-Type: text/html; charset=utf-8\r\n\
334-
{}: {}\r\n\
335-
Content-Length: {}\r\n\r\n",
336-
case.header_name,
337-
content_encoding.as_ref(),
338-
body.len()
339-
);
340-
341-
let mut tm = TestMessage::build(
342-
headers.as_bytes().into(),
343-
Body::Raw(body.as_slice().into()),
344-
None,
345-
);
346-
let mut buf = BytesMut::new();
347-
let mut state = DecodeState::init(&mut tm, &mut buf);
348-
state = state.try_next().unwrap();
349-
assert!((case.expected_state)(&state));
350-
state = state.try_next().unwrap();
351-
assert!(matches!(state, DecodeState::UpdateContentLength(_)));
352-
state = state.try_next().unwrap();
353-
assert!(matches!(state, DecodeState::End));
354-
355-
let result = tm.into_bytes();
356-
let verify = "Host: example.com\r\n\
357-
Content-Type: text/html; charset=utf-8\r\n\
358-
Content-Length: 11\r\n\r\n\
359-
hello world";
360-
assert_eq!(result, verify);
361-
}
362-
363-
#[test]
364-
fn assert_decode_states_single() {
365-
let case = Case {
366-
header_name: "Transfer-Encoding",
367-
expected_state: |s| {
368-
matches!(s, DecodeState::TransferEncoding(_, _))
369-
},
370-
};
371-
run_case(&case, ContentEncoding::Brotli);
372-
}
373329
}
374330
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "tests-utils"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
header-plz = "0.0.25"
8+
body-plz = "0.0.30"
9+
10+
# Compression
11+
brotli = "8.0.0"
12+
flate2 = "1.1.0"
13+
zstd = "0.13.3"

0 commit comments

Comments
 (0)