GRPCStatusCodeTests.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 EchoModel
  17. import Foundation
  18. @testable import GRPC
  19. import Logging
  20. import NIOCore
  21. import NIOEmbedded
  22. import NIOHPACK
  23. import NIOHTTP1
  24. import NIOHTTP2
  25. import XCTest
  26. class GRPCStatusCodeTests: GRPCTestCase {
  27. var channel: EmbeddedChannel!
  28. override func setUp() {
  29. super.setUp()
  30. let handler = GRPCClientChannelHandler(
  31. callType: .unary,
  32. maximumReceiveMessageLength: .max,
  33. logger: GRPCLogger(wrapping: self.logger)
  34. )
  35. self.channel = EmbeddedChannel(handler: handler)
  36. }
  37. func headersFramePayload(status: HTTPResponseStatus) -> HTTP2Frame.FramePayload {
  38. let headers: HPACKHeaders = [":status": "\(status.code)"]
  39. return .headers(.init(headers: headers))
  40. }
  41. func sendRequestHead() {
  42. let requestHead = _GRPCRequestHead(
  43. method: "POST",
  44. scheme: "http",
  45. path: "/foo/bar",
  46. host: "localhost",
  47. deadline: .distantFuture,
  48. customMetadata: [:],
  49. encoding: .disabled
  50. )
  51. let clientRequestHead: _RawGRPCClientRequestPart = .head(requestHead)
  52. XCTAssertNoThrow(try self.channel.writeOutbound(clientRequestHead))
  53. }
  54. func doTestResponseStatus(_ status: HTTPResponseStatus, expected: GRPCStatus.Code) throws {
  55. // Send the request head so we're in a valid state to receive headers.
  56. self.sendRequestHead()
  57. XCTAssertThrowsError(
  58. try self.channel
  59. .writeInbound(self.headersFramePayload(status: status))
  60. ) { error in
  61. guard let withContext = error as? GRPCError.WithContext,
  62. let invalidHTTPStatus = withContext.error as? GRPCError.InvalidHTTPStatus else {
  63. XCTFail("Unexpected error: \(error)")
  64. return
  65. }
  66. XCTAssertEqual(invalidHTTPStatus.makeGRPCStatus().code, expected)
  67. }
  68. }
  69. func testTooManyRequests() throws {
  70. try self.doTestResponseStatus(.tooManyRequests, expected: .unavailable)
  71. }
  72. func testBadGateway() throws {
  73. try self.doTestResponseStatus(.badGateway, expected: .unavailable)
  74. }
  75. func testServiceUnavailable() throws {
  76. try self.doTestResponseStatus(.serviceUnavailable, expected: .unavailable)
  77. }
  78. func testGatewayTimeout() throws {
  79. try self.doTestResponseStatus(.gatewayTimeout, expected: .unavailable)
  80. }
  81. func testBadRequest() throws {
  82. try self.doTestResponseStatus(.badRequest, expected: .internalError)
  83. }
  84. func testUnauthorized() throws {
  85. try self.doTestResponseStatus(.unauthorized, expected: .unauthenticated)
  86. }
  87. func testForbidden() throws {
  88. try self.doTestResponseStatus(.forbidden, expected: .permissionDenied)
  89. }
  90. func testNotFound() throws {
  91. try self.doTestResponseStatus(.notFound, expected: .unimplemented)
  92. }
  93. func testStatusCodeAndMessageAreRespectedForNon200Responses() throws {
  94. let status = GRPCStatus(code: .unknown, message: "Not the HTTP error phrase")
  95. let headers: HPACKHeaders = [
  96. ":status": "\(HTTPResponseStatus.imATeapot.code)",
  97. GRPCHeaderName.statusCode: "\(status.code.rawValue)",
  98. GRPCHeaderName.statusMessage: status.message!,
  99. ]
  100. self.sendRequestHead()
  101. let headerFramePayload = HTTP2Frame.FramePayload.headers(.init(headers: headers))
  102. XCTAssertThrowsError(try self.channel.writeInbound(headerFramePayload)) { error in
  103. guard let withContext = error as? GRPCError.WithContext,
  104. let invalidHTTPStatus = withContext.error as? GRPCError.InvalidHTTPStatusWithGRPCStatus
  105. else {
  106. XCTFail("Unexpected error: \(error)")
  107. return
  108. }
  109. XCTAssertEqual(invalidHTTPStatus.makeGRPCStatus(), status)
  110. }
  111. }
  112. func testCodeFromRawValue() {
  113. XCTAssertEqual(GRPCStatus.Code(rawValue: 0), .ok)
  114. XCTAssertEqual(GRPCStatus.Code(rawValue: 1), .cancelled)
  115. XCTAssertEqual(GRPCStatus.Code(rawValue: 2), .unknown)
  116. XCTAssertEqual(GRPCStatus.Code(rawValue: 3), .invalidArgument)
  117. XCTAssertEqual(GRPCStatus.Code(rawValue: 4), .deadlineExceeded)
  118. XCTAssertEqual(GRPCStatus.Code(rawValue: 5), .notFound)
  119. XCTAssertEqual(GRPCStatus.Code(rawValue: 6), .alreadyExists)
  120. XCTAssertEqual(GRPCStatus.Code(rawValue: 7), .permissionDenied)
  121. XCTAssertEqual(GRPCStatus.Code(rawValue: 8), .resourceExhausted)
  122. XCTAssertEqual(GRPCStatus.Code(rawValue: 9), .failedPrecondition)
  123. XCTAssertEqual(GRPCStatus.Code(rawValue: 10), .aborted)
  124. XCTAssertEqual(GRPCStatus.Code(rawValue: 11), .outOfRange)
  125. XCTAssertEqual(GRPCStatus.Code(rawValue: 12), .unimplemented)
  126. XCTAssertEqual(GRPCStatus.Code(rawValue: 13), .internalError)
  127. XCTAssertEqual(GRPCStatus.Code(rawValue: 14), .unavailable)
  128. XCTAssertEqual(GRPCStatus.Code(rawValue: 15), .dataLoss)
  129. XCTAssertEqual(GRPCStatus.Code(rawValue: 16), .unauthenticated)
  130. XCTAssertNil(GRPCStatus.Code(rawValue: -1))
  131. XCTAssertNil(GRPCStatus.Code(rawValue: 17))
  132. }
  133. }