2 # Copyright 2020 The Pigweed Authors
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 # use this file except in compliance with the License. You may obtain a copy of
8 # https://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations under
15 """Tests creating pw_rpc client."""
19 from pw_status import Status
20 from pw_rpc_protos.internal.packet_pb2 import PacketType, RpcPacket
22 from pw_rpc import packets
24 _TEST_REQUEST = RpcPacket(type=PacketType.REQUEST,
28 payload=RpcPacket(status=321).SerializeToString())
31 class PacketsTest(unittest.TestCase):
32 """Tests for packet encoding and decoding."""
33 def test_encode_request(self):
34 data = packets.encode_request((1, 2, 3), RpcPacket(status=321))
36 packet.ParseFromString(data)
38 self.assertEqual(_TEST_REQUEST, packet)
40 def test_encode_response(self):
41 response = RpcPacket(type=PacketType.RESPONSE,
45 payload=RpcPacket(status=321).SerializeToString())
47 data = packets.encode_response((1, 2, 3), RpcPacket(status=321))
49 packet.ParseFromString(data)
51 self.assertEqual(response, packet)
53 def test_encode_cancel(self):
54 data = packets.encode_cancel((9, 8, 7))
57 packet.ParseFromString(data)
61 RpcPacket(type=PacketType.CANCEL_SERVER_STREAM,
66 def test_encode_client_error(self):
67 data = packets.encode_client_error(_TEST_REQUEST, Status.NOT_FOUND)
70 packet.ParseFromString(data)
74 RpcPacket(type=PacketType.CLIENT_ERROR,
78 status=Status.NOT_FOUND.value))
80 def test_decode(self):
81 self.assertEqual(_TEST_REQUEST,
82 packets.decode(_TEST_REQUEST.SerializeToString()))
84 def test_for_server(self):
85 self.assertTrue(packets.for_server(_TEST_REQUEST))
89 RpcPacket(type=PacketType.RESPONSE,
93 payload=RpcPacket(status=321).SerializeToString())))
96 if __name__ == '__main__':