cassandra_proto/frame/
frame_auth_response.rs

1use rand;
2
3use crate::types::CBytes;
4use crate::frame::*;
5
6#[derive(Debug)]
7pub struct BodyReqAuthResponse {
8    data: CBytes,
9}
10
11impl BodyReqAuthResponse {
12    pub fn new(data: CBytes) -> BodyReqAuthResponse {
13        BodyReqAuthResponse { data: data }
14    }
15}
16
17impl IntoBytes for BodyReqAuthResponse {
18    fn into_cbytes(&self) -> Vec<u8> {
19        self.data.into_cbytes()
20    }
21}
22
23// Frame implementation related to BodyReqStartup
24
25impl Frame {
26    /// Creates new frame of type `AuthResponse`.
27    pub fn new_req_auth_response(bytes: Vec<u8>) -> Frame {
28        let version = Version::Request;
29        let flag = Flag::Ignore;
30        let stream = rand::random::<u16>();
31        let opcode = Opcode::AuthResponse;
32        let body = BodyReqAuthResponse::new(CBytes::new(bytes));
33
34        Frame { version: version,
35                flags: vec![flag],
36                stream: stream,
37                opcode: opcode,
38                body: body.into_cbytes(),
39                // for request frames it's always None
40                tracing_id: None,
41                warnings: vec![], }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use crate::types::CBytes;
49    use crate::frame::traits::IntoBytes;
50
51    #[test]
52    fn body_req_auth_response() {
53        let bytes = CBytes::new(vec![1, 2, 3]);
54        let body = BodyReqAuthResponse::new(bytes);
55        assert_eq!(body.into_cbytes(), vec![0, 0, 0, 3, 1, 2, 3]);
56    }
57
58    #[test]
59    fn frame_body_req_auth_response() {
60        let bytes = vec![1, 2, 3];
61        let frame = Frame::new_req_auth_response(bytes);
62
63        assert_eq!(frame.version, Version::Request);
64        assert_eq!(frame.flags, vec![Flag::Ignore]);
65        assert_eq!(frame.opcode, Opcode::AuthResponse);
66        assert_eq!(frame.body, &[0, 0, 0, 3, 1, 2, 3]);
67        assert_eq!(frame.tracing_id, None);
68        assert_eq!(frame.warnings.len(), 0);
69    }
70}