ServerResponseTests.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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("ServerResponse")
  19. struct ServerResponseTests {
  20. @Test("ServerResponse(message:metadata:trailingMetadata:)")
  21. func responseInitSuccess() throws {
  22. let response = ServerResponse(
  23. message: "message",
  24. metadata: ["metadata": "initial"],
  25. trailingMetadata: ["metadata": "trailing"]
  26. )
  27. let contents = try #require(try response.accepted.get())
  28. #expect(contents.message == "message")
  29. #expect(contents.metadata == ["metadata": "initial"])
  30. #expect(contents.trailingMetadata == ["metadata": "trailing"])
  31. }
  32. @Test("ServerResponse(of:error:)")
  33. func responseInitError() throws {
  34. let error = RPCError(code: .aborted, message: "Aborted")
  35. let response = ServerResponse(of: String.self, error: error)
  36. switch response.accepted {
  37. case .success:
  38. Issue.record("Expected error")
  39. case .failure(let rpcError):
  40. #expect(rpcError == error)
  41. }
  42. }
  43. @Test("StreamingServerResponse(of:metadata:producer:)")
  44. func streamingResponseInitSuccess() async throws {
  45. let response = StreamingServerResponse(
  46. of: String.self,
  47. metadata: ["metadata": "initial"]
  48. ) { _ in
  49. // Empty body.
  50. return ["metadata": "trailing"]
  51. }
  52. let contents = try #require(try response.accepted.get())
  53. #expect(contents.metadata == ["metadata": "initial"])
  54. let trailingMetadata = try await contents.producer(.failTestOnWrite())
  55. #expect(trailingMetadata == ["metadata": "trailing"])
  56. }
  57. @Test("StreamingServerResponse(of:error:)")
  58. func streamingResponseInitError() async throws {
  59. let error = RPCError(code: .aborted, message: "Aborted")
  60. let response = StreamingServerResponse(of: String.self, error: error)
  61. switch response.accepted {
  62. case .success:
  63. Issue.record("Expected error")
  64. case .failure(let rpcError):
  65. #expect(rpcError == error)
  66. }
  67. }
  68. @Test("StreamingServerResponse(single:) (accepted)")
  69. func singleToStreamConversionForSuccessfulResponse() async throws {
  70. let single = ServerResponse(
  71. message: "foo",
  72. metadata: ["metadata": "initial"],
  73. trailingMetadata: ["metadata": "trailing"]
  74. )
  75. let stream = StreamingServerResponse(single: single)
  76. let (messages, continuation) = AsyncStream.makeStream(of: String.self)
  77. let trailingMetadata: Metadata
  78. switch stream.accepted {
  79. case .success(let contents):
  80. trailingMetadata = try await contents.producer(.gathering(into: continuation))
  81. continuation.finish()
  82. case .failure(let error):
  83. throw error
  84. }
  85. #expect(stream.metadata == ["metadata": "initial"])
  86. let collected = try await messages.collect()
  87. #expect(collected == ["foo"])
  88. #expect(trailingMetadata == ["metadata": "trailing"])
  89. }
  90. @Test("StreamingServerResponse(single:) (rejected)")
  91. func singleToStreamConversionForFailedResponse() async throws {
  92. let error = RPCError(code: .aborted, message: "aborted")
  93. let single = ServerResponse(of: String.self, error: error)
  94. let stream = StreamingServerResponse(single: single)
  95. switch stream.accepted {
  96. case .success:
  97. Issue.record("Expected error")
  98. case .failure(let rpcError):
  99. #expect(rpcError == error)
  100. }
  101. }
  102. @Test("Mutate metadata on response", arguments: [true, false])
  103. func mutateMetadataOnResponse(accepted: Bool) {
  104. var response: ServerResponse<String>
  105. if accepted {
  106. response = ServerResponse(message: "")
  107. } else {
  108. response = ServerResponse(error: RPCError(code: .aborted, message: ""))
  109. }
  110. response.metadata.addString("value", forKey: "key")
  111. #expect(response.metadata == ["key": "value"])
  112. }
  113. @Test("Mutate metadata on streaming response", arguments: [true, false])
  114. func mutateMetadataOnStreamingResponse(accepted: Bool) {
  115. var response: StreamingServerResponse<String>
  116. if accepted {
  117. response = StreamingServerResponse { _ in [:] }
  118. } else {
  119. response = StreamingServerResponse(error: RPCError(code: .aborted, message: ""))
  120. }
  121. response.metadata.addString("value", forKey: "key")
  122. #expect(response.metadata == ["key": "value"])
  123. }
  124. }