azure_iot_operations_services/state_store/
resp3.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Types and serialization/deserialization implementations for RESP3 protocol.

use std::time::Duration;

use azure_iot_operations_protocol::common::payload_serialize::{
    DeserializationError, FormatIndicator, PayloadSerialize, SerializedPayload,
};

/// Request types for the State Store service, used internally for serialization
#[derive(Clone, Debug)]
pub(crate) enum Request {
    Set {
        key: Vec<u8>,
        value: Vec<u8>,
        options: SetOptions,
    },
    Get {
        key: Vec<u8>,
    },
    Del {
        key: Vec<u8>,
    },
    VDel {
        key: Vec<u8>,
        value: Vec<u8>,
    },
    KeyNotify {
        key: Vec<u8>,
        options: KeyNotifyOptions,
    },
}

/// Options for a `Set` Request
#[derive(Clone, Debug, Default)]
pub struct SetOptions {
    /// Condition for the `Set` operation. Default is [`SetCondition::Unconditional`]
    pub set_condition: SetCondition,
    /// How long the key should persist before it expires, in millisecond precision.
    pub expires: Option<Duration>,
}

/// Condition for a `Set` Request
#[derive(Clone, Debug, Default)]
pub enum SetCondition {
    /// The `Set` operation will only execute if the State Store does not have this key already.
    OnlyIfDoesNotExist,
    /// The `Set` operation will only execute if the State Store does not have this key or it has this key and
    /// the value in the State Store is equal to the value provided for this `Set` operation.
    OnlyIfEqualOrDoesNotExist,
    /// The `Set` operation will execute regardless of if the key exists already and regardless of the value
    /// of this key in the State Store.
    #[default]
    Unconditional,
}

/// `KeyNotifyOptions` is how a client specifies various KEYNOTIFY permutations
#[derive(Clone, Debug, Default)]
pub(crate) struct KeyNotifyOptions {
    /// If there's an existing notification with the same `key` and `client_id` as this request, the state store removes it
    pub stop: bool,
}

impl PayloadSerialize for Request {
    type Error = String;

    fn serialize(self) -> Result<SerializedPayload, String> {
        Ok(SerializedPayload {
            payload: match self {
                Request::Set {
                    key,
                    value,
                    options,
                } => serialize_set(&key, &value, &options),
                Request::Get { key } => serialize_get(&key),
                Request::KeyNotify { key, options } => serialize_key_notify(&key, &options),
                Request::Del { key } => serialize_del(&key),
                Request::VDel { key, value } => serialize_v_del(&key, &value),
            },
            content_type: "application/octet-stream".to_string(),
            format_indicator: FormatIndicator::UnspecifiedBytes,
        })
    }

    fn deserialize(
        _payload: &[u8],
        _content_type: Option<&String>,
        _format_indicator: &FormatIndicator,
    ) -> Result<Self, DeserializationError<String>> {
        Err(DeserializationError::InvalidPayload(
            "Not implemented".into(),
        ))
    }
}

// ----------------------- Serialization Functions -----------------------

/// `RequestBufferBuilder` builds a RESP3 buffer for sending to the State Store.
struct RequestBufferBuilder {
    buffer: Vec<u8>,
}

impl RequestBufferBuilder {
    fn new() -> Self {
        RequestBufferBuilder { buffer: Vec::new() }
    }

    fn get_buffer(self) -> Vec<u8> {
        self.buffer
    }

    fn append_array_number(&mut self, num_elements: u32) {
        self.buffer
            .extend(format!("*{num_elements}\r\n").as_bytes());
    }

    fn append_argument(&mut self, arg: &[u8]) {
        self.buffer.extend(format!("${}\r\n", arg.len()).as_bytes());
        self.buffer.extend(arg);
        self.buffer.extend(b"\r\n");
    }
}

