|
| 1 | +use super::super::parse::Parse; |
| 2 | +use crate::application::server::connection::WriteConnection; |
| 3 | +use crate::application::server::context::Context; |
| 4 | +use crate::application::server::frame::Frame; |
| 5 | + |
| 6 | +/// The CLIENT GETNAME returns the name of the current connection as set by |
| 7 | +/// CLIENT SETNAME. Since every new connection starts without an associated |
| 8 | +/// name, if no name was assigned a null bulk reply is returned. |
| 9 | +#[derive(Debug, Default)] |
| 10 | +pub struct ClientGetName {} |
| 11 | + |
| 12 | +impl ClientGetName { |
| 13 | + pub fn new() -> ClientGetName { |
| 14 | + ClientGetName {} |
| 15 | + } |
| 16 | + |
| 17 | + pub(crate) fn parse_frames( |
| 18 | + parse: &mut Parse, |
| 19 | + ) -> anyhow::Result<ClientGetName> { |
| 20 | + parse.finish()?; |
| 21 | + Ok(ClientGetName::new()) |
| 22 | + } |
| 23 | + |
| 24 | + pub(crate) async fn apply( |
| 25 | + self, |
| 26 | + dst: &mut WriteConnection, |
| 27 | + ctx: Context, |
| 28 | + ) -> anyhow::Result<()> { |
| 29 | + let name = ctx.connection.name().await; |
| 30 | + |
| 31 | + let response = match name { |
| 32 | + Some(name) => Frame::Bulk(name.into_bytes()), |
| 33 | + None => Frame::Null, |
| 34 | + }; |
| 35 | + dst.write_frame(&response).await?; |
| 36 | + |
| 37 | + Ok(()) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(test)] |
| 42 | +mod tests { |
| 43 | + use std::io::Cursor; |
| 44 | + |
| 45 | + use bytes::BytesMut; |
| 46 | + use redis_async::resp::{RespCodec, RespValue}; |
| 47 | + use redis_async::resp_array; |
| 48 | + use tokio_util::codec::Encoder; |
| 49 | + |
| 50 | + use crate::application::server::cmd::Command; |
| 51 | + use crate::application::server::frame::Frame; |
| 52 | + |
| 53 | + fn parse_cmd(obj: RespValue) -> anyhow::Result<Command> { |
| 54 | + let mut bytes = BytesMut::new(); |
| 55 | + let mut codec = RespCodec; |
| 56 | + codec.encode(obj, &mut bytes).unwrap(); |
| 57 | + |
| 58 | + let mut bytes = Cursor::new(bytes.freeze()); |
| 59 | + let frame = Frame::parse(&mut bytes)?; |
| 60 | + let client_list = Command::from_frame(frame)?; |
| 61 | + Ok(client_list) |
| 62 | + } |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn ensure_parsing() { |
| 66 | + let entry: RespValue = resp_array!["CLIENT", "GETNAME"]; |
| 67 | + let client_cmd = parse_cmd(entry).unwrap(); |
| 68 | + insta::assert_debug_snapshot!(client_cmd, @r###" |
| 69 | + Client( |
| 70 | + GetName( |
| 71 | + ClientGetName, |
| 72 | + ), |
| 73 | + ) |
| 74 | + "###); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn ensure_parsing_too_much() { |
| 79 | + let entry: RespValue = resp_array!["CLIENT", "GETNAME", "BLBL"]; |
| 80 | + let client_cmd = parse_cmd(entry); |
| 81 | + assert!(client_cmd.is_err()); |
| 82 | + let client_cmd = client_cmd.unwrap_err(); |
| 83 | + insta::assert_debug_snapshot!(client_cmd, @r###" |
| 84 | + Other( |
| 85 | + "protocol error; expected end of frame, but there was more", |
| 86 | + ) |
| 87 | + "###); |
| 88 | + } |
| 89 | +} |
0 commit comments