Skip to content

Commit 73df3f5

Browse files
committed
test(decompression-plz): both ce and te and chunked transfer encoding
1 parent 80d8cd8 commit 73df3f5

File tree

8 files changed

+116
-19
lines changed

8 files changed

+116
-19
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use super::*;
2+
use tests_utils::all_compressed_data;
3+
4+
#[test]
5+
fn test_both_te_ce() {
6+
let body: Vec<u8> = all_compressed_data();
7+
let headers = format!(
8+
"Host: example.com\r\n\
9+
Content-Type: text/html; charset=utf-8\r\n\
10+
Transfer-Encoding: gzip, zstd\r\n\
11+
Content-Encoding: br, deflate\r\n\
12+
Content-Length: {}\r\n\r\n",
13+
body.len()
14+
);
15+
16+
let mut tm = TestMessage::build(
17+
headers.as_bytes().into(),
18+
Body::Raw(body.as_slice().into()),
19+
None,
20+
);
21+
let mut buf = BytesMut::new();
22+
let mut state = DecodeState::init(&mut tm, &mut buf);
23+
state = state.try_next().unwrap();
24+
assert!(matches!(state, DecodeState::TransferEncoding(..)));
25+
26+
state = state.try_next().unwrap();
27+
assert!(matches!(state, DecodeState::ContentEncoding(..)));
28+
state = state.try_next().unwrap();
29+
assert!(matches!(state, DecodeState::UpdateContentLength(_)));
30+
state = state.try_next().unwrap();
31+
assert!(matches!(state, DecodeState::End));
32+
33+
let result = tm.into_bytes();
34+
let verify = "Host: example.com\r\n\
35+
Content-Type: text/html; charset=utf-8\r\n\
36+
Content-Length: 11\r\n\r\n\
37+
hello world";
38+
assert_eq!(result, verify);
39+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use super::*;
2+
use body_plz::{
3+
reader::chunked_reader::ChunkReaderState, variants::chunked::ChunkType,
4+
};
5+
6+
fn build_chunked_body_large() -> Body {
7+
/*
8+
7\r\n\
9+
Mozilla\r\n\
10+
9\r\n\
11+
Developer\r\n\
12+
7\r\n\
13+
Network\r\n\
14+
0\r\n\
15+
Header: Val\r\n\
16+
*/
17+
18+
let chunk_vec = vec![
19+
ChunkType::Size("7".into()),
20+
ChunkType::Chunk("Mozilla\r\n".into()),
21+
ChunkType::Size("9".into()),
22+
ChunkType::Chunk("Developer\r\n".into()),
23+
ChunkType::Size("7".into()),
24+
ChunkType::Chunk("Network\r\n".into()),
25+
ChunkType::LastChunk("0\r\n".into()),
26+
ChunkType::EndCRLF("\r\n".into()),
27+
];
28+
Body::Chunked(chunk_vec)
29+
}
30+
31+
#[test]
32+
fn test_chunked_body_large() {
33+
let headers = "Host: example.com\r\n\
34+
Content-Type: text/html; charset=utf-8\r\n\
35+
Transfer-Encoding: chunked\r\n\r\n";
36+
let mut tm =
37+
TestMessage::build(headers.into(), build_chunked_body_large(), None);
38+
39+
let mut buf = BytesMut::new();
40+
let mut state = DecodeState::init(&mut tm, &mut buf);
41+
state = state.try_next().unwrap();
42+
assert!(matches!(state, DecodeState::TransferEncoding(..)));
43+
44+
state = state.try_next().unwrap();
45+
assert!(matches!(state, DecodeState::UpdateContentLength(_)));
46+
47+
state = state.try_next().unwrap();
48+
assert!(matches!(state, DecodeState::End));
49+
50+
let verify = "Host: example.com\r\n\
51+
Content-Type: text/html; charset=utf-8\r\n\
52+
Content-Length: 23\r\n\r\n\
53+
MozillaDeveloperNetwork";
54+
let result = tm.into_bytes();
55+
assert_eq!(result, verify);
56+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use super::*;
2+
pub mod both_te_ce;
3+
pub mod chunked;
24
pub mod multi_compression;
35
pub mod no_encodings;
46
pub mod single_compression;

decompression-plz/tests/test_cases/multi_compression/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::*;
55
mod multi_header;
66
mod single_header;
77

8-
fn run_case_multi_compression(
8+
fn assert_case_multi_compression(
99
f: fn(&DecodeState<TestMessage>) -> bool,
1010
mut tm: TestMessage,
1111
verify: &str,

decompression-plz/tests/test_cases/multi_compression/multi_header.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn assert_decode_state_ce_all_multi_header() {
4949
let f = move |s: &DecodeState<TestMessage>| {
5050
matches!(s, DecodeState::ContentEncoding(_, _))
5151
};
52-
run_case_multi_compression(f, tm, VERIFY);
52+
assert_case_multi_compression(f, tm, VERIFY);
5353
}
5454

5555
#[test]
@@ -58,5 +58,5 @@ fn assert_decode_state_te_all_multi_header() {
5858
let f = move |s: &DecodeState<TestMessage>| {
5959
matches!(s, DecodeState::TransferEncoding(_, _))
6060
};
61-
run_case_multi_compression(f, tm, VERIFY);
61+
assert_case_multi_compression(f, tm, VERIFY);
6262
}

decompression-plz/tests/test_cases/multi_compression/single_header.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn assert_decode_state_ce_all_single_header() {
3636
let f = move |s: &DecodeState<TestMessage>| {
3737
matches!(s, DecodeState::ContentEncoding(_, _))
3838
};
39-
run_case_multi_compression(f, tm, VERIFY);
39+
assert_case_multi_compression(f, tm, VERIFY);
4040
}
4141

4242
#[test]
@@ -45,5 +45,5 @@ fn assert_decode_state_te_all_single_header() {
4545
let f = move |s: &DecodeState<TestMessage>| {
4646
matches!(s, DecodeState::TransferEncoding(_, _))
4747
};
48-
run_case_multi_compression(f, tm, VERIFY);
48+
assert_case_multi_compression(f, tm, VERIFY);
4949
}

decompression-plz/tests/test_cases/no_encodings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use body_plz::variants::Body;
33
use bytes::BytesMut;
44
use decompression_plz::state::DecodeState;
55

6-
//#[test]
6+
#[test]
77
fn test_decode_init_no_enc() {
88
let headers = "Host: example.com\r\n\
99
Content-Type: text/html; charset=utf-8\r\n\

decompression-plz/tests/test_cases/single_compression.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use tests_utils::single_compression;
33

44
use super::*;
55

6-
fn run_case_single_compression(
6+
fn assert_case_single_compression(
77
case: &Case,
88
content_encoding: ContentEncoding,
99
) {
@@ -48,7 +48,7 @@ fn assert_decode_state_single_te_brotli() {
4848
header_name: "Transfer-Encoding",
4949
expected_state: |s| matches!(s, DecodeState::TransferEncoding(_, _)),
5050
};
51-
run_case_single_compression(&case, ContentEncoding::Brotli);
51+
assert_case_single_compression(&case, ContentEncoding::Brotli);
5252
}
5353

5454
#[test]
@@ -57,7 +57,7 @@ fn assert_decode_state_single_te_compress() {
5757
header_name: "Transfer-Encoding",
5858
expected_state: |s| matches!(s, DecodeState::TransferEncoding(_, _)),
5959
};
60-
run_case_single_compression(&case, ContentEncoding::Compress);
60+
assert_case_single_compression(&case, ContentEncoding::Compress);
6161
}
6262

6363
#[test]
@@ -66,7 +66,7 @@ fn assert_decode_state_single_te_deflate() {
6666
header_name: "Transfer-Encoding",
6767
expected_state: |s| matches!(s, DecodeState::TransferEncoding(_, _)),
6868
};
69-
run_case_single_compression(&case, ContentEncoding::Deflate);
69+
assert_case_single_compression(&case, ContentEncoding::Deflate);
7070
}
7171

7272
#[test]
@@ -75,7 +75,7 @@ fn assert_decode_state_single_te_gzip() {
7575
header_name: "Transfer-Encoding",
7676
expected_state: |s| matches!(s, DecodeState::TransferEncoding(_, _)),
7777
};
78-
run_case_single_compression(&case, ContentEncoding::Gzip);
78+
assert_case_single_compression(&case, ContentEncoding::Gzip);
7979
}
8080

8181
#[test]
@@ -84,7 +84,7 @@ fn assert_decode_state_single_te_identity() {
8484
header_name: "Transfer-Encoding",
8585
expected_state: |s| matches!(s, DecodeState::TransferEncoding(_, _)),
8686
};
87-
run_case_single_compression(&case, ContentEncoding::Identity);
87+
assert_case_single_compression(&case, ContentEncoding::Identity);
8888
}
8989

9090
#[test]
@@ -93,7 +93,7 @@ fn assert_decode_state_single_te_zstd() {
9393
header_name: "Transfer-Encoding",
9494
expected_state: |s| matches!(s, DecodeState::TransferEncoding(_, _)),
9595
};
96-
run_case_single_compression(&case, ContentEncoding::Zstd);
96+
assert_case_single_compression(&case, ContentEncoding::Zstd);
9797
}
9898

9999
// CE only
@@ -103,7 +103,7 @@ fn assert_decode_state_single_ce_brotli() {
103103
header_name: "Content-Encoding",
104104
expected_state: |s| matches!(s, DecodeState::ContentEncoding(_, _)),
105105
};
106-
run_case_single_compression(&case, ContentEncoding::Brotli);
106+
assert_case_single_compression(&case, ContentEncoding::Brotli);
107107
}
108108

