GRPCChannelHandlerTests.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. class GRPCChannelHandlerTests: GRPCChannelHandlerResponseCapturingTestCase {
  22. func testUnimplementedMethodReturnsUnimplementedStatus() throws {
  23. let responses = try waitForGRPCChannelHandlerResponses(count: 1) { channel in
  24. let requestHead = HTTPRequestHead(version: .init(major: 2, minor: 0), method: .POST, uri: "unimplementedMethodName")
  25. try channel.writeInbound(RawGRPCServerRequestPart.head(requestHead))
  26. }
  27. let expectedError = GRPCServerError.unimplementedMethod("unimplementedMethodName")
  28. XCTAssertEqual([expectedError], errorCollector.asGRPCServerErrors)
  29. responses[0].assertStatus { status in
  30. assertEqualStatusIgnoringTrailers(status, expectedError.asGRPCStatus())
  31. }
  32. }
  33. func testImplementedMethodReturnsHeadersMessageAndStatus() throws {
  34. let responses = try waitForGRPCChannelHandlerResponses(count: 3) { channel in
  35. let requestHead = HTTPRequestHead(version: .init(major: 2, minor: 0), method: .POST, uri: "/echo.Echo/Get")
  36. try channel.writeInbound(RawGRPCServerRequestPart.head(requestHead))
  37. let request = Echo_EchoRequest.with { $0.text = "echo!" }
  38. let requestData = try request.serializedData()
  39. var buffer = channel.allocator.buffer(capacity: requestData.count)
  40. buffer.writeBytes(requestData)
  41. try channel.writeInbound(RawGRPCServerRequestPart.message(buffer))
  42. }
  43. responses[0].assertHeaders()
  44. responses[1].assertMessage()
  45. responses[2].assertStatus { status in
  46. XCTAssertEqual(status.code, .ok)
  47. }
  48. }
  49. func testImplementedMethodReturnsStatusForBadlyFormedProto() throws {
  50. let responses = try waitForGRPCChannelHandlerResponses(count: 2) { channel in
  51. let requestHead = HTTPRequestHead(version: .init(major: 2, minor: 0), method: .POST, uri: "/echo.Echo/Get")
  52. try channel.writeInbound(RawGRPCServerRequestPart.head(requestHead))
  53. var buffer = channel.allocator.buffer(capacity: 3)
  54. buffer.writeBytes([1, 2, 3])
  55. try channel.writeInbound(RawGRPCServerRequestPart.message(buffer))
  56. }
  57. let expectedError = GRPCServerError.requestProtoDeserializationFailure
  58. XCTAssertEqual([expectedError], errorCollector.asGRPCServerErrors)
  59. responses[0].assertHeaders()
  60. responses[1].assertStatus { status in
  61. assertEqualStatusIgnoringTrailers(status, expectedError.asGRPCStatus())
  62. }
  63. }
  64. }