http_endpoint_server_harness/entities/
endpoint.rs

1use super::{Handler, Method};
2
3/// Represents an HTTP endpoint with a path, method, and handlers
4#[derive(Debug, Clone)]
5pub struct Endpoint {
6    pub path: String,
7    pub method: Method,
8    pub handlers: Vec<Handler>,
9}
10
11impl Endpoint {
12    pub fn new(path: impl Into<String>, method: Method) -> Self {
13        Self {
14            path: path.into(),
15            method,
16            handlers: Vec::new(),
17        }
18    }
19
20    pub fn with_handler(mut self, handler: Handler) -> Self {
21        self.handlers.push(handler);
22        self
23    }
24
25    pub fn with_handlers(mut self, handlers: impl IntoIterator<Item = Handler>) -> Self {
26        self.handlers.extend(handlers);
27        self
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn test_endpoint_new() {
37        let endpoint = Endpoint::new("/api/test", Method::Get);
38        assert_eq!(endpoint.path, "/api/test");
39        assert_eq!(endpoint.method, Method::Get);
40        assert!(endpoint.handlers.is_empty());
41    }
42
43    #[test]
44    fn test_endpoint_with_handler() {
45        let handler = Handler::from_json(&serde_json::json!({}));
46        let endpoint = Endpoint::new("/api/test", Method::Post).with_handler(handler);
47        assert_eq!(endpoint.handlers.len(), 1);
48    }
49
50    #[test]
51    fn test_endpoint_with_multiple_handlers() {
52        let endpoint = Endpoint::new("/api/test", Method::Get)
53            .with_handler(Handler::from_json(&serde_json::json!({"first": true})))
54            .with_handler(Handler::from_json(&serde_json::json!({"second": true})));
55        assert_eq!(endpoint.handlers.len(), 2);
56    }
57}
58