109109
#[test]
@@ -112,7 +112,7 @@ fn assert_decode_state_single_ce_compress() {
112112
header_name: "Content-Encoding",
113113
expected_state: |s| matches!(s, DecodeState::ContentEncoding(_, _)),
114114
};
115-
run_case_single_compression(&case, ContentEncoding::Compress);
115+
assert_case_single_compression(&case, ContentEncoding::Compress);
116116
}
117117

118118
#[test]
@@ -121,7 +121,7 @@ fn assert_decode_state_single_ce_deflate() {
121121
header_name: "Content-Encoding",
122122
expected_state: |s| matches!(s, DecodeState::ContentEncoding(_, _)),
123123
};
124-
run_case_single_compression(&case, ContentEncoding::Deflate);
124+
assert_case_single_compression(&case, ContentEncoding::Deflate);
125125
}
126126

127127
#[test]
@@ -130,7 +130,7 @@ fn assert_decode_state_single_ce_gzip() {
130130
header_name: "Content-Encoding",
131131
expected_state: |s| matches!(s, DecodeState::ContentEncoding(_, _)),
132132
};
133-
run_case_single_compression(&case, ContentEncoding::Gzip);
133+
assert_case_single_compression(&case, ContentEncoding::Gzip);
134134
}
135135

136136
#[test]
@@ -139,7 +139,7 @@ fn assert_decode_state_single_ce_identity() {
139139
header_name: "Content-Encoding",
140140
expected_state: |s| matches!(s, DecodeState::ContentEncoding(_, _)),
141141
};
142-
run_case_single_compression(&case, ContentEncoding::Identity);
142+
assert_case_single_compression(&case, ContentEncoding::Identity);
143143
}
144144

145145
#[test]
@@ -148,5 +148,5 @@ fn assert_decode_state_single_ce_zstd() {
148148
header_name: "Content-Encoding",
149149
expected_state: |s| matches!(s, DecodeState::ContentEncoding(_, _)),
150150
};
151-
run_case_single_compression(&case, ContentEncoding::Zstd);
151+
assert_case_single_compression(&case, ContentEncoding::Zstd);
152152
}

0 commit comments

Comments
 (0)