0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-30 19:41:46 +01:00
posthog/rust/feature-flags/tests/common/mod.rs
Neil Kakkar 50adf3b4ae
feat(flags): Fetch flags and team from database (#23120)
Co-authored-by: Dylan Martin <dylan@posthog.com>
Co-authored-by: James Greenhill <fuziontech@gmail.com>
2024-07-24 08:40:09 -07:00

60 lines
1.6 KiB
Rust

use std::net::SocketAddr;
use std::sync::Arc;
use reqwest::header::CONTENT_TYPE;
use tokio::net::TcpListener;
use tokio::sync::Notify;
use feature_flags::config::Config;
use feature_flags::server::serve;
pub struct ServerHandle {
pub addr: SocketAddr,
shutdown: Arc<Notify>,
}
impl ServerHandle {
pub async fn for_config(config: Config) -> ServerHandle {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let notify = Arc::new(Notify::new());
let shutdown = notify.clone();
tokio::spawn(async move {
serve(config, listener, async move { notify.notified().await }).await
});
ServerHandle { addr, shutdown }
}
pub async fn send_flags_request<T: Into<reqwest::Body>>(&self, body: T) -> reqwest::Response {
let client = reqwest::Client::new();
client
.post(format!("http://{:?}/flags", self.addr))
.body(body)
.header(CONTENT_TYPE, "application/json")
.send()
.await
.expect("failed to send request")
}
pub async fn send_invalid_header_for_flags_request<T: Into<reqwest::Body>>(
&self,
body: T,
) -> reqwest::Response {
let client = reqwest::Client::new();
client
.post(format!("http://{:?}/flags", self.addr))
.body(body)
.header(CONTENT_TYPE, "xyz")
.send()
.await
.expect("failed to send request")
}
}
impl Drop for ServerHandle {
fn drop(&mut self) {
self.shutdown.notify_one()
}
}