Skip to main content

earl_protocol_http/
schema.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use earl_core::schema::{AuthTemplate, BodyTemplate, TransportTemplate};
7
8#[derive(Debug, Clone, Deserialize, Serialize)]
9#[serde(deny_unknown_fields)]
10pub struct HttpOperationTemplate {
11    pub method: String,
12    pub url: String,
13    pub path: Option<String>,
14    pub query: Option<BTreeMap<String, Value>>,
15    pub headers: Option<BTreeMap<String, Value>>,
16    pub cookies: Option<BTreeMap<String, Value>>,
17    pub auth: Option<AuthTemplate>,
18    pub body: Option<BodyTemplate>,
19    #[serde(default)]
20    pub stream: bool,
21    pub transport: Option<TransportTemplate>,
22}
23
24#[derive(Debug, Clone, Deserialize, Serialize)]
25#[serde(deny_unknown_fields)]
26pub struct GraphqlOperationTemplate {
27    #[serde(default)]
28    pub method: String,
29    pub url: String,
30    pub path: Option<String>,
31    pub query: Option<BTreeMap<String, Value>>,
32    pub headers: Option<BTreeMap<String, Value>>,
33    pub cookies: Option<BTreeMap<String, Value>>,
34    pub auth: Option<AuthTemplate>,
35    pub graphql: GraphqlTemplate,
36    #[serde(default)]
37    pub stream: bool,
38    pub transport: Option<TransportTemplate>,
39}
40
41#[derive(Debug, Clone, Deserialize, Serialize)]
42#[serde(deny_unknown_fields)]
43pub struct GraphqlTemplate {
44    pub query: String,
45    pub operation_name: Option<String>,
46    pub variables: Option<Value>,
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn http_operation_defaults_stream_false() {
55        let json = r#"{"method":"GET","url":"https://example.com"}"#;
56        let op: HttpOperationTemplate = serde_json::from_str(json).unwrap();
57        assert!(!op.stream);
58    }
59
60    #[test]
61    fn http_operation_accepts_stream_true() {
62        let json = r#"{"method":"GET","url":"https://example.com","stream":true}"#;
63        let op: HttpOperationTemplate = serde_json::from_str(json).unwrap();
64        assert!(op.stream);
65    }
66
67    #[test]
68    fn graphql_operation_defaults_stream_false() {
69        let json = r#"{"url":"https://example.com","graphql":{"query":"{ users { id } }"}}"#;
70        let op: GraphqlOperationTemplate = serde_json::from_str(json).unwrap();
71        assert!(!op.stream);
72    }
73
74    #[test]
75    fn graphql_operation_accepts_stream_true() {
76        let json =
77            r#"{"url":"https://example.com","stream":true,"graphql":{"query":"{ users { id } }"}}"#;
78        let op: GraphqlOperationTemplate = serde_json::from_str(json).unwrap();
79        assert!(op.stream);
80    }
81}