/// Determines number of additional arguments needed for RESP3 payload
fn get_number_additional_arguments(options: &SetOptions) -> u32 {
    let mut additional_arguments: u32 = 0;

    match options.set_condition {
        // Will add `NX` or `NEX` argument to the request
        SetCondition::OnlyIfEqualOrDoesNotExist | SetCondition::OnlyIfDoesNotExist => {
            additional_arguments += 1;
        }
        SetCondition::Unconditional => (),
    }

    // Will add `PX` and the expiration time as arguments to the request
    if options.expires.is_some() {
        additional_arguments += 2;
    }

    additional_arguments
}

/// Builds a RESP3 payload to `SET(key=value)`
/// For additional documentation on the format,
/// see <https://learn.microsoft.com/azure/iot-operations/create-edge-apps/concept-about-state-store-protocol#request-format>
fn serialize_set(key: &[u8], value: &[u8], options: &SetOptions) -> Vec<u8> {
    let mut builder = RequestBufferBuilder::new();

    // All `SET` requests have a minimum of 3 arguments: `SET`, the key, and the value
    let mut num_arguments = 3;

    // Gets number of any additional arguments needed because of the options
    num_arguments += get_number_additional_arguments(options);

    builder.append_array_number(num_arguments);
    builder.append_argument(b"SET");

    builder.append_argument(key);
    builder.append_argument(value);

    match options.set_condition {
        SetCondition::OnlyIfDoesNotExist => builder.append_argument(b"NX"),
        SetCondition::OnlyIfEqualOrDoesNotExist => builder.append_argument(b"NEX"),
        SetCondition::Unconditional => (),
    }

    if let Some(expires) = options.expires {
        builder.append_argument(b"PX");
        builder.append_argument(expires.as_millis().to_string().as_bytes());
    }

    builder.get_buffer()
}

/// Builds a RESP3 payload to `GET(key)`
fn serialize_get(key: &[u8]) -> Vec<u8> {
    let mut builder = RequestBufferBuilder::new();
    // All `GET` requests have 2 arguments: `GET` and the key
    builder.append_array_number(2);
    builder.append_argument(b"GET");
    builder.append_argument(key);
    builder.get_buffer()
}

/// Builds a RESP3 payload to `DEL(key)`
fn serialize_del(key: &[u8]) -> Vec<u8> {
    let mut builder = RequestBufferBuilder::new();
    // All `DEL` requests have 2 arguments: `DEL` and the key
    builder.append_array_number(2);
    builder.append_argument(b"DEL");
    builder.append_argument(key);
    builder.get_buffer()
}

/// Builds a RESP3 payload to `VDEL(key, value)`
fn serialize_v_del(key: &[u8], value: &[u8]) -> Vec<u8> {
    let mut builder = RequestBufferBuilder::new();
    // All `VDEL` requests have 3 arguments: `VDEL`, the key, and the value
    builder.append_array_number(3);
    builder.append_argument(b"VDEL");
    builder.append_argument(key);
    builder.append_argument(value);
    builder.get_buffer()
}

fn serialize_key_notify(key: &[u8], options: &KeyNotifyOptions) -> Vec<u8> {
    let mut num_arguments = 2;
    let mut builder = RequestBufferBuilder::new();

    if options.stop {
        num_arguments += 1;
    }

    builder.append_array_number(num_arguments);
    builder.append_argument(b"KEYNOTIFY");
    builder.append_argument(key);

    if options.stop {
        builder.append_argument(b"STOP");
    }

    builder.get_buffer()
}

// ----------------------- Response Types -----------------------

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Response {
    /// Successful `Set` response
    Ok,
    /// Successful `Get` response
    Value(Vec<u8>),
    /// Successful `Del` or `VDel` response. Specifies the number of keys deleted
    ValuesDeleted(i64),
    /// 'Set' or `VDel` not applied because of conditions provided
    NotApplied,
    /// Key not found for `Get`, `Del`, or `VDel` or parameters caused the operation to not be applied for `Set` or `VDel`
    NotFound,
    /// Description of error because of invalid request
    Error(Vec<u8>),
}

