GRPCStatusCodeTests.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. @testable import GRPC
  18. import EchoModel
  19. import NIO
  20. import NIOHTTP1
  21. import NIOHTTP2
  22. import XCTest
  23. import Logging
  24. class GRPCStatusCodeTests: GRPCTestCase {
  25. var channel: EmbeddedChannel!
  26. var metadataPromise: EventLoopPromise<HTTPHeaders>!
  27. var trailingMetadataPromise: EventLoopPromise<HTTPHeaders>!
  28. var responsePromise: EventLoopPromise<Echo_EchoResponse>!
  29. var statusPromise: EventLoopPromise<GRPCStatus>!
  30. override func setUp() {
  31. super.setUp()
  32. self.channel = EmbeddedChannel()
  33. self.metadataPromise = self.channel.eventLoop.makePromise()
  34. self.trailingMetadataPromise = self.channel.eventLoop.makePromise()
  35. self.responsePromise = self.channel.eventLoop.makePromise()
  36. self.statusPromise = self.channel.eventLoop.makePromise()
  37. let requestID = UUID().uuidString
  38. let logger = Logger(subsystem: .clientChannelCall, metadata: [MetadataKey.requestID: "\(requestID)"])
  39. try! self.channel.pipeline.addHandlers([
  40. HTTP1ToRawGRPCClientCodec(logger: logger),
  41. GRPCClientCodec<Echo_EchoRequest, Echo_EchoResponse>(logger: logger),
  42. GRPCClientUnaryResponseChannelHandler<Echo_EchoResponse>(
  43. initialMetadataPromise: self.metadataPromise,
  44. trailingMetadataPromise: self.trailingMetadataPromise,
  45. responsePromise: self.responsePromise,
  46. statusPromise: self.statusPromise,
  47. errorDelegate: nil,
  48. timeout: .infinite,
  49. logger: logger
  50. )
  51. ]).wait()
  52. }
  53. override func tearDown() {
  54. self.metadataPromise.fail(GRPCError.client(.cancelledByClient))
  55. self.responsePromise.fail(GRPCError.client(.cancelledByClient))
  56. self.statusPromise.fail(GRPCError.client(.cancelledByClient))
  57. }
  58. func responseHead(code: HTTPResponseStatus, headers: HTTPHeaders = HTTPHeaders()) -> HTTPClientResponsePart {
  59. return .head(HTTPResponseHead(version: HTTPVersion(major: 2, minor: 0), status: code, headers: headers))
  60. }
  61. func testTooManyRequests() throws {
  62. XCTAssertNoThrow(try self.channel.writeInbound(self.responseHead(code: .tooManyRequests)))
  63. XCTAssertEqual(try statusPromise.futureResult.map { $0.code }.wait(), .unavailable)
  64. }
  65. func testBadGateway() throws {
  66. XCTAssertNoThrow(try self.channel.writeInbound(self.responseHead(code: .badGateway)))
  67. XCTAssertEqual(try statusPromise.futureResult.map { $0.code }.wait(), .unavailable)
  68. }
  69. func testServiceUnavailable() throws {
  70. XCTAssertNoThrow(try self.channel.writeInbound(self.responseHead(code: .serviceUnavailable)))
  71. XCTAssertEqual(try statusPromise.futureResult.map { $0.code }.wait(), .unavailable)
  72. }
  73. func testGatewayTimeout() throws {
  74. XCTAssertNoThrow(try self.channel.writeInbound(self.responseHead(code: .gatewayTimeout)))
  75. XCTAssertEqual(try statusPromise.futureResult.map { $0.code }.wait(), .unavailable)
  76. }
  77. func testBadRequest() throws {
  78. XCTAssertNoThrow(try self.channel.writeInbound(self.responseHead(code: .badRequest)))
  79. XCTAssertEqual(try statusPromise.futureResult.map { $0.code }.wait(), .internalError)
  80. }
  81. func testUnauthorized() throws {
  82. XCTAssertNoThrow(try self.channel.writeInbound(self.responseHead(code: .unauthorized)))
  83. XCTAssertEqual(try statusPromise.futureResult.map { $0.code }.wait(), .unauthenticated)
  84. }
  85. func testForbidden() throws {
  86. XCTAssertNoThrow(try self.channel.writeInbound(self.responseHead(code: .forbidden)))
  87. XCTAssertEqual(try statusPromise.futureResult.map { $0.code }.wait(), .permissionDenied)
  88. }
  89. func testNotFound() throws {
  90. XCTAssertNoThrow(try self.channel.writeInbound(self.responseHead(code: .notFound)))
  91. XCTAssertEqual(try statusPromise.futureResult.map { $0.code }.wait(), .unimplemented)
  92. }
  93. func testStatusCodeAndMessageAreRespectedForNon200Responses() throws {
  94. let statusCode: GRPCStatus.Code = .doNotUse
  95. let statusMessage = "Not the HTTP error phrase"
  96. var headers = HTTPHeaders()
  97. headers.add(name: GRPCHeaderName.statusCode, value: "\(statusCode.rawValue)")
  98. headers.add(name: GRPCHeaderName.statusMessage, value: statusMessage)
  99. XCTAssertNoThrow(try self.channel.writeInbound(self.responseHead(code: .imATeapot, headers: headers)))
  100. let status = try statusPromise.futureResult.wait()
  101. XCTAssertEqual(status.code, statusCode)
  102. XCTAssertEqual(status.message, statusMessage)
  103. }
  104. }