Skip to content

Commit dc06222

Browse files
committed
test(decompression-plz): organize tests
1 parent d91a0f7 commit dc06222

File tree

7 files changed

+283
-75
lines changed

7 files changed

+283
-75
lines changed

build_rs_cov.profraw

-1.34 MB
Binary file not shown.

decompression-plz/src/decompression/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::decompression::{
22
dstruct::DecompressionStruct, multi::error::MultiDecompressError,
33
};
4-
use bytes::{buf::Writer, BufMut, BytesMut};
4+
use bytes::{BufMut, BytesMut, buf::Writer};
55
use header_plz::body_headers::encoding_info::EncodingInfo;
66
use tracing::error;
77

decompression-plz/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use crate::state::DecodeState;
66
pub mod chunked;
77
pub mod content_length;
88
pub use decompression::multi::error::MultiDecompressErrorReason;
9-
mod decode_struct;
9+
pub mod decode_struct;
1010
mod decompress_trait;
1111
mod decompression;
1212
pub use decompress_trait::DecompressTrait;
13-
mod state;
13+
pub mod state;
1414

1515
pub fn decompress<T>(
1616
message: &mut T,
@@ -58,7 +58,10 @@ pub mod tests {
5858
ContentEncoding::Brotli => compress_brotli(INPUT),
5959
ContentEncoding::Deflate => compress_deflate(INPUT),
6060
ContentEncoding::Gzip => compress_gzip(INPUT),
61-
ContentEncoding::Zstd => compress_zstd(INPUT),
61+
ContentEncoding::Zstd | ContentEncoding::Compress => {
62+
compress_zstd(INPUT)
63+
}
64+
ContentEncoding::Identity => INPUT.to_vec(),
6265
_ => panic!(),
6366
}
6467
}

decompression-plz/src/state.rs

Lines changed: 150 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -178,88 +178,111 @@ where
178178
}
179179
}
180180

