2
0

ServerErrorDelegateTests.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 EchoImplementation
  17. import EchoModel
  18. import Foundation
  19. import GRPC
  20. import Logging
  21. import NIO
  22. import NIOHTTP1
  23. import XCTest
  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 self.testTransformLibraryError_whenTransformingErrorToStatus(uri: "/echo.Echo/Get")
  42. }
  43. func testTransformLibraryError_whenTransformingErrorToStatus_clientStreaming() throws {
  44. try self.testTransformLibraryError_whenTransformingErrorToStatus(uri: "/echo.Echo/Collect")
  45. }
  46. func testTransformLibraryError_whenTransformingErrorToStatus_serverStreaming() throws {
  47. try self.testTransformLibraryError_whenTransformingErrorToStatus(uri: "/echo.Echo/Expand")
  48. }
  49. func testTransformLibraryError_whenTransformingErrorToStatus_bidirectionalStreaming() throws {
  50. try self.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 self
  76. .testTransformLibraryError_whenTransformingErrorToStatusAndMetadata(uri: "/echo.Echo/Get")
  77. }
  78. func testTransformLibraryError_whenTransformingErrorToStatusAndMetadata_clientStreaming() throws {
  79. try self
  80. .testTransformLibraryError_whenTransformingErrorToStatusAndMetadata(uri: "/echo.Echo/Collect")
  81. }
  82. func testTransformLibraryError_whenTransformingErrorToStatusAndMetadata_serverStreaming() throws {
  83. try self
  84. .testTransformLibraryError_whenTransformingErrorToStatusAndMetadata(uri: "/echo.Echo/Expand")
  85. }
  86. func testTransformLibraryError_whenTransformingErrorToStatusAndMetadata_bidirectionalStreaming(
  87. ) throws {
  88. try self
  89. .testTransformLibraryError_whenTransformingErrorToStatusAndMetadata(uri: "/echo.Echo/Update")
  90. }
  91. private func testTransformLibraryError_whenTransformingErrorToStatusAndMetadata(
  92. uri: String
  93. ) throws {
  94. self.setupChannelAndDelegate { _ in
  95. GRPCStatusAndMetadata(
  96. status: .init(code: .notFound, message: "some error"),
  97. metadata: ["some-metadata": "test"]
  98. )
  99. }
  100. let requestHead = HTTPRequestHead(
  101. version: .init(major: 2, minor: 0),
  102. method: .POST,
  103. uri: uri,
  104. headers: ["content-type": "application/grpc"]
  105. )
  106. XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(requestHead)))
  107. self.channel.pipeline.fireErrorCaught(GRPCStatus(code: .aborted, message: nil))
  108. // This is the head
  109. XCTAssertNoThrow(try self.channel.readOutbound(as: HTTPServerResponsePart.self))
  110. let end = try self.channel.readOutbound(as: HTTPServerResponsePart.self)
  111. guard case let .some(.end(headers)) = end else {
  112. XCTFail("Expected headers but got \(end.debugDescription)")
  113. return
  114. }
  115. XCTAssertEqual(headers?.first(name: "grpc-status"), "5")
  116. XCTAssertEqual(headers?.first(name: "grpc-message"), "some error")
  117. XCTAssertEqual(headers?.first(name: "some-metadata"), "test")
  118. }
  119. private func setupChannelAndDelegate(transformLibraryErrorHandler: @escaping (
  120. (Error)
  121. -> (GRPCStatusAndMetadata?)
  122. )) {
  123. let provider = EchoProvider()
  124. self
  125. .errorDelegate =
  126. ServerErrorDelegateMock(transformLibraryErrorHandler: transformLibraryErrorHandler)
  127. let handler = GRPCServerRequestRoutingHandler(
  128. servicesByName: [provider.serviceName: provider],
  129. encoding: .disabled,
  130. errorDelegate: self.errorDelegate,
  131. logger: self.logger
  132. )
  133. self.channel = EmbeddedChannel(handler: handler)
  134. }
  135. }