Skip to content

Commit f4c6d06

Browse files
committed
test(decompression-plz): add TestMessage which implements DecompressionTrait
1 parent 190a305 commit f4c6d06

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

decompression-plz/src/decode_struct.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::chunked::chunked_to_raw;
88
use crate::content_length::add_body_and_update_cl;
99
use crate::decompress_trait::DecompressTrait;
1010

11+
#[cfg_attr(test, derive(PartialEq))]
12+
#[derive(Debug)]
1113
pub struct DecodeStruct<'a, T> {
1214
pub message: &'a mut T,
1315
pub body: BytesMut,

decompression-plz/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ where
3333
// helper function for tests
3434
#[cfg(test)]
3535
pub mod tests {
36+
use crate::DecompressTrait;
3637
use body_plz::variants::Body;
3738
use bytes::BytesMut;
3839
use flate2::Compression;
@@ -43,9 +44,6 @@ pub mod tests {
4344
},
4445
};
4546
use std::io::Write;
46-
47-
use crate::DecompressTrait;
48-
4947
pub const INPUT: &[u8] = b"hello world";
5048

5149
pub fn all_compressed_data() -> Vec<u8> {

decompression-plz/src/state.rs

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use crate::{
1313
},
1414
};
1515

16+
#[cfg_attr(test, derive(PartialEq))]
17+
#[derive(Debug)]
1618
pub enum DecodeState<'a, T> {
1719
Start(DecodeStruct<'a, T>),
1820
TransferEncoding(DecodeStruct<'a, T>, Vec<EncodingInfo>),
@@ -178,13 +180,76 @@ where
178180
#[cfg(test)]
179181
mod tests {
180182

181-
use crate::decompress;
182-
use crate::decompress_trait::DecompressTrait;
183183
use body_plz::variants::Body;
184-
use header_plz::InfoLine;
185-
use header_plz::Request;
186-
use header_plz::body_headers::parse::ParseBodyHeaders;
187-
use header_plz::message_head::MessageHead;
184+
use bytes::BytesMut;
185+
use header_plz::{
186+
HeaderMap, abnf::HEADER_DELIMITER, body_headers::BodyHeader,
187+
message_head::MessageHead,
188+
};
188189

189-
use super::*;
190+
use crate::{DecompressTrait, state::DecodeState, tests::INPUT};
191+
192+
#[derive(Debug, PartialEq)]
193+
struct TestMessage {
194+
header_map: HeaderMap,
195+
body_header: Option<BodyHeader>,
196+
body: Option<Body>,
197+
extra_body: Option<BytesMut>,
198+
}
199+
200+
impl DecompressTrait for TestMessage {
201+
fn get_body(&mut self) -> Body {
202+
self.body.take().unwrap()
203+
}
204+
205+
fn get_extra_body(&mut self) -> Option<BytesMut> {
206+
self.extra_body.take()
207+
}
208+
209+
fn set_body(&mut self, body: Body) {
210+
self.body = Some(body);
211+
}
212+
213+
fn body_headers_as_mut(&mut self) -> &mut Option<BodyHeader> {
214+
&mut self.body_header
215+
}
216+
217+
fn header_map(&self) -> &HeaderMap {
218+
self.header_map()
219+
}
220+
221+
fn header_map_as_mut(&mut self) -> &mut HeaderMap {
222+
self.header_map_as_mut()
223+
}
224+
}
225+
226+
impl TestMessage {
227+
fn build(
228+
headers: BytesMut,
229+
body: Body,
230+
extra: Option<BytesMut>,
231+
) -> Self {
232+
let header_map = HeaderMap::from(headers);
233+
let body_header = BodyHeader::from(&header_map);
234+
Self {
235+
header_map,
236+
body_header: Some(body_header),
237+
body: Some(body),
238+
extra_body: extra,
239+
}
240+
}
241+
}
242+
243+
#[test]
244+
fn test_decode_init_no_te_or_ce() {
245+
let headers = "Host: example.com\r\n\
246+
Content-Type: text/html; charset=utf-8\r\n\
247+
Content-Length: 11\r\n\r\n";
248+
let mut tm =
249+
TestMessage::build(headers.into(), Body::Raw(INPUT.into()), None);
250+
let mut buf = BytesMut::new();
251+
let mut state = DecodeState::init(&mut tm, &mut buf);
252+
state = state.try_next().unwrap();
253+
assert!(matches!(state, DecodeState::End));
254+
}
190255
}

0 commit comments

Comments
 (0)