HTTP1ToGRPCServerCodecTests.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2019, 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. import Foundation
  17. import XCTest
  18. import NIO
  19. import NIOHTTP1
  20. @testable import GRPC
  21. import EchoModel
  22. import EchoImplementation
  23. import Logging
  24. class HTTP1ToGRPCServerCodecTests: GRPCTestCase {
  25. var channel: EmbeddedChannel!
  26. override func setUp() {
  27. super.setUp()
  28. let logger = Logger(label: "io.grpc.testing")
  29. let handler = HTTP1ToGRPCServerCodec<Echo_EchoRequest, Echo_EchoResponse>(logger: logger)
  30. self.channel = EmbeddedChannel(handler: handler)
  31. }
  32. override func tearDown() {
  33. super.tearDown()
  34. XCTAssertNoThrow(try self.channel.finish())
  35. }
  36. func makeRequestHead() -> HTTPRequestHead {
  37. return HTTPRequestHead(
  38. version: .init(major: 2, minor: 0),
  39. method: .POST,
  40. uri: "/echo.Echo/Get"
  41. )
  42. }
  43. func testDeserializationErrorOnInvalidMessageBytes() throws {
  44. XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.makeRequestHead())))
  45. var buffer = self.channel.allocator.buffer(capacity: 0)
  46. buffer.writeInteger(UInt8(0)) // not compressed
  47. buffer.writeInteger(UInt32(3)) // message is 3 bytes
  48. buffer.writeBytes([42, 42, 42])
  49. XCTAssertThrowsError(try self.channel.writeInbound(HTTPServerRequestPart.body(buffer))) { error in
  50. let withContext = error as? GRPCError.WithContext
  51. XCTAssertTrue(withContext?.error is GRPCError.DeserializationFailure)
  52. XCTAssertEqual(withContext?.error.makeGRPCStatus().code, .internalError)
  53. }
  54. }
  55. func testSingleMessageFromMultipleBodyParts() throws {
  56. XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.makeRequestHead())))
  57. let requestPart = try self.channel.readInbound(as: _GRPCServerRequestPart<Echo_EchoRequest>.self)
  58. switch requestPart {
  59. case .some(.head):
  60. ()
  61. default:
  62. XCTFail("Unexpected request part: \(String(describing: requestPart))")
  63. }
  64. // Write a message across multiple buffers.
  65. let message = Echo_EchoRequest.with { $0.text = String(repeating: "x", count: 42) }
  66. let data = try message.serializedData()
  67. // Split the payload into two parts.
  68. let halfIndex = data.count / 2
  69. let firstChunk = data[0..<halfIndex]
  70. let secondChunk = data[halfIndex...]
  71. // Frame the message; send it in 2 parts.
  72. var firstBuffer = self.channel.allocator.buffer(capacity: firstChunk.count + 5)
  73. firstBuffer.writeInteger(UInt8(0))
  74. firstBuffer.writeInteger(UInt32(data.count))
  75. firstBuffer.writeBytes(firstChunk)
  76. XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.body(firstBuffer)))
  77. var secondBuffer = self.channel.allocator.buffer(capacity: secondChunk.count)
  78. secondBuffer.writeBytes(secondChunk)
  79. XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.body(secondBuffer)))
  80. let messagePart = try self.channel.readInbound(as: _GRPCServerRequestPart<Echo_EchoRequest>.self)
  81. switch messagePart {
  82. case .some(.message(let actual)):
  83. XCTAssertEqual(message, actual)
  84. default:
  85. XCTFail("Unexpected request part: \(String(describing: requestPart))")
  86. }
  87. }
  88. func testMultipleMessagesFromSingleBodyPart() throws {
  89. XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(self.makeRequestHead())))
  90. let requestPart = try self.channel.readInbound(as: _GRPCServerRequestPart<Echo_EchoRequest>.self)
  91. switch requestPart {
  92. case .some(.head):
  93. ()
  94. default:
  95. XCTFail("Unexpected request part: \(String(describing: requestPart))")
  96. }
  97. // Write three messages into a single body.
  98. var buffer = self.channel.allocator.buffer(capacity: 0)
  99. let messages = ["foo", "bar", "baz"].map { text in
  100. Echo_EchoRequest.with { $0.text = text }
  101. }
  102. for message in messages {
  103. let data = try message.serializedData()
  104. buffer.writeInteger(UInt8(0))
  105. buffer.writeInteger(UInt32(data.count))
  106. buffer.writeBytes(data)
  107. }
  108. XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.body(buffer)))
  109. for message in messages {
  110. let requestPart = try self.channel.readInbound(as: _GRPCServerRequestPart<Echo_EchoRequest>.self)
  111. switch requestPart {
  112. case .some(.message(let actual)):
  113. XCTAssertEqual(message, actual)
  114. default:
  115. XCTFail("Unexpected request part: \(String(describing: requestPart))")
  116. }
  117. }
  118. }
  119. }