GRPCStatusCodeTests.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 NIOHPACK
  21. import NIOHTTP1
  22. import NIOHTTP2
  23. import XCTest
  24. import Logging
  25. class GRPCStatusCodeTests: GRPCTestCase {
  26. var channel: EmbeddedChannel!
  27. var status: EventLoopFuture<GRPCStatus>!
  28. override func setUp() {
  29. super.setUp()
  30. let logger = Logger(label: "io.grpc.testing")
  31. self.channel = EmbeddedChannel()
  32. let statusPromise = self.channel.eventLoop.makePromise(of: GRPCStatus.self)
  33. self.status = statusPromise.futureResult
  34. try! self.channel.pipeline.addHandlers([
  35. _GRPCClientChannelHandler<Echo_EchoRequest, Echo_EchoResponse>(streamID: .init(1), callType: .unary, logger: logger),
  36. GRPCClientUnaryResponseChannelHandler<Echo_EchoResponse>(
  37. initialMetadataPromise: channel.eventLoop.makePromise(),
  38. trailingMetadataPromise: channel.eventLoop.makePromise(),
  39. responsePromise: channel.eventLoop.makePromise(),
  40. statusPromise: statusPromise,
  41. errorDelegate: nil,
  42. timeout: .infinite,
  43. logger: logger
  44. )
  45. ]).wait()
  46. }
  47. override func tearDown() {
  48. }
  49. func headersFrame(status: HTTPResponseStatus) -> HTTP2Frame {
  50. let headers: HPACKHeaders = [":status": "\(status.code)"]
  51. return .init(streamID: .init(1), payload: .headers(.init(headers: headers)))
  52. }
  53. func sendRequestHead() {
  54. let requestHead = _GRPCRequestHead(
  55. method: "POST",
  56. scheme: "http",
  57. path: "/foo/bar",
  58. host: "localhost",
  59. timeout: .infinite,
  60. customMetadata: [:],
  61. encoding: .none
  62. )
  63. let clientRequestHead: _GRPCClientRequestPart<Echo_EchoRequest> = .head(requestHead)
  64. XCTAssertNoThrow(try self.channel.writeOutbound(clientRequestHead))
  65. }
  66. func doTestResponseStatus(_ status: HTTPResponseStatus, expected: GRPCStatus.Code) throws {
  67. // Send the request head so we're in a valid state to receive headers.
  68. self.sendRequestHead()
  69. XCTAssertNoThrow(try self.channel.writeInbound(self.headersFrame(status: status)))
  70. XCTAssertEqual(try self.status.map { $0.code }.wait(), expected)
  71. }
  72. func testTooManyRequests() throws {
  73. try self.doTestResponseStatus(.tooManyRequests, expected: .unavailable)
  74. }
  75. func testBadGateway() throws {
  76. try self.doTestResponseStatus(.badGateway, expected: .unavailable)
  77. }
  78. func testServiceUnavailable() throws {
  79. try self.doTestResponseStatus(.serviceUnavailable, expected: .unavailable)
  80. }
  81. func testGatewayTimeout() throws {
  82. try self.doTestResponseStatus(.gatewayTimeout, expected: .unavailable)
  83. }
  84. func testBadRequest() throws {
  85. try self.doTestResponseStatus(.badRequest, expected: .internalError)
  86. }
  87. func testUnauthorized() throws {
  88. try self.doTestResponseStatus(.unauthorized, expected: .unauthenticated)
  89. }
  90. func testForbidden() throws {
  91. try self.doTestResponseStatus(.forbidden, expected: .permissionDenied)
  92. }
  93. func testNotFound() throws {
  94. try self.doTestResponseStatus(.notFound, expected: .unimplemented)
  95. }
  96. func testStatusCodeAndMessageAreRespectedForNon200Responses() throws {
  97. let statusCode: GRPCStatus.Code = .doNotUse
  98. let statusMessage = "Not the HTTP error phrase"
  99. let headers: HPACKHeaders = [
  100. ":status": "\(HTTPResponseStatus.imATeapot.code)",
  101. GRPCHeaderName.statusCode: "\(statusCode.rawValue)",
  102. GRPCHeaderName.statusMessage: statusMessage
  103. ]
  104. self.sendRequestHead()
  105. let headerFrame = HTTP2Frame(streamID: .init(1), payload: .headers(.init(headers: headers)))
  106. XCTAssertNoThrow(try self.channel.writeInbound(headerFrame))
  107. let status = try self.status.wait()
  108. XCTAssertEqual(status.code, statusCode)
  109. XCTAssertEqual(status.message, statusMessage)
  110. }
  111. }