/// Documentation of response payloads can be found [here](https://learn.microsoft.com/azure/iot-operations/create-edge-apps/concept-about-state-store-protocol#response-format)
impl Response {
    const RESPONSE_OK: &'static [u8] = b"+OK\r\n";
    const RESPONSE_ERROR_PREFIX: &'static [u8] = b"-ERR ";
    const RESPONSE_SUFFIX: &'static [u8] = b"\r\n";
    const GET_RESPONSE_NOT_FOUND: &'static [u8] = b"$-1\r\n";
    const RESPONSE_NOT_APPLIED: &'static [u8] = b":-1\r\n";
    const RESPONSE_KEY_NOT_FOUND: &'static [u8] = b":0\r\n";
    const RESPONSE_LENGTH_PREFIX: &'static [u8] = b"$";
    const DELETE_RESPONSE_PREFIX: &'static [u8] = b":";

    fn parse_error(payload: &[u8]) -> Result<Vec<u8>, String> {
        if let Some(err) = payload.strip_prefix(Self::RESPONSE_ERROR_PREFIX) {
            if let Some(err_msg) = err.strip_suffix(Self::RESPONSE_SUFFIX) {
                return Ok(err_msg.to_vec());
            }
        }
        Err(format!("Invalid error response: {payload:?}"))
    }
}

impl PayloadSerialize for Response {
    type Error = String;

    fn serialize(self) -> Result<SerializedPayload, String> {
        Err("Not implemented".into())
    }

    fn deserialize(
        payload: &[u8],
        content_type: Option<&String>,
        _format_indicator: &FormatIndicator,
    ) -> Result<Self, DeserializationError<String>> {
        if let Some(content_type) = content_type {
            if content_type != "application/octet-stream" {
                return Err(DeserializationError::UnsupportedContentType(format!(
                    "Invalid content type: '{content_type:?}'. Must be 'application/octet-stream'"
                )));
            }
        }

        match payload {
            Self::RESPONSE_OK => Ok(Response::Ok),
            Self::GET_RESPONSE_NOT_FOUND | Self::RESPONSE_KEY_NOT_FOUND => Ok(Response::NotFound),
            Self::RESPONSE_NOT_APPLIED => Ok(Response::NotApplied),
            _ if payload.starts_with(Self::RESPONSE_ERROR_PREFIX) => {
                Ok(Response::Error(Self::parse_error(payload)?))
            }
            _ if payload.starts_with(Self::RESPONSE_LENGTH_PREFIX) => Ok(Response::Value(
                parse_value(payload, Self::RESPONSE_LENGTH_PREFIX)?,
            )),
            _ if payload.starts_with(Self::DELETE_RESPONSE_PREFIX) => {
                match parse_numeric(payload, Self::DELETE_RESPONSE_PREFIX)?.try_into() {
                    Ok(n) => Ok(Response::ValuesDeleted(n)),
                    Err(e) => Err(DeserializationError::InvalidPayload(format!(
                        "Error parsing number of keys deleted: {e}. Payload: {payload:?}"
                    ))),
                }
            }
            _ => Err(DeserializationError::InvalidPayload(format!(
                "Unknown response: {payload:?}"
            ))),
        }
    }
}

/// Provides detail about the state change that occurred on a key
#[derive(Clone, Debug, PartialEq)]
pub enum Operation {
    /// Operation was a `SET`, and the argument is the new value
    Set(Vec<u8>),
    /// Operation was a `DELETE`
    Del,
}

impl Operation {
    /// All delete notifications have identical bodies.
    const OPERATION_DELETE: &'static [u8] = b"*2\r\n$6\r\nNOTIFY\r\n$6\r\nDELETE\r\n";
    /// All set notifications start with an identical prefix.
    const SET_WITH_VALUE_PREFIX: &'static [u8] =
        b"*4\r\n$6\r\nNOTIFY\r\n$3\r\nSET\r\n$5\r\nVALUE\r\n$";
}

impl PayloadSerialize for Operation {
    type Error = String;

    fn serialize(self) -> Result<SerializedPayload, String> {
        Err("Not implemented".into())
    }

