GRPCServerCodecHandlerTests.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. @testable import GRPC
  17. import NIO
  18. import XCTest
  19. class GRPCServerCodecHandlerTests: GRPCTestCase {
  20. struct BlowUpError: Error {}
  21. struct BlowUpSerializer: MessageSerializer {
  22. typealias Input = Any
  23. func serialize(_ input: Any, allocator: ByteBufferAllocator) throws -> ByteBuffer {
  24. throw BlowUpError()
  25. }
  26. }
  27. struct BlowUpDeserializer: MessageDeserializer {
  28. typealias Output = Any
  29. func deserialize(byteBuffer: ByteBuffer) throws -> Any {
  30. throw BlowUpError()
  31. }
  32. }
  33. func testSerializationFailure() throws {
  34. let handler = GRPCServerCodecHandler(
  35. serializer: BlowUpSerializer(),
  36. deserializer: BlowUpDeserializer()
  37. )
  38. let channel = EmbeddedChannel(handler: handler)
  39. XCTAssertThrowsError(try channel.writeInbound(_RawGRPCServerRequestPart.message(ByteBuffer())))
  40. XCTAssertNil(try channel.readInbound(as: Any.self))
  41. }
  42. func testDeserializationFailure() throws {
  43. let handler = GRPCServerCodecHandler(
  44. serializer: BlowUpSerializer(),
  45. deserializer: BlowUpDeserializer()
  46. )
  47. let channel = EmbeddedChannel(handler: handler)
  48. XCTAssertThrowsError(
  49. try channel
  50. .writeOutbound(_GRPCServerResponsePart<Any>.message(.init(ByteBuffer(), compressed: false)))
  51. )
  52. XCTAssertNil(try channel.readOutbound(as: Any.self))
  53. }
  54. }