@@ -173,14 +173,23 @@ impl HttpRecorder {
173173 pub async fn serve (
174174 handler : impl Fn ( & Request ) -> Response + Send + Sync + ' static ,
175175 ) -> Result < Self > {
176- Self :: listen ( None , handler) . await
176+ Self :: builder ( ) . serve ( handler) . await
177+ }
178+
179+ /// A recorder on a chosen port or with TLS, such as on a port the
180+ /// environment published:
181+ ///
182+ /// ```ignore
183+ /// let upstream = HttpRecorder::builder().port(port).tls(&identity).start("ok").await?;
184+ /// ```
185+ pub fn builder ( ) -> HttpRecorderBuilder {
186+ HttpRecorderBuilder :: default ( )
177187 }
178188
179189 /// Answer every request over TLS with `200` and `body`, presenting
180190 /// `identity`'s certificate.
181191 pub async fn start_tls ( identity : & Identity , body : impl Into < String > ) -> Result < Self > {
182- let body = body. into ( ) ;
183- Self :: serve_tls ( identity, move |_| Response :: text ( body. clone ( ) ) ) . await
192+ Self :: builder ( ) . tls ( identity) . start ( body) . await
184193 }
185194
186195 /// Answer every request over TLS with `handler`'s response, presenting
@@ -190,14 +199,15 @@ impl HttpRecorder {
190199 identity : & Identity ,
191200 handler : impl Fn ( & Request ) -> Response + Send + Sync + ' static ,
192201 ) -> Result < Self > {
193- Self :: listen ( Some ( identity. acceptor ( ) ? ) , handler) . await
202+ Self :: builder ( ) . tls ( identity) . serve ( handler) . await
194203 }
195204
196205 async fn listen (
206+ port : u16 ,
197207 tls : Option < TlsAcceptor > ,
198208 handler : impl Fn ( & Request ) -> Response + Send + Sync + ' static ,
199209 ) -> Result < Self > {
200- let ( listener, port) = bind ( ) . await ?;
210+ let ( listener, port) = bind ( port ) . await ?;
201211 let handler: Arc < Handler > = Arc :: new ( handler) ;
202212 let requests = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
203213 let connections = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
@@ -266,6 +276,47 @@ impl HttpRecorder {
266276 }
267277}
268278
279+ /// Options for an [`HttpRecorder`], from [`HttpRecorder::builder`].
280+ #[ derive( Debug , Default ) ]
281+ pub struct HttpRecorderBuilder {
282+ port : u16 ,
283+ tls : Option < Identity > ,
284+ }
285+
286+ impl HttpRecorderBuilder {
287+ /// Listen on `port` instead of a random free one. Fails to start if
288+ /// something already listens there.
289+ #[ must_use]
290+ pub fn port ( mut self , port : u16 ) -> Self {
291+ self . port = port;
292+ self
293+ }
294+
295+ /// Serve TLS, presenting `identity`'s certificate. A connection that
296+ /// fails the TLS handshake counts in
297+ /// [`connections`](HttpRecorder::connections) but records no request.
298+ #[ must_use]
299+ pub fn tls ( mut self , identity : & Identity ) -> Self {
300+ self . tls = Some ( identity. clone ( ) ) ;
301+ self
302+ }
303+
304+ /// Answer every request with `200` and `body`.
305+ pub async fn start ( self , body : impl Into < String > ) -> Result < HttpRecorder > {
306+ let body = body. into ( ) ;
307+ self . serve ( move |_| Response :: text ( body. clone ( ) ) ) . await
308+ }
309+
310+ /// Answer every request with `handler`'s response.
311+ pub async fn serve (
312+ self ,
313+ handler : impl Fn ( & Request ) -> Response + Send + Sync + ' static ,
314+ ) -> Result < HttpRecorder > {
315+ let tls = self . tls . as_ref ( ) . map ( Identity :: acceptor) . transpose ( ) ?;
316+ HttpRecorder :: listen ( self . port , tls, handler) . await
317+ }
318+ }
319+
269320impl Drop for HttpRecorder {
270321 fn drop ( & mut self ) {
271322 self . task . abort ( ) ;
@@ -442,6 +493,34 @@ mod tests {
442493 ) ;
443494 }
444495
496+ #[ tokio:: test]
497+ async fn http_recorder_listens_on_a_chosen_port ( ) {
498+ let port = crate :: free_port ( ) . await . unwrap ( ) ;
499+ let recorder = HttpRecorder :: builder ( )
500+ . port ( port)
501+ . start ( "fixed" )
502+ . await
503+ . unwrap ( ) ;
504+ assert_eq ! ( recorder. port, port) ;
505+ let mut stream = connect ( port) . await ;
506+ stream
507+ . write_all ( b"GET / HTTP/1.1\r \n Host: test\r \n \r \n " )
508+ . await
509+ . unwrap ( ) ;
510+ assert ! ( read_to_end( & mut stream) . await . ends_with( "fixed" ) ) ;
511+
512+ let error = HttpRecorder :: builder ( )
513+ . port ( port)
514+ . start ( "taken" )
515+ . await
516+ . err ( )
517+ . unwrap ( ) ;
518+ assert ! (
519+ error. to_string( ) . contains( & format!( "port {port}" ) ) ,
520+ "{error}"
521+ ) ;
522+ }
523+
445524 #[ tokio:: test]
446525 async fn http_recorder_keeps_a_connection_alive_until_the_request_closes_it ( ) {
447526 let recorder =
0 commit comments