GRPCStatusCodeTests.swift 5.2 KB

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