mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-07-11 02:29:25 +00:00
retrieve key and topic from query string
This commit is contained in:
@@ -78,7 +78,7 @@ impl KafkaEventReaderImpl {
|
||||
let event = Event {topic: String::from(topic), key: std::str::from_utf8(m.key).unwrap().parse().unwrap(), msg: std::str::from_utf8(m.value).unwrap().parse().unwrap() };
|
||||
events.push(event);
|
||||
}
|
||||
self.consumer.consume_messageset(ms);
|
||||
self.consumer.consume_messageset(ms).unwrap();
|
||||
}
|
||||
self.consumer.commit_consumed().unwrap();
|
||||
events
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use crate::http::HttpServer;
|
||||
|
||||
mod http;
|
||||
mod kafka;
|
||||
pub mod http;
|
||||
pub mod kafka;
|
||||
pub mod utils;
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn main() {
|
||||
HttpServer::new([127, 0, 0, 1], 4000).run().await.expect("failed to run server");
|
||||
|
||||
@@ -5,11 +5,42 @@ pub mod http_utils {
|
||||
pub fn get_query_params(req: &Request<Body>) -> HashMap<&str, &str> {
|
||||
let uri = req.uri();
|
||||
let query = uri.query();
|
||||
println!("uri={:?}, query={:?}", uri, query);
|
||||
match query {
|
||||
None => HashMap::new(),
|
||||
Some(query) => {
|
||||
query.split("&").map(|s| s.split_at(query.find("=").unwrap())).collect()
|
||||
query.split("&").map(|s| s.split_at(s.find("=").unwrap())).map(|(key, value)| (key, &value[1..])).collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod http_utils_tests {
|
||||
use rstest::rstest;
|
||||
use std::collections::HashMap;
|
||||
use hyper::{Body, Request, Uri};
|
||||
use hyper::http::uri::{Builder, Parts};
|
||||
use crate::utils::http_utils::get_query_params;
|
||||
use super::*;
|
||||
|
||||
#[rstest]
|
||||
#[case(
|
||||
"?test=abc",
|
||||
HashMap::from([("test", "abc")])
|
||||
)]
|
||||
#[case(
|
||||
"?test=abc&help=123",
|
||||
HashMap::from([("test", "abc"), ("help", "123")])
|
||||
)]
|
||||
#[case(
|
||||
"show?topic=status&key=abc",
|
||||
HashMap::from([("topic", "status"), ("key", "abc")])
|
||||
)]
|
||||
fn get_query_params_tests(#[case] query_str: &str, #[case] expected: HashMap<&str, &str>) {
|
||||
let uri = Builder::new().scheme("https").authority("behnke.rs").path_and_query(query_str).build().unwrap();
|
||||
let req = Request::get(uri).body(Body::empty()).unwrap();
|
||||
let res = get_query_params(&req);
|
||||
assert_eq!(res, expected)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
mod test {
|
||||
pub mod tests {
|
||||
use std::collections::HashMap;
|
||||
use rstest::rstest;
|
||||
|
||||
@@ -8,7 +8,7 @@ mod test {
|
||||
HashMap::from([("test", "abc")])
|
||||
)]
|
||||
fn get_query_params_tests(#[case] query_str: &str, #[case] expected: HashMap<&str, &str>) {
|
||||
let res = get_(query_str);
|
||||
let res = get_query_params(query_str);
|
||||
assert_eq!(res, expected)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user