Skip to main content

canic_core/dto/
http.rs

1use crate::dto::prelude::*;
2
3pub use crate::domain::http::HttpMethod;
4
5//
6// HttpRequestArgs
7//
8
9#[derive(CandidType, Clone, Debug, Deserialize)]
10pub struct HttpRequestArgs {
11    pub url: String,
12    pub max_response_bytes: Option<u64>,
13    pub method: HttpMethod,
14    pub headers: Vec<HttpHeader>,
15    pub body: Option<Vec<u8>>,
16    pub is_replicated: Option<bool>,
17}
18
19//
20// HttpRequestResult
21//
22
23#[derive(CandidType, Clone, Debug, Deserialize)]
24pub struct HttpRequestResult {
25    pub status: Nat,
26    pub headers: Vec<HttpHeader>,
27    #[serde(with = "serde_bytes")]
28    pub body: Vec<u8>,
29}
30
31//
32// HttpHeader
33//
34
35#[derive(CandidType, Clone, Debug, Deserialize)]
36pub struct HttpHeader {
37    pub name: String,
38    pub value: String,
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use candid::{Decode, Encode};
45
46    #[test]
47    fn http_method_roundtrips_candid_with_canonical_labels() {
48        let bytes = Encode!(&HttpMethod::Get).expect("encode domain http method");
49        let method = Decode!(&bytes, HttpMethod).expect("decode domain http method");
50
51        assert_eq!(method, HttpMethod::Get);
52    }
53
54    #[test]
55    fn http_method_deserializes_canonical_lowercase_labels() {
56        assert_http_method_label(HttpMethod::Get, "get");
57        assert_http_method_label(HttpMethod::Head, "head");
58        assert_http_method_label(HttpMethod::Post, "post");
59    }
60
61    fn assert_http_method_label(method: HttpMethod, label: &str) {
62        let label_bytes = serde_cbor::to_vec(&label).expect("encode HTTP method label");
63        let decoded: HttpMethod =
64            serde_cbor::from_slice(&label_bytes).expect("decode HTTP method label");
65        assert_eq!(decoded, method);
66    }
67}