181+
/*
181182
#[cfg(test)]
182183
mod tests {
183184
184-
use body_plz::variants::Body;
185-
use bytes::BytesMut;
186-
use header_plz::{
187-
HeaderMap, abnf::HEADER_DELIMITER, body_headers::BodyHeader,
188-
message_head::MessageHead,
185+
use crate::{
186+
DecompressTrait,
187+
state::DecodeState,
188+
tests::{INPUT, single_compression},
189189
};
190190
191-
use crate::{DecompressTrait, state::DecodeState, tests::INPUT};
192191
193-
#[derive(Debug, PartialEq)]
194-
struct TestMessage {
195-
header_map: HeaderMap,
196-
body_header: Option<BodyHeader>,
197-
body: Option<Body>,
198-
extra_body: Option<BytesMut>,
199-
}
192+
// CE only
193+
fn assert_decode_state_ce_only_single(content_encoding: ContentEncoding) {
194+
let body = single_compression(&content_encoding);
195+
let headers = format!(
196+
"Host: example.com\r\n\
197+
Content-Type: text/html; charset=utf-8\r\n\
198+
Content-Encoding: {}\r\n\
199+
Content-Length: {}\r\n\r\n",
200+
content_encoding.as_ref(),
201+
body.len()
202+
);
203+
let mut tm = TestMessage::build(
204+
headers.as_bytes().into(),
205+
Body::Raw(body.as_slice().into()),
206+
None,
207+
);
200208
201-
impl DecompressTrait for TestMessage {
202-
fn get_body(&mut self) -> Body {
203-
self.body.take().unwrap()
204-
}
209+
let mut buf = BytesMut::new();
210+
let mut state = DecodeState::init(&mut tm, &mut buf);
211+
state = state.try_next().unwrap();
212+
assert!(matches!(state, DecodeState::ContentEncoding(_, _)));
205213
206-
fn get_extra_body(&mut self) -> Option<BytesMut> {
207-
self.extra_body.take()
208-
}
214+
state = state.try_next().unwrap();
215+
assert!(matches!(state, DecodeState::UpdateContentLength(_)));
209216
210-
fn set_body(&mut self, body: Body) {
211-
self.body = Some(body);
212-
}
217+
state = state.try_next().unwrap();
218+
assert!(matches!(state, DecodeState::End));
213219
214-
fn body_headers_as_mut(&mut self) -> &mut Option<BodyHeader> {
215-
&mut self.body_header
216-
}
220+
let result = tm.into_bytes();
221+
let verify = "Host: example.com\r\n\
222+
Content-Type: text/html; charset=utf-8\r\n\
223+
Content-Length: 11\r\n\r\n\
224+
hello world";
225+
assert_eq!(result, verify);
226+
}
217227
218-
fn header_map(&self) -> &HeaderMap {
219-
&self.header_map
220-
}
228+
#[test]
229+
fn test_decode_state_ce_only_brotli() {
230+
assert_decode_state_ce_only_single(ContentEncoding::Brotli);
231+
}
221232
222-
fn header_map_as_mut(&mut self) -> &mut HeaderMap {
223-
&mut self.header_map
224-
}
233+
#[test]
234+
fn test_decode_state_ce_only_compress() {
235+
assert_decode_state_ce_only_single(ContentEncoding::Compress);
225236
}
226237
227-
impl TestMessage {
228-
fn build(
229-
headers: BytesMut,
230-
body: Body,
231-
extra: Option<BytesMut>,
232-
) -> Self {
233-
let header_map = HeaderMap::from(headers);
234-
let body_header = BodyHeader::from(&header_map);
235-
Self {
236-
header_map,
237-
body_header: Some(body_header),
238-
body: Some(body),
239-
extra_body: extra,
240-
}
241-
}
238+
#[test]
239+
fn test_decode_state_ce_only_deflate() {
240+
assert_decode_state_ce_only_single(ContentEncoding::Deflate);
241+
}
242242
243-
fn into_bytes(self) -> BytesMut {
244-
let mut bytes = self.header_map.into_bytes();
245-
bytes.unsplit(self.body.unwrap().into_bytes().unwrap());
246-
bytes
247-
}
243+
#[test]
244+
fn test_decode_state_ce_only_gzip() {
245+
assert_decode_state_ce_only_single(ContentEncoding::Gzip);
248246
}
249247
250248
#[test]
251-
fn test_decode_init_no_enc() {
252-
let headers = "Host: example.com\r\n\
253-
Content-Type: text/html; charset=utf-8\r\n\
254-
Content-Length: 11\r\n\r\n";
255-
let mut tm =
256-
TestMessage::build(headers.into(), Body::Raw(INPUT.into()), None);
249+
fn test_decode_state_ce_only_identity() {
250+
assert_decode_state_ce_only_single(ContentEncoding::Identity);
251+
}
252+
253+
#[test]
254+
fn test_decode_state_ce_only_zstd() {
255+
assert_decode_state_ce_only_single(ContentEncoding::Zstd);
256+
}
257+
258+
// TE only
259+
fn assert_decode_state_te_only_single(content_encoding: ContentEncoding) {
260+
let body = single_compression(&content_encoding);
261+
let headers = format!(
262+
"Host: example.com\r\n\
263+
Content-Type: text/html; charset=utf-8\r\n\
264+
Transfer-Encoding: {}\r\n\
265+
Content-Length: {}\r\n\r\n",
266+
content_encoding.as_ref(),
267+
body.len()
268+
);
269+
let mut tm = TestMessage::build(
270+
headers.as_bytes().into(),
271+
Body::Raw(body.as_slice().into()),
272+
None,
273+
);
274+
257275
let mut buf = BytesMut::new();
258276
let mut state = DecodeState::init(&mut tm, &mut buf);
259-
dbg!(&state);
260277
state = state.try_next().unwrap();
261-
dbg!(&state);
278+
assert!(matches!(state, DecodeState::TransferEncoding(_, _)));
279+
280+
state = state.try_next().unwrap();
281+
assert!(matches!(state, DecodeState::UpdateContentLength(_)));
282+
283+
state = state.try_next().unwrap();
262284
assert!(matches!(state, DecodeState::End));
285+
263286
let result = tm.into_bytes();
264287
let verify = "Host: example.com\r\n\
265288
Content-Type: text/html; charset=utf-8\r\n\
@@ -269,27 +292,83 @@ mod tests {
269292
}
270293
271294
#[test]
272-
fn test_decode_init_no_enc_extra_body() {
273-
let headers = "Host: example.com\r\n\
274-
Content-Type: text/html; charset=utf-8\r\n\
275-
Content-Length: 11\r\n\r\n";
276-
let mut tm = TestMessage::build(
277-
headers.into(),
278-
Body::Raw(INPUT.into()),
279-
Some(INPUT.into()),
295+
fn test_decode_state_te_only_brotli() {
296+
assert_decode_state_te_only_single(ContentEncoding::Brotli);
297+
}
298+
299+
#[test]
300+
fn test_decode_state_te_only_chunked() {
301+
()
302+
}
303+
304+
#[test]
305+
fn test_decode_state_te_only_compress() {
306+
assert_decode_state_te_only_single(ContentEncoding::Compress);
307+
}
308+
309+
#[test]
310+
fn test_decode_state_te_only_deflate() {
311+
assert_decode_state_te_only_single(ContentEncoding::Deflate);
312+
}
313+
314+
#[test]
315+
fn test_decode_state_te_only_gzip() {
316+
assert_decode_state_te_only_single(ContentEncoding::Gzip);
317+
}
318+
319+
#[test]
320+
fn test_decode_state_te_only_identity() {
321+
assert_decode_state_te_only_single(ContentEncoding::Identity);
322+
}
323+
324+
#[test]
325+
fn test_decode_state_te_only_zstd() {
326+
assert_decode_state_te_only_single(ContentEncoding::Zstd);
327+
}
328+
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()
280339
);
281340
341+
let mut tm = TestMessage::build(
342+
headers.as_bytes().into(),
343+
Body::Raw(body.as_slice().into()),
344+
None,
345+
);
282346
let mut buf = BytesMut::new();
283347
let mut state = DecodeState::init(&mut tm, &mut buf);
284348
state = state.try_next().unwrap();
349+
assert!((case.expected_state)(&state));
350+
state = state.try_next().unwrap();
285351
assert!(matches!(state, DecodeState::UpdateContentLength(_)));
286352
state = state.try_next().unwrap();
287353
assert!(matches!(state, DecodeState::End));
354+
288355
let result = tm.into_bytes();
289356
let verify = "Host: example.com\r\n\
290-
Content-Type: text/html; charset=utf-8\r\n\
291-
Content-Length: 22\r\n\r\n\
292-
hello worldhello world";
357+
Content-Type: text/html; charset=utf-8\r\n\
358+
Content-Length: 11\r\n\r\n\
359+
hello world";
293360
assert_eq!(result, verify);
294361
}
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+
}
295373
}
374+
*/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
use super::*;
2+
pub mod no_encodings;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use super::*;
2+
use body_plz::variants::Body;
3+
use bytes::BytesMut;
4+
use decompression_plz::state::DecodeState;
5+
6+
//#[test]
7+
fn test_decode_init_no_enc() {
8+
let headers = "Host: example.com\r\n\
9+
Content-Type: text/html; charset=utf-8\r\n\
10+
Content-Length: 11\r\n\r\n";
11+
let mut tm =
12+
TestMessage::build(headers.into(), Body::Raw(INPUT.into()), None);
13+
let mut buf = BytesMut::new();
14+
let mut state = DecodeState::init(&mut tm, &mut buf);
15+
state = state.try_next().unwrap();
16+
assert!(matches!(state, DecodeState::End));
17+
let result = tm.into_bytes();
18+
let verify = "Host: example.com\r\n\
19+
Content-Type: text/html; charset=utf-8\r\n\
20+
Content-Length: 11\r\n\r\n\
21+
hello world";
22+
assert_eq!(result, verify);
23+
}
24+
25+
#[test]
26+
fn test_decode_init_no_enc_extra_body() {
27+
let headers = "Host: example.com\r\n\
28+
Content-Type: text/html; charset=utf-8\r\n\
29+
Content-Length: 11\r\n\r\n";
30+
let mut tm = TestMessage::build(
31+
headers.into(),
32+
Body::Raw(INPUT.into()),
33+
Some(INPUT.into()),
34+
);
35+
36+
let mut buf = BytesMut::new();
37+
let mut state = DecodeState::init(&mut tm, &mut buf);
38+
state = state.try_next().unwrap();
39+
assert!(matches!(state, DecodeState::UpdateContentLength(_)));
40+
state = state.try_next().unwrap();
41+
assert!(matches!(state, DecodeState::End));
42+
let result = tm.into_bytes();
43+
let verify = "Host: example.com\r\n\
44+
Content-Type: text/html; charset=utf-8\r\n\
45+
Content-Length: 22\r\n\r\n\
46+
hello worldhello world";
47+
assert_eq!(result, verify);
48+
}

0 commit comments

Comments
 (0)