Skip to content

Commit 8f6ecf8

Browse files
committed
test(decompression-plz): DecodeState
1 parent 3b22688 commit 8f6ecf8

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

build_rs_cov.profraw

1.34 MB
Binary file not shown.

decompression-plz/src/decode_struct.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use header_plz::body_headers::encoding_info::EncodingInfo;
55
use header_plz::body_headers::transfer_types::TransferType;
66

77
use crate::chunked::chunked_to_raw;
8-
use crate::content_length::add_body_and_update_cl;
8+
use crate::content_length::{add_body_and_update_cl, update_content_length};
99
use crate::decompress_trait::DecompressTrait;
1010

1111
#[cfg_attr(test, derive(PartialEq))]
@@ -20,7 +20,7 @@ pub struct DecodeStruct<'a, T> {
2020

2121
impl<'a, T> DecodeStruct<'a, T>
2222
where
23-
T: DecompressTrait,
23+
T: DecompressTrait + std::fmt::Debug,
2424
{
2525
pub fn new(message: &'a mut T, buf: &'a mut BytesMut) -> Self {
2626
let body = message.get_body().into_bytes().unwrap();

decompression-plz/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn decompress<T>(
1717
buf: &mut BytesMut,
1818
) -> Result<(), MultiDecompressErrorReason>
1919
where
20-
T: DecompressTrait,
20+
T: DecompressTrait + std::fmt::Debug,
2121
{
2222
let mut state = DecodeState::init(message, buf);
2323
loop {

decompression-plz/src/state.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ where
110110
decode_struct.add_body_and_update_cl();
111111
Err(e)
112112
}
113-
DecodeState::End => Ok(Self::End),
113+
DecodeState::End => Ok(DecodeState::End),
114114
}
115115
}
116116

@@ -221,11 +221,11 @@ mod tests {
221221
}
222222

223223
fn header_map(&self) -> &HeaderMap {
224-
self.header_map()
224+
&self.header_map
225225
}
226226

227227
fn header_map_as_mut(&mut self) -> &mut HeaderMap {
228-
self.header_map_as_mut()
228+
&mut self.header_map
229229
}
230230
}
231231

@@ -244,18 +244,57 @@ mod tests {
244244
extra_body: extra,
245245
}
246246
}
247+
248+
fn into_bytes(self) -> BytesMut {
249+
let mut bytes = self.header_map.into_bytes();
250+
bytes.unsplit(self.body.unwrap().into_bytes().unwrap());
251+
bytes
252+
}
247253
}
248254

249255
#[test]
250-
fn test_decode_init_no_te_or_ce() {
256+
fn test_decode_init_no_enc() {
251257
let headers = "Host: example.com\r\n\
252258
Content-Type: text/html; charset=utf-8\r\n\
253259
Content-Length: 11\r\n\r\n";
254260
let mut tm =
255261
TestMessage::build(headers.into(), Body::Raw(INPUT.into()), None);
256262
let mut buf = BytesMut::new();
257263
let mut state = DecodeState::init(&mut tm, &mut buf);
264+
dbg!(&state);
265+
state = state.try_next().unwrap();
266+
dbg!(&state);
267+
assert!(matches!(state, DecodeState::End));
268+
let result = tm.into_bytes();
269+
let verify = "Host: example.com\r\n\
270+
Content-Type: text/html; charset=utf-8\r\n\
271+
Content-Length: 11\r\n\r\n\
272+
hello world";
273+
assert_eq!(result, verify);
274+
}
275+
276+
#[test]
277+
fn test_decode_init_no_enc_extra_body() {
278+
let headers = "Host: example.com\r\n\
279+
Content-Type: text/html; charset=utf-8\r\n\
280+
Content-Length: 11\r\n\r\n";
281+
let mut tm = TestMessage::build(
282+
headers.into(),
283+
Body::Raw(INPUT.into()),
284+
Some(INPUT.into()),
285+
);
286+
287+
let mut buf = BytesMut::new();
288+
let mut state = DecodeState::init(&mut tm, &mut buf);
289+
state = state.try_next().unwrap();
290+
assert!(matches!(state, DecodeState::UpdateContentLength(_)));
258291
state = state.try_next().unwrap();
259292
assert!(matches!(state, DecodeState::End));
293+
let result = tm.into_bytes();
294+
let verify = "Host: example.com\r\n\
295+
Content-Type: text/html; charset=utf-8\r\n\
296+
Content-Length: 22\r\n\r\n\
297+
hello worldhello world";
298+
assert_eq!(result, verify);
260299
}
261300
}

0 commit comments

Comments
 (0)