GRPCClientChannelHandlerTests.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2021, 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 NIOHPACK
  19. import NIOHTTP2
  20. import XCTest
  21. class GRPCClientChannelHandlerTests: GRPCTestCase {
  22. private func makeRequestHead() -> _GRPCRequestHead {
  23. return _GRPCRequestHead(
  24. method: "POST",
  25. scheme: "https",
  26. path: "/foo/bar",
  27. host: "localhost",
  28. deadline: .distantFuture,
  29. customMetadata: [:],
  30. encoding: .disabled
  31. )
  32. }
  33. func doTestDataFrameWithEndStream(dataContainsMessage: Bool) throws {
  34. let handler = GRPCClientChannelHandler(
  35. callType: .unary,
  36. logger: GRPCLogger(wrapping: self.clientLogger)
  37. )
  38. let channel = EmbeddedChannel(handler: handler)
  39. // Write request head.
  40. let head = self.makeRequestHead()
  41. XCTAssertNoThrow(try channel.writeOutbound(_RawGRPCClientRequestPart.head(head)))
  42. // Read out a frame payload.
  43. XCTAssertNotNil(try channel.readOutbound(as: HTTP2Frame.FramePayload.self))
  44. // Respond with headers.
  45. let headers: HPACKHeaders = [":status": "200", "content-type": "application/grpc"]
  46. let headersPayload = HTTP2Frame.FramePayload.headers(.init(headers: headers))
  47. XCTAssertNoThrow(try channel.writeInbound(headersPayload))
  48. // Read them out the other side.
  49. XCTAssertNotNil(try channel.readInbound(as: _RawGRPCClientResponsePart.self))
  50. // Respond with DATA and end stream.
  51. var buffer = ByteBuffer()
  52. // Write a message, if we need to.
  53. if dataContainsMessage {
  54. buffer.writeInteger(UInt8(0)) // not compressed
  55. buffer.writeInteger(UInt32(42)) // message length
  56. buffer.writeRepeatingByte(0, count: 42) // message
  57. }
  58. let dataPayload = HTTP2Frame.FramePayload.Data(data: .byteBuffer(buffer), endStream: true)
  59. XCTAssertNoThrow(try channel.writeInbound(HTTP2Frame.FramePayload.data(dataPayload)))
  60. if dataContainsMessage {
  61. // Read the message out the other side.
  62. XCTAssertNotNil(try channel.readInbound(as: _RawGRPCClientResponsePart.self))
  63. }
  64. // We should also generate a status since end stream was set.
  65. if let part = try channel.readInbound(as: _RawGRPCClientResponsePart.self) {
  66. switch part {
  67. case .initialMetadata, .message, .trailingMetadata:
  68. XCTFail("Unexpected response part")
  69. case .status:
  70. () // Expected
  71. }
  72. } else {
  73. XCTFail("Expected to read another response part")
  74. }
  75. }
  76. func testDataFrameWithEndStream() throws {
  77. try self.doTestDataFrameWithEndStream(dataContainsMessage: true)
  78. }
  79. func testEmptyDataFrameWithEndStream() throws {
  80. try self.doTestDataFrameWithEndStream(dataContainsMessage: false)
  81. }
  82. }