    fn deserialize(
        payload: &[u8],
        content_type: Option<&String>,
        _format_indicator: &FormatIndicator,
    ) -> Result<Self, DeserializationError<String>> {
        if let Some(content_type) = content_type {
            if content_type != "application/octet-stream" {
                return Err(DeserializationError::UnsupportedContentType(format!(
                    "Invalid content type: '{content_type:?}'. Must be 'application/octet-stream'"
                )));
            }
        }
        match payload {
            Operation::OPERATION_DELETE => Ok(Operation::Del),
            _ if payload.starts_with(Operation::SET_WITH_VALUE_PREFIX) => Ok(Operation::Set(
                parse_value(payload, Operation::SET_WITH_VALUE_PREFIX)?,
            )),
            _ => Err(DeserializationError::InvalidPayload(format!(
                "Unknown operation: {payload:?}"
            ))),
        }
    }
}

// ----------------------- DESERIALIZE FUNCTIONS -----------------------
const RESPONSE_SUFFIX: &[u8] = b"\r\n";

/// Given a payload, parse the numeric value that follows the prefix.
/// Ex: for a payload of ":1234\r\n", the prefix would be b":" and the numeric value returned would be 1234.
fn parse_numeric(payload: &[u8], prefix: &[u8]) -> Result<usize, String> {
    if let Some(val) = payload.strip_prefix(prefix) {
        let (num_deleted, current_index) = get_numeric(val)?;
        // after the number of deleted keys, there should be '\r\n' and then nothing else. '\r' is already verified in get_numeric
        if current_index + 2 == val.len() && val[current_index + 1] == b'\n' {
            return Ok(num_deleted);
        }
    }
    Err(format!("Invalid numeric response: {payload:?}"))
}

/// Given a `&[u8]`, parse the numeric value at the beginning until `\r` and return the length.
/// Ex: for a payload of "26\r\nABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n", this would return (26, 2).
fn get_numeric(payload: &[u8]) -> Result<(usize, usize), String> {
    let mut value_len: usize = 0;
    let mut current_index: usize = 0;
    for byte in &payload[0..] {
        match byte {
            b'\r' => {
                break;
            }

            b'0'..=b'9' => {
                let value = usize::from(byte - b'0');

                match value_len.checked_mul(10) {
                    Some(v) => value_len = v,
                    None => {
                        return Err(format!(
                            "Multiplication overflow while parsing value length: {payload:?}"
                        ));
                    }
                }
                match value_len.checked_add(value) {
                    Some(v) => value_len = v,
                    None => {
                        return Err(format!(
                            "Addition overflow while parsing value length: {payload:?}"
                        ));
                    }
                }
            }

            _ => {
                return Err(format!("Invalid value length format: {payload:?}"));
            }
        }
        current_index += 1;
    }
    Ok((value_len, current_index))
}

/// For a response or notification that contains a value (embedded in extra
/// protocol overhead), return just the value itself.
/// E.G. for the string "$5\r\nABCDE\r\n", this will return
/// b"ABCDE".
/// Inputs to this should be the entire payload (for error purposes) and any prefix that is before the length of the value.
/// So a payload of "$5\r\nABCDE\r\n" should pass in b"$" as the prefix, and a payload
/// of "*4\r\n$6\r\nNOTIFY\r\n$3\r\nSET\r\n$5\r\nVALUE\r\n$3\r\nabc\r\n" should pass in b"*4\r\n$6\r\nNOTIFY\r\n$3\r\nSET\r\n$5\r\nVALUE\r\n$" as the prefix.
fn parse_value(payload: &[u8], prefix: &[u8]) -> Result<Vec<u8>, String> {
    if let Some(stripped_payload) = payload.strip_prefix(prefix) {
        // get length of value
        let (value_len, mut current_index) = get_numeric(stripped_payload)?;
        current_index += 1; // '\r' that triggered get_numeric to return
        // '\n' should be next
        if current_index == stripped_payload.len() || stripped_payload[current_index] != b'\n' {
            return Err(format!("Invalid format: {payload:?}"));
        }
        current_index += 1;
        if current_index + value_len + 2 != stripped_payload.len() {
            return Err(format!(
                "Value length does not match actual value length: {payload:?}"
            ));
        }

        let closing_bytes =
            &stripped_payload[(stripped_payload.len() - 2)..(stripped_payload.len())];
        if closing_bytes != RESPONSE_SUFFIX {
            return Err(format!("Invalid format: {payload:?}"));
        }

        Ok(stripped_payload[current_index..current_index + value_len].to_vec())
    } else {
        Err(format!(
            "Invalid payload, must start with {prefix:?}: {payload:?}"
        ))
    }
}

