MetadataTests.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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("Metadata")
  19. struct MetadataTests {
  20. @Test("Initialize from Sequence")
  21. func initFromSequence() {
  22. let elements: [Metadata.Element] = [
  23. (key: "key1", value: "value1"),
  24. (key: "key2", value: "value2"),
  25. (key: "key3", value: "value3"),
  26. ]
  27. let metadata = Metadata(elements)
  28. let expected: Metadata = ["key1": "value1", "key2": "value2", "key3": "value3"]
  29. #expect(metadata == expected)
  30. }
  31. @Test("Add string Value")
  32. func addStringValue() {
  33. var metadata = Metadata()
  34. #expect(metadata.isEmpty)
  35. metadata.addString("testValue", forKey: "testString")
  36. #expect(metadata.count == 1)
  37. let sequence = metadata[stringValues: "testString"]
  38. var iterator = sequence.makeIterator()
  39. #expect(iterator.next() == "testValue")
  40. #expect(iterator.next() == nil)
  41. }
  42. @Test("Add binary value")
  43. func addBinaryValue() {
  44. var metadata = Metadata()
  45. #expect(metadata.isEmpty)
  46. metadata.addBinary(Array("base64encodedString".utf8), forKey: "testBinary-bin")
  47. #expect(metadata.count == 1)
  48. let sequence = metadata[binaryValues: "testBinary-bin"]
  49. var iterator = sequence.makeIterator()
  50. #expect(iterator.next() == Array("base64encodedString".utf8))
  51. #expect(iterator.next() == nil)
  52. }
  53. @Test("Initialize from dictionary literal")
  54. func initFromDictionaryLiteral() {
  55. let metadata: Metadata = [
  56. "testKey": "stringValue",
  57. "testKey-bin": .binary(Array("base64encodedString".utf8)),
  58. ]
  59. #expect(metadata.count == 2)
  60. let stringSequence = metadata[stringValues: "testKey"]
  61. var stringIterator = stringSequence.makeIterator()
  62. #expect(stringIterator.next() == "stringValue")
  63. #expect(stringIterator.next() == nil)
  64. let binarySequence = metadata[binaryValues: "testKey-bin"]
  65. var binaryIterator = binarySequence.makeIterator()
  66. #expect(binaryIterator.next() == Array("base64encodedString".utf8))
  67. #expect(binaryIterator.next() == nil)
  68. }
  69. @Suite("Replace or add value")
  70. struct ReplaceOrAdd {
  71. @Suite("String")
  72. struct StringValues {
  73. var metadata: Metadata = [
  74. "key1": "value1",
  75. "key1": "value2",
  76. ]
  77. @Test("Add different key")
  78. mutating func addNewKey() async throws {
  79. self.metadata.replaceOrAddString("value3", forKey: "key2")
  80. #expect(Array(self.metadata[stringValues: "key1"]) == ["value1", "value2"])
  81. #expect(Array(self.metadata[stringValues: "key2"]) == ["value3"])
  82. #expect(self.metadata.count == 3)
  83. }
  84. @Test("Replace values for existing key")
  85. mutating func replaceValues() async throws {
  86. self.metadata.replaceOrAddString("value3", forKey: "key1")
  87. #expect(Array(self.metadata[stringValues: "key1"]) == ["value3"])
  88. #expect(self.metadata.count == 1)
  89. }
  90. }
  91. @Suite("Binary")
  92. struct BinaryValues {
  93. var metadata: Metadata = [
  94. "key1-bin": [0],
  95. "key1-bin": [1],
  96. ]
  97. @Test("Add different key")
  98. mutating func addNewKey() async throws {
  99. self.metadata.replaceOrAddBinary([2], forKey: "key2-bin")
  100. #expect(Array(self.metadata[binaryValues: "key1-bin"]) == [[0], [1]])
  101. #expect(Array(self.metadata[binaryValues: "key2-bin"]) == [[2]])
  102. #expect(self.metadata.count == 3)
  103. }
  104. @Test("Replace values for existing key")
  105. mutating func replaceValues() async throws {
  106. self.metadata.replaceOrAddBinary([2], forKey: "key1-bin")
  107. #expect(Array(self.metadata[binaryValues: "key1-bin"]) == [[2]])
  108. #expect(self.metadata.count == 1)
  109. }
  110. }
  111. }
  112. @Test("Reserve more capacity increases capacity")
  113. func reserveMoreCapacity() {
  114. var metadata = Metadata()
  115. #expect(metadata.capacity == 0)
  116. metadata.reserveCapacity(10)
  117. #expect(metadata.capacity == 10)
  118. }
  119. @Test("Reserve less capacity doesn't reduce capacity")
  120. func reserveCapacity() {
  121. var metadata = Metadata()
  122. #expect(metadata.capacity == 0)
  123. metadata.reserveCapacity(10)
  124. #expect(metadata.capacity == 10)
  125. metadata.reserveCapacity(0)
  126. #expect(metadata.capacity == 10)
  127. }
  128. @Test("Iterate over all values for a key")
  129. func iterateOverValuesForKey() {
  130. let metadata: Metadata = [
  131. "key-bin": "1",
  132. "key-bin": [1],
  133. "key-bin": "2",
  134. "key-bin": [2],
  135. "key-bin": "3",
  136. "key-bin": [3],
  137. ]
  138. #expect(Array(metadata["key-bin"]) == ["1", [1], "2", [2], "3", [3]])
  139. }
  140. @Test("Iterate over string values for a key")
  141. func iterateOverStringsForKey() {
  142. let metadata: Metadata = [
  143. "key-bin": "1",
  144. "key-bin": [1],
  145. "key-bin": "2",
  146. "key-bin": [2],
  147. "key-bin": "3",
  148. "key-bin": [3],
  149. ]
  150. #expect(Array(metadata[stringValues: "key-bin"]) == ["1", "2", "3"])
  151. }
  152. @Test("Iterate over binary values for a key")
  153. func iterateOverBinaryForKey() {
  154. let metadata: Metadata = [
  155. "key-bin": "1",
  156. "key-bin": [1],
  157. "key-bin": "2",
  158. "key-bin": [2],
  159. "key-bin": "3",
  160. "key-bin": [3],
  161. ]
  162. #expect(Array(metadata[binaryValues: "key-bin"]) == [[1], [2], [3]])
  163. }
  164. @Test("Iterate over base64 encoded binary values for a key")
  165. func iterateOverBase64BinaryEncodedValuesForKey() {
  166. let metadata: Metadata = [
  167. "key-bin": "c3RyaW5nMQ==",
  168. "key-bin": .binary(.init("data1".utf8)),
  169. "key-bin": "c3RyaW5nMg==",
  170. "key-bin": .binary(.init("data2".utf8)),
  171. "key-bin": "c3RyaW5nMw==",
  172. "key-bin": .binary(.init("data3".utf8)),
  173. ]
  174. let expected: [[UInt8]] = [
  175. Array("string1".utf8),
  176. Array("data1".utf8),
  177. Array("string2".utf8),
  178. Array("data2".utf8),
  179. Array("string3".utf8),
  180. Array("data3".utf8),
  181. ]
  182. #expect(Array(metadata[binaryValues: "key-bin"]) == expected)
  183. }
  184. @Test("Subscripts are case-insensitive")
  185. func subscriptIsCaseInsensitive() {
  186. let metadata: Metadata = [
  187. "key1": "value1",
  188. "KEY2": "value2",
  189. ]
  190. #expect(Array(metadata[stringValues: "key1"]) == ["value1"])
  191. #expect(Array(metadata[stringValues: "KEY1"]) == ["value1"])
  192. #expect(Array(metadata[stringValues: "key2"]) == ["value2"])
  193. #expect(Array(metadata[stringValues: "KEY2"]) == ["value2"])
  194. }
  195. @Suite("Remove all")
  196. struct RemoveAll {
  197. var metadata: Metadata = [
  198. "key1": "value1",
  199. "key2": "value2",
  200. "key3": "value1",
  201. ]
  202. @Test("Where value matches")
  203. mutating func removeAllWhereValueMatches() async throws {
  204. self.metadata.removeAll { _, value in
  205. value == "value1"
  206. }
  207. #expect(self.metadata == ["key2": "value2"])
  208. }
  209. @Test("Where key matches")
  210. mutating func removeAllWhereKeyMatches() async throws {
  211. self.metadata.removeAll { key, _ in
  212. key == "key2"
  213. }
  214. #expect(self.metadata == ["key1": "value1", "key3": "value1"])
  215. }
  216. }
  217. }