-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_modifier_key.rs
55 lines (44 loc) · 1.67 KB
/
test_modifier_key.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
use jbytes_derive::{ByteDecode, ByteEncode};
use jbytes::prelude::*;
// support String/&str/&[u8] type.
#[derive(Debug, PartialEq, Eq, ByteDecode, ByteEncode)]
pub struct KeyExample {
#[jbytes(key=b"Version: ", linend=b"\r\n")]
pub version: String,
#[jbytes(key=b"Host: ", linend=b"\r\n")]
pub host: String,
}
#[test]
fn test_modifier_key_example() {
// decode
let data = b"Cookie: sssss\r\nVersion: 1.0.0\r\nHeader: jkc\r\nHost: 192.168.1.1\r\nOther: jkc\r\n";
let bytes = Bytes::new(data);
let value = KeyExample { version: "1.0.0".to_string(), host: "192.168.1.1".to_string() };
assert_eq!(KeyExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining(), b"Other: jkc\r\n");
// encode
assert_eq!(*jbytes::encode(value).unwrap(), b"Version: 1.0.0\r\nHost: 192.168.1.1\r\n");
}
#[derive(Debug, PartialEq, Eq, ByteDecode, ByteEncode)]
pub enum KeyEnumExample {
#[jbytes(branch_value=1)]
Http {
#[jbytes(key=b"Version: ", linend=b"\r\n")]
version: String,
#[jbytes(key=b"Host: ", linend=b"\r\n")]
host: String,
},
#[jbytes(branch_default)]
Unknown,
}
#[test]
fn test_modifier_key_enum_example() {
// decode
let data = b"\x01Cookie: sssss\r\nVersion: 1.0.0\r\nHeader: jkc\r\nHost: 192.168.1.1\r\nOther: jkc\r\n";
let bytes = Bytes::new(data);
let value = KeyEnumExample::Http { version: "1.0.0".to_string(), host: "192.168.1.1".to_string() };
assert_eq!(KeyEnumExample::decode(&bytes).unwrap(), value);
assert_eq!(bytes.remaining(), b"Other: jkc\r\n");
// encode
assert_eq!(*jbytes::encode(value).unwrap(), b"\x01Version: 1.0.0\r\nHost: 192.168.1.1\r\n");
}