diff --git a/shared/tunnelbroker_messages/src/messages/connection_initialization_response.rs b/shared/tunnelbroker_messages/src/messages/connection_initialization_response.rs new file mode 100644 --- /dev/null +++ b/shared/tunnelbroker_messages/src/messages/connection_initialization_response.rs @@ -0,0 +1,56 @@ +//! Message sent from Tunnelbroker to WebSocket as a response to +//! ConnectionInitializationMessage. + +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, PartialEq, Debug)] +#[serde(tag = "type", content = "data")] +pub enum ConnectionInitializationStatus { + Success, + Error(String), +} + +#[derive(Serialize, Deserialize, PartialEq, Debug)] +#[serde(tag = "type", rename_all = "camelCase")] +pub struct ConnectionInitializationResponse { + pub status: ConnectionInitializationStatus, +} + +#[cfg(test)] +mod connection_init_response_tests { + use super::*; + + #[test] + fn test_connection_init_response_success() { + let example_payload = r#"{ + "type":"ConnectionInitializationResponse", + "status": {"type":"Success"} + }"#; + + let request = + serde_json::from_str::(example_payload) + .unwrap(); + + assert_eq!(request.status, ConnectionInitializationStatus::Success); + } + + #[test] + fn test_connection_init_response_error() { + let example_payload = r#"{ + "type": "ConnectionInitializationResponse", + "status": { + "type":"Error", + "data":"Something went wrong" + } + }"#; + + let request = + serde_json::from_str::(example_payload) + .unwrap(); + + assert_eq!( + request.status, + ConnectionInitializationStatus::Error("Something went wrong".into()) + ); + } +} diff --git a/shared/tunnelbroker_messages/src/messages/mod.rs b/shared/tunnelbroker_messages/src/messages/mod.rs --- a/shared/tunnelbroker_messages/src/messages/mod.rs +++ b/shared/tunnelbroker_messages/src/messages/mod.rs @@ -1,5 +1,6 @@ //! Messages sent between Tunnelbroker and a device. +pub mod connection_initialization_response; pub mod keys; pub mod message_receive_confirmation; pub mod message_to_device; @@ -7,6 +8,7 @@ pub mod message_to_device_request_status; pub mod session; +pub use connection_initialization_response::*; pub use keys::*; pub use message_receive_confirmation::*; pub use message_to_device::*; @@ -21,6 +23,7 @@ pub enum Messages { RefreshKeysRequest(RefreshKeyRequest), ConnectionInitializationMessage(ConnectionInitializationMessage), + ConnectionInitializationResponse(ConnectionInitializationResponse), // MessageToDeviceRequestStatus must be placed before MessageToDeviceRequest. // This is due to serde's pattern matching behavior where it prioritizes // the first matching pattern it encounters. diff --git a/shared/tunnelbroker_messages/src/messages/session.rs b/shared/tunnelbroker_messages/src/messages/session.rs --- a/shared/tunnelbroker_messages/src/messages/session.rs +++ b/shared/tunnelbroker_messages/src/messages/session.rs @@ -43,11 +43,6 @@ pub device_os: Option, } -#[derive(Serialize, Deserialize)] -pub struct ConnectionInitializationResponse { - pub session_id: String, -} - #[cfg(test)] mod session_tests { use super::*;