Skip to content

Commit a3ba0e6

Browse files
committed
test(decompression-plz): move TestMessage to tests-utils
1 parent 73df3f5 commit a3ba0e6

File tree

6 files changed

+85
-60
lines changed

6 files changed

+85
-60
lines changed

Cargo.lock

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

decompression-plz/src/chunked.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,15 @@ pub fn partial_chunked_to_raw(vec_body: Vec<ChunkType>) -> Option<BytesMut> {
5555

5656
Some(body)
5757
}
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
extern crate tests_utils;
63+
use tests_utils::TestMessage;
64+
65+
#[test]
66+
fn test_chunked_to_raw() {
67+
let a: Option<TestMessage>;
68+
}
69+
}

decompression-plz/tests-utils/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ body-plz = "0.0.30"
1111
brotli = "8.0.0"
1212
flate2 = "1.1.0"
1313
zstd = "0.13.3"
14+
bytes = { workspace = true }
15+
16+
decompression-plz = { path = ".." }

decompression-plz/tests-utils/src/lib.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,78 @@
1+
pub use body_plz::variants::Body;
2+
pub use bytes::BytesMut;
3+
use decompression_plz::DecompressTrait;
14
use std::io::Write;
25

36
use flate2::Compression;
4-
use header_plz::body_headers::{
5-
content_encoding::ContentEncoding, encoding_info::EncodingInfo,
7+
use header_plz::{
8+
HeaderMap,
9+
body_headers::{
10+
BodyHeader, content_encoding::ContentEncoding,
11+
encoding_info::EncodingInfo,
12+
},
613
};
714

815
pub const INPUT: &[u8] = b"hello world";
916

1017
pub const ALL_COMPRESSIONS: &str = "br, deflate, gzip, zstd";
1118

19+
#[derive(Debug, PartialEq)]
20+
pub struct TestMessage {
21+
header_map: HeaderMap,
22+
body_header: Option<BodyHeader>,
23+
body: Option<Body>,
24+
extra_body: Option<BytesMut>,
25+
}
26+
27+
impl DecompressTrait for TestMessage {
28+
fn get_body(&mut self) -> Body {
29+
self.body.take().unwrap()
30+
}
31+
32+
fn get_extra_body(&mut self) -> Option<BytesMut> {
33+
self.extra_body.take()
34+
}
35+
36+
fn set_body(&mut self, body: Body) {
37+
self.body = Some(body);
38+
}
39+
40+
fn body_headers_as_mut(&mut self) -> &mut Option<BodyHeader> {
41+
&mut self.body_header
42+
}
43+
44+
fn header_map(&self) -> &HeaderMap {
45+
&self.header_map
46+
}
47+
48+
fn header_map_as_mut(&mut self) -> &mut HeaderMap {
49+
&mut self.header_map
50+
}
51+
}
52+
53+
impl TestMessage {
54+
pub fn build(
55+
headers: BytesMut,
56+
body: Body,
57+
extra: Option<BytesMut>,
58+
) -> Self {
59+
let header_map = HeaderMap::from(headers);
60+
let body_header = BodyHeader::from(&header_map);
61+
Self {
62+
header_map,
63+
body_header: Some(body_header),
64+
body: Some(body),
65+
extra_body: extra,
66+
}
67+
}
68+
69+
pub fn into_bytes(self) -> BytesMut {
70+
let mut bytes = self.header_map.into_bytes();
71+
bytes.unsplit(self.body.unwrap().into_bytes().unwrap());
72+
bytes
73+
}
74+
}
75+
1276
pub fn all_compressed_data() -> Vec<u8> {
1377
let brotli_compressed = compress_brotli(INPUT);
1478
let deflate_compressed = compress_deflate(&brotli_compressed);

decompression-plz/tests/test_cases/no_encodings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::*;
22
use body_plz::variants::Body;
33
use bytes::BytesMut;
44
use decompression_plz::state::DecodeState;
5+
use tests_utils::INPUT;
56

67
#[test]
78
fn test_decode_init_no_enc() {

decompression-plz/tests/tests.rs

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,10 @@ pub use bytes::BytesMut;
44
use decompression_plz::DecompressTrait;
55
use decompression_plz::state::DecodeState;
66
use header_plz::{HeaderMap, body_headers::BodyHeader};
7+
use tests_utils::TestMessage;
78

8-
pub const INPUT: &[u8] = b"hello world";
99
pub mod test_cases;
1010

11-
#[derive(Debug, PartialEq)]
12-
pub struct TestMessage {
13-
header_map: HeaderMap,
14-
body_header: Option<BodyHeader>,
15-
body: Option<Body>,
16-
extra_body: Option<BytesMut>,
17-
}
18-
19-
impl DecompressTrait for TestMessage {
20-
fn get_body(&mut self) -> Body {
21-
self.body.take().unwrap()
22-
}
23-
24-
fn get_extra_body(&mut self) -> Option<BytesMut> {
25-
self.extra_body.take()
26-
}
27-
28-
fn set_body(&mut self, body: Body) {
29-
self.body = Some(body);
30-
}
31-
32-
fn body_headers_as_mut(&mut self) -> &mut Option<BodyHeader> {
33-
&mut self.body_header
34-
}
35-
36-
fn header_map(&self) -> &HeaderMap {
37-
&self.header_map
38-
}
39-
40-
fn header_map_as_mut(&mut self) -> &mut HeaderMap {
41-
&mut self.header_map
42-
}
43-
}
44-
45-
impl TestMessage {
46-
pub fn build(
47-
headers: BytesMut,
48-
body: Body,
49-
extra: Option<BytesMut>,
50-
) -> Self {
51-
let header_map = HeaderMap::from(headers);
52-
let body_header = BodyHeader::from(&header_map);
53-
Self {
54-
header_map,
55-
body_header: Some(body_header),
56-
body: Some(body),
57-
extra_body: extra,
58-
}
59-
}
60-
61-
pub fn into_bytes(self) -> BytesMut {
62-
let mut bytes = self.header_map.into_bytes();
63-
bytes.unsplit(self.body.unwrap().into_bytes().unwrap());
64-
bytes
65-
}
66-
}
67-
6811
enum EncodingKind {
6912
Te,
7013
Ce,

0 commit comments

Comments
 (0)