2
0

GRPCStatusCodeTests.swift 4.6 KB

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