Skip to main content

rocketmq_remoting/protocol/header/
client_request_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 cheetah_string::CheetahString;
16use rocketmq_macros::RequestHeaderCodecV2;
17use serde::Deserialize;
18use serde::Serialize;
19
20use crate::rpc::topic_request_header::TopicRequestHeader;
21
22#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
23pub struct GetRouteInfoRequestHeader {
24    #[required]
25    pub topic: CheetahString,
26
27    #[serde(rename = "acceptStandardJsonOnly")]
28    pub accept_standard_json_only: Option<bool>,
29
30    #[serde(flatten)]
31    pub topic_request_header: Option<TopicRequestHeader>,
32}
33
34impl GetRouteInfoRequestHeader {
35    pub fn new(topic: impl Into<CheetahString>, accept_standard_json_only: Option<bool>) -> Self {
36        GetRouteInfoRequestHeader {
37            topic: topic.into(),
38            accept_standard_json_only,
39            ..Default::default()
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use cheetah_string::CheetahString;
47
48    use super::*;
49
50    #[test]
51    fn get_route_info_request_header_with_required_topic() {
52        let header = crate::protocol::header::client_request_header::GetRouteInfoRequestHeader {
53            topic: CheetahString::from("testTopic"),
54            accept_standard_json_only: Some(true),
55            topic_request_header: None,
56        };
57        assert_eq!(header.topic, CheetahString::from("testTopic"));
58        assert_eq!(header.accept_standard_json_only, Some(true));
59        assert!(header.topic_request_header.is_none());
60    }
61
62    #[test]
63    fn get_route_info_request_header_with_optional_fields() {
64        let header = crate::protocol::header::client_request_header::GetRouteInfoRequestHeader {
65            topic: CheetahString::from("testTopic"),
66            accept_standard_json_only: None,
67            topic_request_header: Some(TopicRequestHeader::default()),
68        };
69        assert_eq!(header.topic, CheetahString::from("testTopic"));
70        assert!(header.accept_standard_json_only.is_none());
71        assert!(header.topic_request_header.is_some());
72    }
73
74    #[test]
75    fn get_route_info_request_header_with_empty_topic() {
76        let header = crate::protocol::header::client_request_header::GetRouteInfoRequestHeader {
77            topic: CheetahString::from(""),
78            accept_standard_json_only: Some(false),
79            topic_request_header: None,
80        };
81        assert_eq!(header.topic, CheetahString::from(""));
82        assert_eq!(header.accept_standard_json_only, Some(false));
83        assert!(header.topic_request_header.is_none());
84    }
85
86    #[test]
87    fn get_route_info_request_header_with_long_topic() {
88        let long_topic = "a".repeat(1000);
89        let header = crate::protocol::header::client_request_header::GetRouteInfoRequestHeader {
90            topic: CheetahString::from(&long_topic),
91            accept_standard_json_only: Some(true),
92            topic_request_header: None,
93        };
94        assert_eq!(header.topic, CheetahString::from(&long_topic));
95        assert_eq!(header.accept_standard_json_only, Some(true));
96        assert!(header.topic_request_header.is_none());
97    }
98}