From 946c08da5b656899da1868969c3d1033bcc2417b Mon Sep 17 00:00:00 2001 From: "Daniel.Bloom" Date: Thu, 3 Feb 2022 14:18:07 -0800 Subject: [PATCH 1/2] test: regression test for `HttpResponse` decode Test if we can decode without the `update` field. --- ic-utils/src/interfaces/http_request.rs | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/ic-utils/src/interfaces/http_request.rs b/ic-utils/src/interfaces/http_request.rs index 95f7fb41..cc4587a0 100644 --- a/ic-utils/src/interfaces/http_request.rs +++ b/ic-utils/src/interfaces/http_request.rs @@ -126,3 +126,57 @@ impl<'agent> Canister<'agent, HttpRequestCanister> { self.query_(&method.into()).with_arg(token).build() } } + +#[cfg(test)] +mod test { + use candid::{Encode, Decode}; + use super::HttpResponse; + + mod pre_update_legacy { + use candid::{CandidType, Deserialize, Func, Nat}; + use serde_bytes::ByteBuf; + + #[derive(CandidType, Deserialize)] + pub struct Token { + key: String, + content_encoding: String, + index: Nat, + sha256: Option, + } + + #[derive(CandidType, Deserialize)] + pub struct CallbackStrategy { + pub callback: Func, + pub token: Token, + } + + #[derive(CandidType, Clone, Deserialize)] + pub struct HeaderField(pub String, pub String); + + #[derive(CandidType, Deserialize)] + pub enum StreamingStrategy { + Callback(CallbackStrategy), + } + + #[derive(CandidType, Deserialize)] + pub struct HttpResponse { + pub status_code: u16, + pub headers: Vec, + #[serde(with = "serde_bytes")] + pub body: Vec, + pub streaming_strategy: Option, + } + } + + #[test] + fn deserialize_legacy_http_response() { + let bytes: Vec = Encode!(&pre_update_legacy::HttpResponse{ + status_code: 100, + headers: Vec::new(), + body: Vec::new(), + streaming_strategy: None, + }).unwrap(); + + let _response = Decode!(&bytes, HttpResponse).unwrap(); + } +} \ No newline at end of file From a1ee134165a89fa980154f70f990277f651cfd25 Mon Sep 17 00:00:00 2001 From: "Daniel.Bloom" Date: Thu, 3 Feb 2022 14:19:27 -0800 Subject: [PATCH 2/2] cargo fmt --- ic-utils/src/interfaces/http_request.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ic-utils/src/interfaces/http_request.rs b/ic-utils/src/interfaces/http_request.rs index cc4587a0..a940fa2c 100644 --- a/ic-utils/src/interfaces/http_request.rs +++ b/ic-utils/src/interfaces/http_request.rs @@ -129,8 +129,8 @@ impl<'agent> Canister<'agent, HttpRequestCanister> { #[cfg(test)] mod test { - use candid::{Encode, Decode}; use super::HttpResponse; + use candid::{Decode, Encode}; mod pre_update_legacy { use candid::{CandidType, Deserialize, Func, Nat}; @@ -143,21 +143,21 @@ mod test { index: Nat, sha256: Option, } - + #[derive(CandidType, Deserialize)] pub struct CallbackStrategy { pub callback: Func, pub token: Token, } - + #[derive(CandidType, Clone, Deserialize)] pub struct HeaderField(pub String, pub String); - + #[derive(CandidType, Deserialize)] pub enum StreamingStrategy { Callback(CallbackStrategy), } - + #[derive(CandidType, Deserialize)] pub struct HttpResponse { pub status_code: u16, @@ -170,13 +170,14 @@ mod test { #[test] fn deserialize_legacy_http_response() { - let bytes: Vec = Encode!(&pre_update_legacy::HttpResponse{ + let bytes: Vec = Encode!(&pre_update_legacy::HttpResponse { status_code: 100, headers: Vec::new(), body: Vec::new(), streaming_strategy: None, - }).unwrap(); + }) + .unwrap(); let _response = Decode!(&bytes, HttpResponse).unwrap(); } -} \ No newline at end of file +}