GRPCClientChannelHandlerTests.swift 3.2 KB

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