GRPCStatusCodeTests.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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<Echo_EchoRequest, Echo_EchoResponse>(
  30. streamID: .init(1),
  31. callType: .unary,
  32. logger: self.logger
  33. )
  34. self.channel = EmbeddedChannel(handler: handler)
  35. }
  36. func headersFrame(status: HTTPResponseStatus) -> HTTP2Frame {
  37. let headers: HPACKHeaders = [":status": "\(status.code)"]
  38. return .init(streamID: .init(1), payload: .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: _GRPCClientRequestPart<Echo_EchoRequest> = .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(try self.channel.writeInbound(self.headersFrame(status: status))) { error in
  57. guard let withContext = error as? GRPCError.WithContext,
  58. let invalidHTTPStatus = withContext.error as? GRPCError.InvalidHTTPStatus else {
  59. XCTFail("Unexpected error: \(error)")
  60. return
  61. }
  62. XCTAssertEqual(invalidHTTPStatus.makeGRPCStatus().code, expected)
  63. }
  64. }
  65. func testTooManyRequests() throws {
  66. try self.doTestResponseStatus(.tooManyRequests, expected: .unavailable)
  67. }
  68. func testBadGateway() throws {
  69. try self.doTestResponseStatus(.badGateway, expected: .unavailable)
  70. }
  71. func testServiceUnavailable() throws {
  72. try self.doTestResponseStatus(.serviceUnavailable, expected: .unavailable)
  73. }
  74. func testGatewayTimeout() throws {
  75. try self.doTestResponseStatus(.gatewayTimeout, expected: .unavailable)
  76. }
  77. func testBadRequest() throws {
  78. try self.doTestResponseStatus(.badRequest, expected: .internalError)
  79. }
  80. func testUnauthorized() throws {
  81. try self.doTestResponseStatus(.unauthorized, expected: .unauthenticated)
  82. }
  83. func testForbidden() throws {
  84. try self.doTestResponseStatus(.forbidden, expected: .permissionDenied)
  85. }
  86. func testNotFound() throws {
  87. try self.doTestResponseStatus(.notFound, expected: .unimplemented)
  88. }
  89. func testStatusCodeAndMessageAreRespectedForNon200Responses() throws {
  90. let status = GRPCStatus(code: .doNotUse, message: "Not the HTTP error phrase")
  91. let headers: HPACKHeaders = [
  92. ":status": "\(HTTPResponseStatus.imATeapot.code)",
  93. GRPCHeaderName.statusCode: "\(status.code.rawValue)",
  94. GRPCHeaderName.statusMessage: status.message!
  95. ]
  96. self.sendRequestHead()
  97. let headerFrame = HTTP2Frame(streamID: .init(1), payload: .headers(.init(headers: headers)))
  98. XCTAssertThrowsError(try self.channel.writeInbound(headerFrame)) { error in
  99. guard let withContext = error as? GRPCError.WithContext,
  100. let invalidHTTPStatus = withContext.error as? GRPCError.InvalidHTTPStatusWithGRPCStatus else {
  101. XCTFail("Unexpected error: \(error)")
  102. return
  103. }
  104. XCTAssertEqual(invalidHTTPStatus.makeGRPCStatus(), status)
  105. }
  106. }
  107. }