ServerErrorDelegateTests.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2020, 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. import GRPC
  21. import EchoModel
  22. import EchoImplementation
  23. import Logging
  24. private class ServerErrorDelegateMock: ServerErrorDelegate {
  25. private let transformLibraryErrorHandler: ((Error) -> (GRPCStatusAndMetadata?))
  26. init(transformLibraryErrorHandler: @escaping ((Error) -> (GRPCStatusAndMetadata?))) {
  27. self.transformLibraryErrorHandler = transformLibraryErrorHandler
  28. }
  29. func transformLibraryError(_ error: Error) -> GRPCStatusAndMetadata? {
  30. return self.transformLibraryErrorHandler(error)
  31. }
  32. }
  33. class ServerErrorDelegateTests: GRPCTestCase {
  34. private var channel: EmbeddedChannel!
  35. private var errorDelegate: ServerErrorDelegate!
  36. override func tearDown() {
  37. XCTAssertNoThrow(try self.channel.finish())
  38. super.tearDown()
  39. }
  40. func testTransformLibraryError_whenTransformingErrorToStatus_unary() throws {
  41. try testTransformLibraryError_whenTransformingErrorToStatus(uri: "/echo.Echo/Get")
  42. }
  43. func testTransformLibraryError_whenTransformingErrorToStatus_clientStreaming() throws {
  44. try testTransformLibraryError_whenTransformingErrorToStatus(uri: "/echo.Echo/Collect")
  45. }
  46. func testTransformLibraryError_whenTransformingErrorToStatus_serverStreaming() throws {
  47. try testTransformLibraryError_whenTransformingErrorToStatus(uri: "/echo.Echo/Expand")
  48. }
  49. func testTransformLibraryError_whenTransformingErrorToStatus_bidirectionalStreaming() throws {
  50. try testTransformLibraryError_whenTransformingErrorToStatus(uri: "/echo.Echo/Update")
  51. }
  52. private func testTransformLibraryError_whenTransformingErrorToStatus(uri: String) throws {
  53. self.setupChannelAndDelegate { _ in
  54. GRPCStatusAndMetadata(status: .init(code: .notFound, message: "some error"))
  55. }
  56. let requestHead = HTTPRequestHead(
  57. version: .init(major: 2, minor: 0),
  58. method: .POST,
  59. uri: uri,
  60. headers: ["content-type": "application/grpc"]
  61. )
  62. XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(requestHead)))
  63. self.channel.pipeline.fireErrorCaught(GRPCStatus(code: .aborted, message: nil))
  64. // This is the head
  65. XCTAssertNoThrow(try self.channel.readOutbound(as: HTTPServerResponsePart.self))
  66. let end = try self.channel.readOutbound(as: HTTPServerResponsePart.self)
  67. guard case let .some(.end(headers)) = end else {
  68. XCTFail("Expected headers but got \(end.debugDescription)")
  69. return
  70. }
  71. XCTAssertEqual(headers?.first(name: "grpc-status"), "5")
  72. XCTAssertEqual(headers?.first(name: "grpc-message"), "some error")
  73. }
  74. func testTransformLibraryError_whenTransformingErrorToStatusAndMetadata_unary() throws {
  75. try testTransformLibraryError_whenTransformingErrorToStatusAndMetadata(uri: "/echo.Echo/Get")
  76. }
  77. func testTransformLibraryError_whenTransformingErrorToStatusAndMetadata_clientStreaming() throws {
  78. try testTransformLibraryError_whenTransformingErrorToStatusAndMetadata(uri: "/echo.Echo/Collect")
  79. }
  80. func testTransformLibraryError_whenTransformingErrorToStatusAndMetadata_serverStreaming() throws {
  81. try testTransformLibraryError_whenTransformingErrorToStatusAndMetadata(uri: "/echo.Echo/Expand")
  82. }
  83. func testTransformLibraryError_whenTransformingErrorToStatusAndMetadata_bidirectionalStreaming() throws {
  84. try testTransformLibraryError_whenTransformingErrorToStatusAndMetadata(uri: "/echo.Echo/Update")
  85. }
  86. private func testTransformLibraryError_whenTransformingErrorToStatusAndMetadata(uri: String) throws {
  87. self.setupChannelAndDelegate { _ in
  88. GRPCStatusAndMetadata(
  89. status: .init(code: .notFound, message: "some error"),
  90. metadata: ["some-metadata": "test"]
  91. )
  92. }
  93. let requestHead = HTTPRequestHead(
  94. version: .init(major: 2, minor: 0),
  95. method: .POST,
  96. uri: uri,
  97. headers: ["content-type": "application/grpc"]
  98. )
  99. XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(requestHead)))
  100. self.channel.pipeline.fireErrorCaught(GRPCStatus(code: .aborted, message: nil))
  101. // This is the head
  102. XCTAssertNoThrow(try self.channel.readOutbound(as: HTTPServerResponsePart.self))
  103. let end = try self.channel.readOutbound(as: HTTPServerResponsePart.self)
  104. guard case let .some(.end(headers)) = end else {
  105. XCTFail("Expected headers but got \(end.debugDescription)")
  106. return
  107. }
  108. XCTAssertEqual(headers?.first(name: "grpc-status"), "5")
  109. XCTAssertEqual(headers?.first(name: "grpc-message"), "some error")
  110. XCTAssertEqual(headers?.first(name: "some-metadata"), "test")
  111. }
  112. private func setupChannelAndDelegate(transformLibraryErrorHandler: @escaping ((Error) -> (GRPCStatusAndMetadata?))) {
  113. let provider = EchoProvider()
  114. self.errorDelegate = ServerErrorDelegateMock(transformLibraryErrorHandler: transformLibraryErrorHandler)
  115. let handler = GRPCServerRequestRoutingHandler(
  116. servicesByName: [provider.serviceName: provider],
  117. encoding: .disabled,
  118. errorDelegate: self.errorDelegate,
  119. logger: self.logger
  120. )
  121. self.channel = EmbeddedChannel(handler: handler)
  122. }
  123. }