11use super :: * ;
22use body_plz:: variants:: chunked:: ChunkType ;
33use decompression_plz:: chunked:: { chunked_to_raw, partial_chunked_to_raw} ;
4- use tests_utils:: all_compressed_data;
4+ use tests_utils:: { INPUT , all_compressed_data} ;
55
66const HEADERS : & str = "Host: example.com\r \n \
77 Content-Type: text/html; charset=utf-8\r \n \
@@ -129,51 +129,192 @@ fn build_all_compressed_chunk_body() -> Body {
129129 Body :: Chunked ( chunk_vec)
130130}
131131
132- #[ test ]
133- fn test_chunked_with_compression ( ) {
134- let headers = "Host: example.com \r \n \
135- Content-Type: text/html; charset=utf-8 \r \n \
136- Transfer-Encoding: br, deflate, identity, gzip, zstd, chunked \r \n \
137- \r \n " ;
132+ #[ track_caller ]
133+ fn assert_chunked_encoding (
134+ headers : & str ,
135+ with_ce : bool ,
136+ extra : Option < BytesMut > ,
137+ ) {
138138 let mut buf = BytesMut :: new ( ) ;
139139
140+ let verify = if extra. is_none ( ) {
141+ "Host: example.com\r \n \
142+ Content-Type: text/html; charset=utf-8\r \n \
143+ Content-Length: 11\r \n \r \n \
144+ hello world"
145+ } else {
146+ "Host: example.com\r \n \
147+ Content-Type: text/html; charset=utf-8\r \n \
148+ Content-Length: 22\r \n \r \n \
149+ hello worldhello world"
150+ } ;
151+
140152 let mut tm = TestMessage :: build (
141153 headers. into ( ) ,
142154 build_all_compressed_chunk_body ( ) ,
143- None ,
155+ extra ,
144156 ) ;
157+
145158 let mut state = DecodeState :: init ( & mut tm, & mut buf) ;
146159 state = state. try_next ( ) . unwrap ( ) ;
147160 assert ! ( matches!( state, DecodeState :: TransferEncoding ( ..) ) ) ;
148161
162+ if with_ce {
163+ state = state. try_next ( ) . unwrap ( ) ;
164+ assert ! ( matches!( state, DecodeState :: ContentEncoding ( ..) ) ) ;
165+ }
166+
149167 state = state. try_next ( ) . unwrap ( ) ;
150168 assert ! ( matches!( state, DecodeState :: UpdateContentLength ( ..) ) ) ;
151169
152170 state = state. try_next ( ) . unwrap ( ) ;
153171 assert ! ( state. is_ended( ) ) ;
154172
155- let verify = "Host: example.com\r \n \
156- Content-Type: text/html; charset=utf-8\r \n \
157- Content-Length: 11\r \n \r \n \
158- hello world";
159173 let result = tm. into_bytes ( ) ;
160174 assert_eq ! ( result, verify) ;
161175}
162176
177+ #[ test]
178+ fn test_chunked_with_compression ( ) {
179+ let headers = "Host: example.com\r \n \
180+ Content-Type: text/html; charset=utf-8\r \n \
181+ Transfer-Encoding: br, deflate, identity, gzip, zstd, chunked\r \n \
182+ \r \n ";
183+
184+ assert_chunked_encoding ( headers, false , None ) ;
185+ }
186+
187+ #[ test]
188+ fn test_chunked_with_compression_extra_raw ( ) {
189+ let headers = "Host: example.com\r \n \
190+ Content-Type: text/html; charset=utf-8\r \n \
191+ Transfer-Encoding: br, deflate, identity, gzip, zstd, chunked\r \n \
192+ \r \n ";
193+
194+ assert_chunked_encoding ( headers, false , Some ( INPUT . into ( ) ) ) ;
195+ }
196+
197+ #[ test]
198+ fn test_chunked_with_compress_extra_compressed_together ( ) {
199+ let body = all_compressed_data ( ) ; // len 53
200+ let mut chunk_vec = vec ! [
201+ ChunkType :: Size ( "10\r \n " . into( ) ) ,
202+ ChunkType :: Chunk ( body[ 0 ..10 ] . into( ) ) ,
203+ ChunkType :: Size ( "10\r \n " . into( ) ) ,
204+ ChunkType :: Chunk ( body[ 10 ..20 ] . into( ) ) ,
205+ ChunkType :: Size ( "10\r \n " . into( ) ) ,
206+ ChunkType :: Chunk ( body[ 20 ..30 ] . into( ) ) ,
207+ ChunkType :: Size ( "10\r \n " . into( ) ) ,
208+ ChunkType :: Chunk ( body[ 30 ..40 ] . into( ) ) ,
209+ ChunkType :: Size ( "10\r \n " . into( ) ) ,
210+ ] ;
211+
212+ for chunk in chunk_vec. iter_mut ( ) {
213+ if let ChunkType :: Chunk ( chunk) = chunk {
214+ chunk. extend_from_slice ( "\r \n " . as_bytes ( ) ) ;
215+ }
216+ }
217+ let chunk_body = Body :: Chunked ( chunk_vec) ;
218+ let extra = BytesMut :: from ( & body[ 40 ..] ) ;
219+
220+ let headers = "Host: example.com\r \n \
221+ Content-Type: text/html; charset=utf-8\r \n \
222+ Transfer-Encoding: br, deflate, identity, gzip, zstd, chunked\r \n \
223+ \r \n ";
224+
225+ let mut tm = TestMessage :: build ( headers. into ( ) , chunk_body, Some ( extra) ) ;
226+
227+ let mut buf = BytesMut :: new ( ) ;
228+ let mut state = DecodeState :: init ( & mut tm, & mut buf) ;
229+ state = state. try_next ( ) . unwrap ( ) ;
230+ assert ! ( matches!( state, DecodeState :: TransferEncoding ( ..) ) ) ;
231+
232+ state = state. try_next ( ) . unwrap ( ) ;
233+ assert ! ( matches!( state, DecodeState :: UpdateContentLength ( ..) ) ) ;
234+
235+ state = state. try_next ( ) . unwrap ( ) ;
236+ assert ! ( state. is_ended( ) ) ;
237+ assert_eq ! ( tm. into_bytes( ) , VERIFY_SINGLE_HEADER_BODY_ONLY ) ;
238+ }
239+
240+ #[ test]
241+ fn test_chunked_with_compress_extra_compressed_separate ( ) {
242+ let body = build_all_compressed_chunk_body ( ) ;
243+ let extra = all_compressed_data ( ) ;
244+
245+ let headers = "Host: example.com\r \n \
246+ Content-Type: text/html; charset=utf-8\r \n \
247+ Transfer-Encoding: br, deflate, identity, gzip, zstd, chunked\r \n \
248+ \r \n ";
249+
250+ let mut tm = TestMessage :: build ( headers. into ( ) , body, Some ( extra) ) ;
251+
252+ let mut buf = BytesMut :: new ( ) ;
253+ let mut state = DecodeState :: init ( & mut tm, & mut buf) ;
254+ state = state. try_next ( ) . unwrap ( ) ;
255+ assert ! ( matches!( state, DecodeState :: TransferEncoding ( ..) ) ) ;
256+
257+ state = state. try_next ( ) . unwrap ( ) ;
258+ assert ! ( matches!( state, DecodeState :: UpdateContentLength ( ..) ) ) ;
259+
260+ state = state. try_next ( ) . unwrap ( ) ;
261+ assert ! ( state. is_ended( ) ) ;
262+ assert_eq ! ( tm. into_bytes( ) , VERIFY_SINGLE_HEADER_BODY_AND_EXTRA ) ;
263+ }
264+
265+ /// Ce
163266#[ test]
164267fn test_chunked_with_ce_compression ( ) {
165268 let headers = "Host: example.com\r \n \
166269 Content-Type: text/html; charset=utf-8\r \n \
167270 Transfer-Encoding: chunked\r \n \
271+ Content-Encoding: br, deflate, identity, gzip, zstd\r \n \r \n ";
272+
273+ assert_chunked_encoding ( headers, true , None ) ;
274+ }
275+
276+ #[ test]
277+ fn test_chunked_with_ce_compression_extra_raw ( ) {
278+ let headers = "Host: example.com\r \n \
279+ Content-Type: text/html; charset=utf-8\r \n \
280+ Transfer-Encoding: chunked\r \n \
281+ Content-Encoding: br, deflate, identity, gzip, zstd\r \n \r \n ";
282+
283+ assert_chunked_encoding ( headers, true , Some ( INPUT . into ( ) ) ) ;
284+ }
285+
286+ #[ test]
287+ fn test_chunked_with_ce_compress_extra_compressed_together ( ) {
288+ let body = all_compressed_data ( ) ; // len 53
289+ let mut chunk_vec = vec ! [
290+ ChunkType :: Size ( "10\r \n " . into( ) ) ,
291+ ChunkType :: Chunk ( body[ 0 ..10 ] . into( ) ) ,
292+ ChunkType :: Size ( "10\r \n " . into( ) ) ,
293+ ChunkType :: Chunk ( body[ 10 ..20 ] . into( ) ) ,
294+ ChunkType :: Size ( "10\r \n " . into( ) ) ,
295+ ChunkType :: Chunk ( body[ 20 ..30 ] . into( ) ) ,
296+ ChunkType :: Size ( "10\r \n " . into( ) ) ,
297+ ChunkType :: Chunk ( body[ 30 ..40 ] . into( ) ) ,
298+ ChunkType :: Size ( "10\r \n " . into( ) ) ,
299+ ] ;
300+
301+ for chunk in chunk_vec. iter_mut ( ) {
302+ if let ChunkType :: Chunk ( chunk) = chunk {
303+ chunk. extend_from_slice ( "\r \n " . as_bytes ( ) ) ;
304+ }
305+ }
306+ let chunk_body = Body :: Chunked ( chunk_vec) ;
307+ let extra = BytesMut :: from ( & body[ 40 ..] ) ;
308+
309+ let headers = "Host: example.com\r \n \
310+ Content-Type: text/html; charset=utf-8\r \n \
168311 Content-Encoding: br, deflate, identity, gzip, zstd\r \n \
312+ Transfer-Encoding: chunked\r \n \
169313 \r \n ";
170- let mut buf = BytesMut :: new ( ) ;
171314
172- let mut tm = TestMessage :: build (
173- headers. into ( ) ,
174- build_all_compressed_chunk_body ( ) ,
175- None ,
176- ) ;
315+ let mut tm = TestMessage :: build ( headers. into ( ) , chunk_body, Some ( extra) ) ;
316+
317+ let mut buf = BytesMut :: new ( ) ;
177318 let mut state = DecodeState :: init ( & mut tm, & mut buf) ;
178319 state = state. try_next ( ) . unwrap ( ) ;
179320 assert ! ( matches!( state, DecodeState :: TransferEncoding ( ..) ) ) ;
@@ -186,11 +327,36 @@ fn test_chunked_with_ce_compression() {
186327
187328 state = state. try_next ( ) . unwrap ( ) ;
188329 assert ! ( state. is_ended( ) ) ;
330+ assert_eq ! ( tm. into_bytes( ) , VERIFY_SINGLE_HEADER_BODY_ONLY ) ;
331+ }
189332
190- let verify = "Host: example.com\r \n \
191- Content-Type: text/html; charset=utf-8\r \n \
192- Content-Length: 11\r \n \r \n \
193- hello world";
194- let result = tm. into_bytes ( ) ;
195- assert_eq ! ( result, verify) ;
333+ #[ test]
334+ fn test_chunked_with_ce_compress_extra_compressed_separate ( ) {
335+ let body = build_all_compressed_chunk_body ( ) ;
336+ let extra = all_compressed_data ( ) ;
337+
338+ let headers = "Host: example.com\r \n \
339+ Content-Type: text/html; charset=utf-8\r \n \
340+ Content-Encoding: br, deflate, identity, gzip, zstd\r \n \
341+ Transfer-Encoding: chunked\r \n \
342+ \r \n ";
343+
344+ let mut tm = TestMessage :: build ( headers. into ( ) , body, Some ( extra) ) ;
345+
346+ let mut buf = BytesMut :: new ( ) ;
347+ let mut state = DecodeState :: init ( & mut tm, & mut buf) ;
348+ state = state. try_next ( ) . unwrap ( ) ;
349+ assert ! ( matches!( state, DecodeState :: TransferEncoding ( ..) ) ) ;
350+
351+ state = state. try_next ( ) . unwrap ( ) ;
352+ assert ! ( matches!( state, DecodeState :: ContentEncoding ( ..) ) ) ;
353+
354+ state = state. try_next ( ) . unwrap ( ) ;
355+ assert ! ( matches!( state, DecodeState :: UpdateContentLength ( ..) ) ) ;
356+
357+ state = state. try_next ( ) . unwrap ( ) ;
358+ assert ! ( state. is_ended( ) ) ;
359+ assert_eq ! ( tm. into_bytes( ) , VERIFY_SINGLE_HEADER_BODY_AND_EXTRA ) ;
196360}
361+
362+ // Partial
0 commit comments