2
0

GRPCStatusCodeTests.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. )
  62. let clientRequestHead: _GRPCClientRequestPart<Echo_EchoRequest> = .head(requestHead)
  63. XCTAssertNoThrow(try self.channel.writeOutbound(clientRequestHead))
  64. }
  65. func doTestResponseStatus(_ status: HTTPResponseStatus, expected: GRPCStatus.Code) throws {
  66. // Send the request head so we're in a valid state to receive headers.
  67. self.sendRequestHead()
  68. XCTAssertNoThrow(try self.channel.writeInbound(self.headersFrame(status: status)))
  69. XCTAssertEqual(try self.status.map { $0.code }.wait(), expected)
  70. }
  71. func testTooManyRequests() throws {
  72. try self.doTestResponseStatus(.tooManyRequests, expected: .unavailable)
  73. }
  74. func testBadGateway() throws {
  75. try self.doTestResponseStatus(.badGateway, expected: .unavailable)
  76. }
  77. func testServiceUnavailable() throws {
  78. try self.doTestResponseStatus(.serviceUnavailable, expected: .unavailable)
  79. }
  80. func testGatewayTimeout() throws {
  81. try self.doTestResponseStatus(.gatewayTimeout, expected: .unavailable)
  82. }
  83. func testBadRequest() throws {
  84. try self.doTestResponseStatus(.badRequest, expected: .internalError)
  85. }
  86. func testUnauthorized() throws {
  87. try self.doTestResponseStatus(.unauthorized, expected: .unauthenticated)
  88. }
  89. func testForbidden() throws {
  90. try self.doTestResponseStatus(.forbidden, expected: .permissionDenied)
  91. }
  92. func testNotFound() throws {
  93. try self.doTestResponseStatus(.notFound, expected: .unimplemented)
  94. }
  95. func testStatusCodeAndMessageAreRespectedForNon200Responses() throws {
  96. let statusCode: GRPCStatus.Code = .doNotUse
  97. let statusMessage = "Not the HTTP error phrase"
  98. let headers: HPACKHeaders = [
  99. ":status": "\(HTTPResponseStatus.imATeapot.code)",
  100. GRPCHeaderName.statusCode: "\(statusCode.rawValue)",
  101. GRPCHeaderName.statusMessage: statusMessage
  102. ]
  103. self.sendRequestHead()
  104. let headerFrame = HTTP2Frame(streamID: .init(1), payload: .headers(.init(headers: headers)))
  105. XCTAssertNoThrow(try self.channel.writeInbound(headerFrame))
  106. let status = try self.status.wait()
  107. XCTAssertEqual(status.code, statusCode)
  108. XCTAssertEqual(status.message, statusMessage)
  109. }
  110. }