#[cfg(test)]
mod tests {
    use test_case::test_case;

    use super::*;

    // ------------- Deserialize Response Tests -------------

    #[test_case(b"+OK\r\n", &Response::Ok; "test_set_response")]
    #[test_case(b":-1\r\n", &Response::NotApplied; "test_did_not_set_response")]
    #[test_case(b"$4\r\n1234\r\n", &Response::Value(b"1234".to_vec()); "test_get_response_success")]
    #[test_case(b"$0\r\n\r\n", &Response::Value(b"".to_vec()); "test_get_response_empty_success")]
    #[test_case(b"$-1\r\n", &Response::NotFound; "test_get_response_no_key")]
    #[test_case(b":1\r\n", &Response::ValuesDeleted(1); "test_del_response")] // Same as vdel response
    #[test_case(b":-1\r\n", &Response::NotApplied; "test_vdel_no_match_response")]
    #[test_case(b":6\r\n", &Response::ValuesDeleted(6); "test_del_multiple_response")] // this isn't currently possible, but could be in the future. same as a vdel response
    #[test_case(b":0\r\n", &Response::NotFound; "test_del_no_key")] // same as a vdel response
    #[test_case(b"-ERR syntax error\r\n", &Response::Error(b"syntax error".to_vec()); "test_error_response")]
    #[test_case(b"-ERR \r\n", &Response::Error(b"".to_vec()); "test_empty_error_response_success")]

    fn test_response_deserialization_success(payload: &[u8], expected: &Response) {
        assert_eq!(
            Response::deserialize(
                payload,
                Some(&"application/octet-stream".to_string()),
                &FormatIndicator::UnspecifiedBytes
            )
            .unwrap(),
            expected.clone()
        );
    }

    #[test]
    fn test_response_deserialization_no_content_type_success() {
        assert_eq!(
            Response::deserialize(b"+OK\r\n", None, &FormatIndicator::UnspecifiedBytes).unwrap(),
            Response::Ok
        );
    }

    #[test_case(b"1"; "too short")]
    #[test_case(b"11\r\nhello world\r\n"; "no $ on get response")]
    #[test_case(b"$11hello world\r\n"; "missing first newline")]
    #[test_case(b"$11\r\nhello world"; "missing second newline")]
    #[test_case(b"$not an integer\r\nhello world"; "length not an integer")]
    #[test_case(b"$11\r\nthis string is longer than 11 characters\r\n"; "length not accurate")]
    #[test_case(b"-ERR\r\n"; "Malformed error")]
    #[test_case(b"ERR description\r\n"; "Error missing minus")]
    #[test_case(b"-ERR description"; "Error missing newline")]
    #[test_case(b":"; "Delete response too short")]
    #[test_case(b"1234\r\n"; "Delete response doesn't start with colon")]
    #[test_case(b":1234"; "Delete response doesn't end with newline")]
    #[test_case(b":not an integer\r\n"; "Delete response value not integer")]
    #[test_case(b"+hello world\r\n"; "Incorrect OK value")]
    #[test_case(b"+"; "OK response too short")]
    #[test_case(b"OK\r\n"; "OK response doesn't start with plus sign")]
    #[test_case(b"+OK"; "OK response doesn't end with newline")]

    fn test_response_deserialization_failures(payload: &[u8]) {
        assert!(
            Response::deserialize(
                payload,
                Some(&"application/octet-stream".to_string()),
                &FormatIndicator::UnspecifiedBytes
            )
            .is_err()
        );
    }

    #[test]
    fn test_response_deserialization_content_type_failure() {
        assert!(
            Response::deserialize(
                b"+OK\r\n",
                Some(&"application/json".to_string()),
                &FormatIndicator::UnspecifiedBytes
            )
            .is_err()
        );
    }

