@@ -10,7 +10,7 @@ use std::{
1010 sync:: { LazyLock , Mutex , Once } ,
1111} ;
1212
13- use http_body_util:: Full ;
13+ use http_body_util:: { BodyExt , Full } ;
1414use hyper:: {
1515 body:: { self , Bytes } ,
1616 server:: conn:: http1,
@@ -45,7 +45,28 @@ impl Drop for HyperFixtureHandle {
4545 }
4646}
4747
48- type FixtureAssertionResult = ( Response < Full < Bytes > > , Result < ( ) , Request < body:: Incoming > > ) ;
48+ // BoxedBody for supporting streaming/chunked responses
49+ type BoxedBody = http_body_util:: combinators:: BoxBody < Bytes , hyper:: Error > ;
50+
51+ // New type allowing both Full<Bytes> and BoxedBody response types
52+ // This makes existing tests compatible with the new changes
53+ type FixtureAssertionResult = ( ResponseWrapper , Result < ( ) , Request < body:: Incoming > > ) ;
54+
55+ struct ResponseWrapper ( Response < BoxedBody > ) ;
56+
57+ impl From < Response < Full < Bytes > > > for ResponseWrapper {
58+ fn from ( resp : Response < Full < Bytes > > ) -> Self {
59+ let resp = resp. map ( |body| body. map_err ( |_| unreachable ! ( ) ) . boxed ( ) ) ;
60+ ResponseWrapper ( resp)
61+ }
62+ }
63+
64+ impl From < Response < BoxedBody > > for ResponseWrapper {
65+ fn from ( resp : Response < BoxedBody > ) -> Self {
66+ ResponseWrapper ( resp)
67+ }
68+ }
69+
4970struct HyperServiceFixture {
5071 svc : Box <
5172 dyn Fn (
@@ -56,20 +77,28 @@ struct HyperServiceFixture {
5677 > ,
5778 assertion_failed_request : Option < Request < body:: Incoming > > ,
5879}
80+
5981static HYPER_SERVICE_FIXTURES : Mutex < BTreeMap < String , HyperServiceFixture > > =
6082 Mutex :: new ( BTreeMap :: new ( ) ) ;
6183
62- fn add_hyper_fixture < Fut : Future < Output = FixtureAssertionResult > + Send + ' static > (
84+ fn add_hyper_fixture < Fut , Resp > (
6385 url : impl Into < String > ,
6486 svc_fn : impl Fn ( Request < body:: Incoming > ) -> Fut + Send + Sync + ' static ,
65- ) -> HyperFixtureHandle {
87+ ) -> HyperFixtureHandle
88+ where
89+ Fut : Future < Output = ( Resp , Result < ( ) , Request < body:: Incoming > > ) > + Send + ' static ,
90+ Resp : Into < ResponseWrapper > ,
91+ {
6692 let mut url: String = url. into ( ) ;
6793 if !url. starts_with ( '/' ) {
6894 url. insert ( 0 , '/' ) ;
6995 }
7096 let svc = Box :: new ( move |req| {
7197 let fut = svc_fn ( req) ;
72- Box :: pin ( async move { fut. await } ) as _
98+ Box :: pin ( async move {
99+ let ( resp, result) = fut. await ;
100+ ( resp. into ( ) , result)
101+ } ) as _
73102 } ) ;
74103 let fixture = HyperServiceFixture {
75104 svc,
@@ -83,20 +112,22 @@ fn add_hyper_fixture<Fut: Future<Output = FixtureAssertionResult> + Send + 'stat
83112 HyperFixtureHandle ( url)
84113}
85114
86- async fn handle_service ( req : Request < body:: Incoming > ) -> Result < Response < Full < Bytes > > , Infallible > {
115+ async fn handle_service ( req : Request < body:: Incoming > ) -> Result < Response < BoxedBody > , Infallible > {
87116 let path = req. uri ( ) . path ( ) . to_owned ( ) ;
88117 let fut = {
89118 let services = HYPER_SERVICE_FIXTURES . lock ( ) . unwrap ( ) ;
90119 let fixture = services. get ( & * path) . unwrap ( ) ;
91120 ( fixture. svc ) ( req)
92121 } ;
93122 let ( response, result) = fut. await ;
123+
94124 if let Err ( req) = result {
95125 let mut services = HYPER_SERVICE_FIXTURES . lock ( ) . unwrap ( ) ;
96126 let fixture = services. get_mut ( & * path) . unwrap ( ) ;
97127 fixture. assertion_failed_request = Some ( req) ;
98128 }
99- Ok ( response)
129+
130+ Ok ( response. 0 )
100131}
101132
102133async fn setup_hyper_impl ( ) -> Result < String , io:: Error > {
0 commit comments