@@ -178,88 +178,111 @@ where
178178 }
179179}
180180
181+ /*
181182#[cfg(test)]
182183mod 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+ */
0 commit comments