GRPCStatusCodeTests.swift 4.6 KB

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