Allow RegistryBuilder responder URLs to be a String

This allows tests to generate dynamic URLs for custom responders.
This commit is contained in:
Eric Huss 2023-03-23 05:20:25 -07:00
parent b5d772f303
commit c38e050fc6

View File

@ -97,7 +97,7 @@ pub struct RegistryBuilder {
/// Write the registry in configuration. /// Write the registry in configuration.
configure_registry: bool, configure_registry: bool,
/// API responders. /// API responders.
custom_responders: HashMap<&'static str, Box<dyn Send + Fn(&Request, &HttpServer) -> Response>>, custom_responders: HashMap<String, Box<dyn Send + Fn(&Request, &HttpServer) -> Response>>,
/// If nonzero, the git index update to be delayed by the given number of seconds. /// If nonzero, the git index update to be delayed by the given number of seconds.
delayed_index_update: usize, delayed_index_update: usize,
} }
@ -167,10 +167,11 @@ impl RegistryBuilder {
#[must_use] #[must_use]
pub fn add_responder<R: 'static + Send + Fn(&Request, &HttpServer) -> Response>( pub fn add_responder<R: 'static + Send + Fn(&Request, &HttpServer) -> Response>(
mut self, mut self,
url: &'static str, url: impl Into<String>,
responder: R, responder: R,
) -> Self { ) -> Self {
self.custom_responders.insert(url, Box::new(responder)); self.custom_responders
.insert(url.into(), Box::new(responder));
self self
} }
@ -601,7 +602,7 @@ pub struct HttpServer {
addr: SocketAddr, addr: SocketAddr,
token: Token, token: Token,
auth_required: bool, auth_required: bool,
custom_responders: HashMap<&'static str, Box<dyn Send + Fn(&Request, &HttpServer) -> Response>>, custom_responders: HashMap<String, Box<dyn Send + Fn(&Request, &HttpServer) -> Response>>,
delayed_index_update: usize, delayed_index_update: usize,
} }
@ -621,10 +622,7 @@ impl HttpServer {
api_path: PathBuf, api_path: PathBuf,
token: Token, token: Token,
auth_required: bool, auth_required: bool,
api_responders: HashMap< api_responders: HashMap<String, Box<dyn Send + Fn(&Request, &HttpServer) -> Response>>,
&'static str,
Box<dyn Send + Fn(&Request, &HttpServer) -> Response>,
>,
delayed_index_update: usize, delayed_index_update: usize,
) -> HttpServerHandle { ) -> HttpServerHandle {
let listener = TcpListener::bind("127.0.0.1:0").unwrap(); let listener = TcpListener::bind("127.0.0.1:0").unwrap();