This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27e69e9
commit 68fe2bb
Showing
9 changed files
with
137 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::net::SocketAddr; | ||
use std::str::FromStr; | ||
use std::string::ToString; | ||
use std::sync::Arc; | ||
|
||
use once_cell::sync::Lazy; | ||
use rand::distributions::Alphanumeric; | ||
use rand::Rng; | ||
use tokio::net::TcpListener; | ||
use tokio::sync::Notify; | ||
|
||
use feature_flags::config::Config; | ||
use feature_flags::server::serve; | ||
|
||
pub static DEFAULT_CONFIG: Lazy<Config> = Lazy::new(|| Config { | ||
address: SocketAddr::from_str("127.0.0.1:0").unwrap(), | ||
redis_url: "redis://localhost:6379/".to_string(), | ||
write_database_url: "postgres://posthog:posthog@localhost:15432/test_database".to_string(), | ||
read_database_url: "postgres://posthog:posthog@localhost:15432/test_database".to_string(), | ||
max_concurrent_jobs: 1024, | ||
max_pg_connections: 100, | ||
}); | ||
|
||
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) | ||
.send() | ||
.await | ||
.expect("failed to send request") | ||
} | ||
} | ||
|
||
impl Drop for ServerHandle { | ||
fn drop(&mut self) { | ||
self.shutdown.notify_one() | ||
} | ||
} | ||
|
||
pub fn random_string(prefix: &str, length: usize) -> String { | ||
let suffix: String = rand::thread_rng() | ||
.sample_iter(Alphanumeric) | ||
.take(length) | ||
.map(char::from) | ||
.collect(); | ||
format!("{}_{}", prefix, suffix) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use anyhow::Result; | ||
use assert_json_diff::assert_json_include; | ||
|
||
use reqwest::StatusCode; | ||
use serde_json::{json, Value}; | ||
|
||
use crate::common::*; | ||
mod common; | ||
|
||
#[tokio::test] | ||
async fn it_sends_flag_request() -> Result<()> { | ||
let token = random_string("token", 16); | ||
let distinct_id = "user_distinct_id".to_string(); | ||
|
||
let config = DEFAULT_CONFIG.clone(); | ||
|
||
let server = ServerHandle::for_config(config).await; | ||
|
||
let payload = json!({ | ||
"token": token, | ||
"distinct_id": distinct_id, | ||
"groups": {"group1": "group1"} | ||
}); | ||
let res = server.send_flags_request(payload.to_string()).await; | ||
assert_eq!(StatusCode::OK, res.status()); | ||
|
||
// We don't want to deserialize the data into a flagResponse struct here, | ||
// because we want to assert the shape of the raw json data. | ||
let json_data = res.json::<Value>().await?; | ||
|
||
assert_json_include!( | ||
actual: json_data, | ||
expected: json!({ | ||
"errorWhileComputingFlags": false, | ||
"featureFlags": { | ||
"beta-feature": "variant-1", | ||
"rollout-flag": "true", | ||
} | ||
}) | ||
); | ||
|
||
Ok(()) | ||
} |