Skip to main content

rocketmq_remoting/protocol/header/
lite_subscription_ctl_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 rocketmq_macros::RequestHeaderCodecV2;
16use serde::Deserialize;
17use serde::Serialize;
18
19use crate::rpc::rpc_request_header::RpcRequestHeader;
20
21#[derive(Clone, Debug, Serialize, Deserialize, Default, RequestHeaderCodecV2)]
22#[serde(rename_all = "camelCase")]
23pub struct LiteSubscriptionCtlRequestHeader {
24    #[serde(flatten)]
25    pub rpc_request_header: Option<RpcRequestHeader>,
26}
27
28#[cfg(test)]
29mod tests {
30    use cheetah_string::CheetahString;
31
32    use super::*;
33    use crate::protocol::command_custom_header::CommandCustomHeader;
34    use crate::protocol::command_custom_header::FromMap;
35
36    #[test]
37    fn lite_subscription_ctl_request_header_maps_rpc_fields_like_java_header() {
38        let header = LiteSubscriptionCtlRequestHeader {
39            rpc_request_header: Some(RpcRequestHeader {
40                broker_name: Some(CheetahString::from_static_str("broker-a")),
41                namespace: Some(CheetahString::from_static_str("ns-a")),
42                namespaced: Some(false),
43                oneway: Some(true),
44            }),
45        };
46
47        let map = header.to_map().expect("header should encode");
48        assert_eq!(map.get("brokerName").map(|value| value.as_str()), Some("broker-a"));
49        assert_eq!(map.get("namespace").map(|value| value.as_str()), Some("ns-a"));
50        assert_eq!(map.get("namespaced").map(|value| value.as_str()), Some("false"));
51        assert_eq!(map.get("oneway").map(|value| value.as_str()), Some("true"));
52
53        let decoded = <LiteSubscriptionCtlRequestHeader as FromMap>::from(&map).expect("header should decode");
54        let rpc = decoded
55            .rpc_request_header
56            .expect("flattened rpc request header should decode");
57        assert_eq!(rpc.broker_name.as_deref(), Some("broker-a"));
58        assert_eq!(rpc.namespace.as_deref(), Some("ns-a"));
59        assert_eq!(rpc.namespaced, Some(false));
60        assert_eq!(rpc.oneway, Some(true));
61    }
62}