GRPCChannelHandlerTests.swift 3.0 KB

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