GRPCStatusCodeTests.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. override func setUp() {
  28. super.setUp()
  29. let handler = _GRPCClientChannelHandler(callType: .unary, logger: self.logger)
  30. self.channel = EmbeddedChannel(handler: handler)
  31. }
  32. func headersFramePayload(status: HTTPResponseStatus) -> HTTP2Frame.FramePayload {
  33. let headers: HPACKHeaders = [":status": "\(status.code)"]
  34. return .headers(.init(headers: headers))
  35. }
  36. func sendRequestHead() {
  37. let requestHead = _GRPCRequestHead(
  38. method: "POST",
  39. scheme: "http",
  40. path: "/foo/bar",
  41. host: "localhost",
  42. deadline: .distantFuture,
  43. customMetadata: [:],
  44. encoding: .disabled
  45. )
  46. let clientRequestHead: _RawGRPCClientRequestPart = .head(requestHead)
  47. XCTAssertNoThrow(try self.channel.writeOutbound(clientRequestHead))
  48. }
  49. func doTestResponseStatus(_ status: HTTPResponseStatus, expected: GRPCStatus.Code) throws {
  50. // Send the request head so we're in a valid state to receive headers.
  51. self.sendRequestHead()
  52. XCTAssertThrowsError(try self.channel.writeInbound(self.headersFramePayload(status: status))) { error in
  53. guard let withContext = error as? GRPCError.WithContext,
  54. let invalidHTTPStatus = withContext.error as? GRPCError.InvalidHTTPStatus else {
  55. XCTFail("Unexpected error: \(error)")
  56. return
  57. }
  58. XCTAssertEqual(invalidHTTPStatus.makeGRPCStatus().code, expected)
  59. }
  60. }
  61. func testTooManyRequests() throws {
  62. try self.doTestResponseStatus(.tooManyRequests, expected: .unavailable)
  63. }
  64. func testBadGateway() throws {
  65. try self.doTestResponseStatus(.badGateway, expected: .unavailable)
  66. }
  67. func testServiceUnavailable() throws {
  68. try self.doTestResponseStatus(.serviceUnavailable, expected: .unavailable)
  69. }
  70. func testGatewayTimeout() throws {
  71. try self.doTestResponseStatus(.gatewayTimeout, expected: .unavailable)
  72. }
  73. func testBadRequest() throws {
  74. try self.doTestResponseStatus(.badRequest, expected: .internalError)
  75. }
  76. func testUnauthorized() throws {
  77. try self.doTestResponseStatus(.unauthorized, expected: .unauthenticated)
  78. }
  79. func testForbidden() throws {
  80. try self.doTestResponseStatus(.forbidden, expected: .permissionDenied)
  81. }
  82. func testNotFound() throws {
  83. try self.doTestResponseStatus(.notFound, expected: .unimplemented)
  84. }
  85. func testStatusCodeAndMessageAreRespectedForNon200Responses() throws {
  86. let status = GRPCStatus(code: .doNotUse, message: "Not the HTTP error phrase")
  87. let headers: HPACKHeaders = [
  88. ":status": "\(HTTPResponseStatus.imATeapot.code)",
  89. GRPCHeaderName.statusCode: "\(status.code.rawValue)",
  90. GRPCHeaderName.statusMessage: status.message!
  91. ]
  92. self.sendRequestHead()
  93. let headerFramePayload = HTTP2Frame.FramePayload.headers(.init(headers: headers))
  94. XCTAssertThrowsError(try self.channel.writeInbound(headerFramePayload)) { error in
  95. guard let withContext = error as? GRPCError.WithContext,
  96. let invalidHTTPStatus = withContext.error as? GRPCError.InvalidHTTPStatusWithGRPCStatus else {
  97. XCTFail("Unexpected error: \(error)")
  98. return
  99. }
  100. XCTAssertEqual(invalidHTTPStatus.makeGRPCStatus(), status)
  101. }
  102. }
  103. }