Browse Source

Use JSON (de)serializers instead of custom ones for tests (#2162)

In some tests, we were using a custom serializer/deserializer to deal
with the `PeerInfo` type. We can just reuse the JSON (de)serialiser we
have. Had to copy it over as it was in a different target.
Gus Cairo 1 year ago
parent
commit
a1dbd1506f

+ 1 - 0
Tests/GRPCCoreTests/Test Utilities/Coding+JSON.swift

@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * limitations under the License.
  */
  */
+
 import GRPCCore
 import GRPCCore
 
 
 import struct Foundation.Data
 import struct Foundation.Data

+ 2 - 18
Tests/GRPCInProcessTransportTests/InProcessTransportTests.swift

@@ -80,7 +80,7 @@ struct InProcessTransportTests {
         request: ClientRequest(message: ()),
         request: ClientRequest(message: ()),
         descriptor: .peerInfo,
         descriptor: .peerInfo,
         serializer: VoidSerializer(),
         serializer: VoidSerializer(),
-        deserializer: PeerInfoDeserializer(),
+        deserializer: JSONDeserializer<PeerInfo>(),
         options: .defaults
         options: .defaults
       ) {
       ) {
         try $0.message
         try $0.message
@@ -142,7 +142,7 @@ private struct TestService: RegistrableRPCService {
     router.registerHandler(
     router.registerHandler(
       forMethod: .peerInfo,
       forMethod: .peerInfo,
       deserializer: VoidDeserializer(),
       deserializer: VoidDeserializer(),
-      serializer: PeerInfoSerializer(),
+      serializer: JSONSerializer<PeerInfo>(),
       handler: {
       handler: {
         let response = try await self.peerInfo(
         let response = try await self.peerInfo(
           request: ServerRequest<Void>(stream: $0),
           request: ServerRequest<Void>(stream: $0),
@@ -171,22 +171,6 @@ private struct PeerInfo: Codable {
   var remote: String
   var remote: String
 }
 }
 
 
-private struct PeerInfoSerializer: MessageSerializer {
-  func serialize<Bytes: GRPCContiguousBytes>(_ message: PeerInfo) throws -> Bytes {
-    Bytes("\(message.local) \(message.remote)".utf8)
-  }
-}
-
-private struct PeerInfoDeserializer: MessageDeserializer {
-  func deserialize<Bytes: GRPCContiguousBytes>(_ 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 {
 private struct UTF8Serializer: MessageSerializer {
   func serialize<Bytes: GRPCContiguousBytes>(_ message: String) throws -> Bytes {
   func serialize<Bytes: GRPCContiguousBytes>(_ message: String) throws -> Bytes {
     Bytes(message.utf8)
     Bytes(message.utf8)

+ 45 - 0
Tests/GRPCInProcessTransportTests/Test Utilities/JSONSerializing.swift

@@ -0,0 +1,45 @@
+/*
+ * 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 GRPCCore
+
+import struct Foundation.Data
+import class Foundation.JSONDecoder
+import class Foundation.JSONEncoder
+
+struct JSONSerializer<Message: Codable>: MessageSerializer {
+  func serialize<Bytes: GRPCContiguousBytes>(_ message: Message) throws -> Bytes {
+    do {
+      let jsonEncoder = JSONEncoder()
+      let data = try jsonEncoder.encode(message)
+      return Bytes(data)
+    } catch {
+      throw RPCError(code: .internalError, message: "Can't serialize message to JSON. \(error)")
+    }
+  }
+}
+
+struct JSONDeserializer<Message: Codable>: MessageDeserializer {
+  func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> Message {
+    do {
+      let jsonDecoder = JSONDecoder()
+      let data = serializedMessageBytes.withUnsafeBytes { Data($0) }
+      return try jsonDecoder.decode(Message.self, from: data)
+    } catch {
+      throw RPCError(code: .internalError, message: "Can't deserialze message from JSON. \(error)")
+    }
+  }
+}