From 17d01a5f09491fe549b377f14207afc6448307f7 Mon Sep 17 00:00:00 2001 From: Gus Cairo Date: Fri, 17 Jan 2025 10:58:18 +0000 Subject: [PATCH] Use JSON (de)serializers instead of custom ones for tests --- .../InProcessTransportTests.swift | 20 ++--------- .../Test Utilities/JSONSerializing.swift | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 Tests/GRPCInProcessTransportTests/Test Utilities/JSONSerializing.swift diff --git a/Tests/GRPCInProcessTransportTests/InProcessTransportTests.swift b/Tests/GRPCInProcessTransportTests/InProcessTransportTests.swift index 1de0a12bd..5751d74a1 100644 --- a/Tests/GRPCInProcessTransportTests/InProcessTransportTests.swift +++ b/Tests/GRPCInProcessTransportTests/InProcessTransportTests.swift @@ -80,7 +80,7 @@ struct InProcessTransportTests { request: ClientRequest(message: ()), descriptor: .peerInfo, serializer: VoidSerializer(), - deserializer: PeerInfoDeserializer(), + deserializer: JSONDeserializer(), options: .defaults ) { try $0.message @@ -142,7 +142,7 @@ private struct TestService: RegistrableRPCService { router.registerHandler( forMethod: .peerInfo, deserializer: VoidDeserializer(), - serializer: PeerInfoSerializer(), + serializer: JSONSerializer(), handler: { let response = try await self.peerInfo( request: ServerRequest(stream: $0), @@ -171,22 +171,6 @@ private struct PeerInfo: Codable { var remote: String } -private struct PeerInfoSerializer: MessageSerializer { - func serialize(_ message: PeerInfo) throws -> Bytes { - Bytes("\(message.local) \(message.remote)".utf8) - } -} - -private struct PeerInfoDeserializer: MessageDeserializer { - func deserialize(_ serializedMessageBytes: Bytes) throws -> PeerInfo { - let stringPeerInfo = serializedMessageBytes.withUnsafeBytes { - String(decoding: $0, as: UTF8.self) - } - let peerInfoComponents = stringPeerInfo.split(separator: " ") - return PeerInfo(local: String(peerInfoComponents[0]), remote: String(peerInfoComponents[1])) - } -} - private struct UTF8Serializer: MessageSerializer { func serialize(_ message: String) throws -> Bytes { Bytes(message.utf8) diff --git a/Tests/GRPCInProcessTransportTests/Test Utilities/JSONSerializing.swift b/Tests/GRPCInProcessTransportTests/Test Utilities/JSONSerializing.swift new file mode 100644 index 000000000..da44de468 --- /dev/null +++ b/Tests/GRPCInProcessTransportTests/Test Utilities/JSONSerializing.swift @@ -0,0 +1,35 @@ +/* + * Copyright 2025, gRPC Authors All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Foundation +import GRPCCore + +struct JSONSerializer: MessageSerializer { + private let encoder = JSONEncoder() + + func serialize(_ message: Message) throws -> [UInt8] { + let data = try self.encoder.encode(message) + return Array(data) + } +} + +struct JSONDeserializer: MessageDeserializer { + private let decoder = JSONDecoder() + + func deserialize(_ serializedMessageBytes: [UInt8]) throws -> Message { + try self.decoder.decode(Message.self, from: Data(serializedMessageBytes)) + } +}