StatusTests.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2023, 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 GRPCCore
  17. import Testing
  18. @Suite("Status")
  19. struct StatusTests {
  20. @Suite("Code")
  21. struct Code {
  22. @Test("rawValue", arguments: zip(Status.Code.all, 0 ... 16))
  23. func rawValueOfStatusCodes(code: Status.Code, expected: Int) {
  24. #expect(code.rawValue == expected)
  25. }
  26. @Test(
  27. "Initialize from RPCError.Code",
  28. arguments: zip(
  29. RPCError.Code.all,
  30. Status.Code.all.dropFirst() // Drop '.ok', there is no '.ok' error code.
  31. )
  32. )
  33. func initFromRPCErrorCode(errorCode: RPCError.Code, expected: Status.Code) {
  34. #expect(Status.Code(errorCode) == expected)
  35. }
  36. @Test("Initialize from rawValue", arguments: zip(0 ... 16, Status.Code.all))
  37. func initFromRawValue(rawValue: Int, expected: Status.Code) {
  38. #expect(Status.Code(rawValue: rawValue) == expected)
  39. }
  40. @Test("Initialize from invalid rawValue", arguments: [-1, 17, 100, .max])
  41. func initFromInvalidRawValue(rawValue: Int) {
  42. #expect(Status.Code(rawValue: rawValue) == nil)
  43. }
  44. }
  45. @Test("CustomStringConvertible conformance")
  46. func customStringConvertible() {
  47. #expect("\(Status(code: .ok, message: ""))" == #"ok: """#)
  48. #expect("\(Status(code: .dataLoss, message: "oh no"))" == #"dataLoss: "oh no""#)
  49. }
  50. @Test("Equatable conformance")
  51. func equatable() {
  52. let ok = Status(code: .ok, message: "")
  53. let okWithMessage = Status(code: .ok, message: "message")
  54. let internalError = Status(code: .internalError, message: "")
  55. #expect(ok == ok)
  56. #expect(ok != okWithMessage)
  57. #expect(ok != internalError)
  58. }
  59. @Test("Fits in existential container")
  60. func fitsInExistentialContainer() {
  61. #expect(MemoryLayout<Status>.size <= 24)
  62. }
  63. @Test(
  64. "From HTTP status code",
  65. arguments: [
  66. (400, Status(code: .internalError, message: "HTTP 400: Bad Request")),
  67. (401, Status(code: .unauthenticated, message: "HTTP 401: Unauthorized")),
  68. (403, Status(code: .permissionDenied, message: "HTTP 403: Forbidden")),
  69. (404, Status(code: .unimplemented, message: "HTTP 404: Not Found")),
  70. (429, Status(code: .unavailable, message: "HTTP 429: Too Many Requests")),
  71. (502, Status(code: .unavailable, message: "HTTP 502: Bad Gateway")),
  72. (503, Status(code: .unavailable, message: "HTTP 503: Service Unavailable")),
  73. (504, Status(code: .unavailable, message: "HTTP 504: Gateway Timeout")),
  74. (418, Status(code: .unknown, message: "HTTP 418")),
  75. ]
  76. )
  77. func convertFromHTTPStatusCode(code: Int, expected: Status) {
  78. let status = Status(httpStatusCode: code)
  79. #expect(status == expected)
  80. }
  81. }