2
0

GRPCClientChannelHandlerTests.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. maximumReceiveMessageLength: .max,
  37. logger: GRPCLogger(wrapping: self.clientLogger)
  38. )
  39. let channel = EmbeddedChannel(handler: handler)
  40. // Write request head.
  41. let head = self.makeRequestHead()
  42. XCTAssertNoThrow(try channel.writeOutbound(_RawGRPCClientRequestPart.head(head)))
  43. // Read out a frame payload.
  44. XCTAssertNotNil(try channel.readOutbound(as: HTTP2Frame.FramePayload.self))
  45. // Respond with headers.
  46. let headers: HPACKHeaders = [":status": "200", "content-type": "application/grpc"]
  47. let headersPayload = HTTP2Frame.FramePayload.headers(.init(headers: headers))
  48. XCTAssertNoThrow(try channel.writeInbound(headersPayload))
  49. // Read them out the other side.
  50. XCTAssertNotNil(try channel.readInbound(as: _RawGRPCClientResponsePart.self))
  51. // Respond with DATA and end stream.
  52. var buffer = ByteBuffer()
  53. // Write a message, if we need to.
  54. if dataContainsMessage {
  55. buffer.writeInteger(UInt8(0)) // not compressed
  56. buffer.writeInteger(UInt32(42)) // message length
  57. buffer.writeRepeatingByte(0, count: 42) // message
  58. }
  59. let dataPayload = HTTP2Frame.FramePayload.Data(data: .byteBuffer(buffer), endStream: true)
  60. XCTAssertNoThrow(try channel.writeInbound(HTTP2Frame.FramePayload.data(dataPayload)))
  61. if dataContainsMessage {
  62. // Read the message out the other side.
  63. XCTAssertNotNil(try channel.readInbound(as: _RawGRPCClientResponsePart.self))
  64. }
  65. // We should also generate a status since end stream was set.
  66. if let part = try channel.readInbound(as: _RawGRPCClientResponsePart.self) {
  67. switch part {
  68. case .initialMetadata, .message, .trailingMetadata:
  69. XCTFail("Unexpected response part")
  70. case .status:
  71. () // Expected
  72. }
  73. } else {
  74. XCTFail("Expected to read another response part")
  75. }
  76. }
  77. func testDataFrameWithEndStream() throws {
  78. try self.doTestDataFrameWithEndStream(dataContainsMessage: true)
  79. }
  80. func testEmptyDataFrameWithEndStream() throws {
  81. try self.doTestDataFrameWithEndStream(dataContainsMessage: false)
  82. }
  83. }