rocketmq_remoting/protocol/header/
client_request_header.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18use cheetah_string::CheetahString;
19use rocketmq_macros::RequestHeaderCodecV2;
20use serde::Deserialize;
21use serde::Serialize;
22
23use crate::rpc::topic_request_header::TopicRequestHeader;
24
25#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
26pub struct GetRouteInfoRequestHeader {
27    #[required]
28    pub topic: CheetahString,
29
30    #[serde(rename = "acceptStandardJsonOnly")]
31    pub accept_standard_json_only: Option<bool>,
32
33    #[serde(flatten)]
34    pub topic_request_header: Option<TopicRequestHeader>,
35}
36
37impl GetRouteInfoRequestHeader {
38    pub fn new(topic: impl Into<CheetahString>, accept_standard_json_only: Option<bool>) -> Self {
39        GetRouteInfoRequestHeader {
40            topic: topic.into(),
41            accept_standard_json_only,
42            ..Default::default()
43        }
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use cheetah_string::CheetahString;
50
51    use super::*;
52
53    #[test]
54    fn get_route_info_request_header_with_required_topic() {
55        let header = crate::protocol::header::client_request_header::GetRouteInfoRequestHeader {
56            topic: CheetahString::from("testTopic"),
57            accept_standard_json_only: Some(true),
58            topic_request_header: None,
59        };
60        assert_eq!(header.topic, CheetahString::from("testTopic"));
61        assert_eq!(header.accept_standard_json_only, Some(true));
62        assert!(header.topic_request_header.is_none());
63    }
64
65    #[test]
66    fn get_route_info_request_header_with_optional_fields() {
67        let header = crate::protocol::header::client_request_header::GetRouteInfoRequestHeader {
68            topic: CheetahString::from("testTopic"),
69            accept_standard_json_only: None,
70            topic_request_header: Some(TopicRequestHeader::default()),
71        };
72        assert_eq!(header.topic, CheetahString::from("testTopic"));
73        assert!(header.accept_standard_json_only.is_none());
74        assert!(header.topic_request_header.is_some());
75    }
76
77    #[test]
78    fn get_route_info_request_header_with_empty_topic() {
79        let header = crate::protocol::header::client_request_header::GetRouteInfoRequestHeader {
80            topic: CheetahString::from(""),
81            accept_standard_json_only: Some(false),
82            topic_request_header: None,
83        };
84        assert_eq!(header.topic, CheetahString::from(""));
85        assert_eq!(header.accept_standard_json_only, Some(false));
86        assert!(header.topic_request_header.is_none());
87    }
88
89    #[test]
90    fn get_route_info_request_header_with_long_topic() {
91        let long_topic = "a".repeat(1000);
92        let header = crate::protocol::header::client_request_header::GetRouteInfoRequestHeader {
93            topic: CheetahString::from(&long_topic),
94            accept_standard_json_only: Some(true),
95            topic_request_header: None,
96        };
97        assert_eq!(header.topic, CheetahString::from(&long_topic));
98        assert_eq!(header.accept_standard_json_only, Some(true));
99        assert!(header.topic_request_header.is_none());
100    }
101}