    // --------------- Internal Fns tests ---------------------
    #[test]
    fn test_parse_number() {
        assert_eq!(
            parse_numeric(b":1234\r\n", Response::DELETE_RESPONSE_PREFIX).unwrap(),
            1234
        );
    }

    // ---------------- Serialize Request tests -----------------------
    #[test_case(SetOptions::default(),
        b"*3\r\n$3\r\nSET\r\n$7\r\ntestkey\r\n$9\r\ntestvalue\r\n";
        "default")]
    #[test_case(SetOptions {set_condition: SetCondition::OnlyIfDoesNotExist, ..Default::default()},
        b"*4\r\n$3\r\nSET\r\n$7\r\ntestkey\r\n$9\r\ntestvalue\r\n$2\r\nNX\r\n";
        "OnlyIfDoesNotExist")]
    #[test_case(SetOptions {set_condition: SetCondition::OnlyIfEqualOrDoesNotExist, ..Default::default()},
        b"*4\r\n$3\r\nSET\r\n$7\r\ntestkey\r\n$9\r\ntestvalue\r\n$3\r\nNEX\r\n";
        "OnlyIfEqualOrDoesNotExist")]
    #[test_case(SetOptions {expires: Some(Duration::from_millis(10)), ..Default::default()},
        b"*5\r\n$3\r\nSET\r\n$7\r\ntestkey\r\n$9\r\ntestvalue\r\n$2\r\nPX\r\n$2\r\n10\r\n";
        "expires set")]
    fn test_serialize_set_options(set_options: SetOptions, expected: &[u8]) {
        assert_eq!(
            Request::serialize(Request::Set {
                key: b"testkey".to_vec(),
                value: b"testvalue".to_vec(),
                options: set_options
            })
            .unwrap(),
            SerializedPayload {
                payload: expected.to_vec(),
                content_type: "application/octet-stream".to_string(),
                format_indicator: FormatIndicator::UnspecifiedBytes,
            }
        );
    }

    #[test]
    fn test_serialize_empty_set() {
        assert_eq!(
            Request::serialize(Request::Set {
                key: b"".to_vec(),
                value: b"".to_vec(),
                options: SetOptions::default()
            })
            .unwrap(),
            SerializedPayload {
                payload: b"*3\r\n$3\r\nSET\r\n$0\r\n\r\n$0\r\n\r\n".to_vec(),
                content_type: "application/octet-stream".to_string(),
                format_indicator: FormatIndicator::UnspecifiedBytes,
            }
        );
    }

    #[test]
    fn test_serialize_get() {
        assert_eq!(
            Request::serialize(Request::Get {
                key: b"testkey".to_vec()
            })
            .unwrap(),
            SerializedPayload {
                payload: b"*2\r\n$3\r\nGET\r\n$7\r\ntestkey\r\n".to_vec(),
                content_type: "application/octet-stream".to_string(),
                format_indicator: FormatIndicator::UnspecifiedBytes,
            }
        );
    }

    #[test]
    fn test_serialize_del() {
        assert_eq!(
            Request::serialize(Request::Del {
                key: b"testkey".to_vec()
            })
            .unwrap(),
            SerializedPayload {
                payload: b"*2\r\n$3\r\nDEL\r\n$7\r\ntestkey\r\n".to_vec(),
                content_type: "application/octet-stream".to_string(),
                format_indicator: FormatIndicator::UnspecifiedBytes,
            }
        );
    }

    #[test]
    fn test_serialize_vdel() {
        assert_eq!(
            Request::serialize(Request::VDel {
                key: b"testkey".to_vec(),
                value: b"testvalue".to_vec()
            })
            .unwrap(),
            SerializedPayload {
                payload: b"*3\r\n$4\r\nVDEL\r\n$7\r\ntestkey\r\n$9\r\ntestvalue\r\n".to_vec(),
                content_type: "application/octet-stream".to_string(),
                format_indicator: FormatIndicator::UnspecifiedBytes,
            }
        );
    }
}