HTTP1ToGRPCServerCodecTests.swift 4.8 KB

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