Skip to main content

rocketmq_remoting/protocol/header/
notification_response_header.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use rocketmq_macros::RequestHeaderCodecV2;
16use serde::Deserialize;
17use serde::Serialize;
18
19#[derive(Debug, Clone, Deserialize, Serialize, Default, RequestHeaderCodecV2)]
20#[serde(rename_all = "camelCase")]
21pub struct NotificationResponseHeader {
22    #[required]
23    pub has_msg: bool,
24
25    #[serde(default)]
26    pub polling_full: bool,
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn notification_response_header_initializes_java_fields() {
35        let header = NotificationResponseHeader {
36            has_msg: true,
37            polling_full: true,
38        };
39
40        assert!(header.has_msg);
41        assert!(header.polling_full);
42    }
43
44    #[test]
45    fn notification_response_header_serializes_java_camel_case_fields() {
46        let header = NotificationResponseHeader {
47            has_msg: true,
48            polling_full: true,
49        };
50
51        let serialized = serde_json::to_string(&header).unwrap();
52
53        assert_eq!(serialized, r#"{"hasMsg":true,"pollingFull":true}"#);
54        assert!(!serialized.contains("has_msg"));
55        assert!(!serialized.contains("polling_full"));
56    }
57
58    #[test]
59    fn notification_response_header_deserializes_java_camel_case_fields() {
60        let json = r#"{"hasMsg":true,"pollingFull":true}"#;
61
62        let header: NotificationResponseHeader = serde_json::from_str(json).unwrap();
63
64        assert!(header.has_msg);
65        assert!(header.polling_full);
66    }
67
68    #[test]
69    fn notification_response_header_defaults_missing_polling_full_like_java() {
70        let json = r#"{"hasMsg":true}"#;
71
72        let header: NotificationResponseHeader = serde_json::from_str(json).unwrap();
73
74        assert!(header.has_msg);
75        assert!(!header.polling_full);
76    }
77
78    #[test]
79    fn notification_response_header_default_is_empty() {
80        let header = NotificationResponseHeader::default();
81
82        assert!(!header.has_msg);
83        assert!(!header.polling_full);
84    }
85}