ln_websocket_proxy/
lib.rs

1use serde::{Deserialize, Serialize};
2
3/// MutinyProxyCommand are proxy commands that get sent to/from
4/// the clients to/from the proxy.
5///
6/// Disconnect:
7/// The proxy uses this to inform a client that one of the peers
8/// that they were connected to has gone away.
9/// The clients should use this to inform the proxy that they are
10/// asking to be disconnected to one of the peers that they are
11/// currently connected to. The proxy will send the other peer
12/// the `Disconnect` message afterwards.
13///
14/// Ping:
15/// A message for the clients to send to us to keep websocket alive.
16#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
17pub enum MutinyProxyCommand {
18    Disconnect { to: Vec<u8>, from: Vec<u8> },
19    Ping,
20}
21
22#[cfg(test)]
23mod tests {
24    use crate::MutinyProxyCommand;
25
26    #[test]
27    fn test_deserialization() {
28        assert_eq!(
29            serde_json::from_str::<MutinyProxyCommand>(
30                "{\"Disconnect\":{\"to\":[1,1],\"from\":[10,10]}}"
31            )
32            .unwrap(),
33            MutinyProxyCommand::Disconnect {
34                to: vec![1, 1],
35                from: vec![10, 10]
36            }
37        );
38        assert_eq!(
39            serde_json::from_str::<MutinyProxyCommand>("\"Ping\"").unwrap(),
40            MutinyProxyCommand::Ping {}
41        );
42    }
43
44    #[test]
45    fn test_serialization() {
46        assert_eq!(
47            "{\"Disconnect\":{\"to\":[1,1],\"from\":[10,10]}}",
48            serde_json::to_string(&MutinyProxyCommand::Disconnect {
49                to: vec![01, 01],
50                from: vec![10, 10]
51            })
52            .unwrap()
53        );
54
55        assert_eq!(
56            "\"Ping\"",
57            serde_json::to_string(&MutinyProxyCommand::Ping {}).unwrap()
58        );
59    }
60}