MetadataTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. @available(gRPCSwift 2.0, *)
  22. func initFromSequence() {
  23. let elements: [Metadata.Element] = [
  24. (key: "key1", value: "value1"),
  25. (key: "key2", value: "value2"),
  26. (key: "key3", value: "value3"),
  27. ]
  28. let metadata = Metadata(elements)
  29. let expected: Metadata = ["key1": "value1", "key2": "value2", "key3": "value3"]
  30. #expect(metadata == expected)
  31. }
  32. @Test("Add string Value")
  33. @available(gRPCSwift 2.0, *)
  34. func addStringValue() {
  35. var metadata = Metadata()
  36. #expect(metadata.isEmpty)
  37. metadata.addString("testValue", forKey: "testString")
  38. #expect(metadata.count == 1)
  39. let sequence = metadata[stringValues: "testString"]
  40. var iterator = sequence.makeIterator()
  41. #expect(iterator.next() == "testValue")
  42. #expect(iterator.next() == nil)
  43. }
  44. @Test("Add binary value")
  45. @available(gRPCSwift 2.0, *)
  46. func addBinaryValue() {
  47. var metadata = Metadata()
  48. #expect(metadata.isEmpty)
  49. metadata.addBinary(Array("base64encodedString".utf8), forKey: "testBinary-bin")
  50. #expect(metadata.count == 1)
  51. let sequence = metadata[binaryValues: "testBinary-bin"]
  52. var iterator = sequence.makeIterator()
  53. #expect(iterator.next() == Array("base64encodedString".utf8))
  54. #expect(iterator.next() == nil)
  55. }
  56. @Test("Initialize from dictionary literal")
  57. @available(gRPCSwift 2.0, *)
  58. func initFromDictionaryLiteral() {
  59. let metadata: Metadata = [
  60. "testKey": "stringValue",
  61. "testKey-bin": .binary(Array("base64encodedString".utf8)),
  62. ]
  63. #expect(metadata.count == 2)
  64. let stringSequence = metadata[stringValues: "testKey"]
  65. var stringIterator = stringSequence.makeIterator()
  66. #expect(stringIterator.next() == "stringValue")
  67. #expect(stringIterator.next() == nil)
  68. let binarySequence = metadata[binaryValues: "testKey-bin"]
  69. var binaryIterator = binarySequence.makeIterator()
  70. #expect(binaryIterator.next() == Array("base64encodedString".utf8))
  71. #expect(binaryIterator.next() == nil)
  72. }
  73. @Suite("Replace or add value")
  74. struct ReplaceOrAdd {
  75. @Suite("String")
  76. struct StringValues {
  77. @Test("Add different key")
  78. @available(gRPCSwift 2.0, *)
  79. mutating func addNewKey() async throws {
  80. var metadata: Metadata = ["key1": "value1", "key1": "value2"]
  81. metadata.replaceOrAddString("value3", forKey: "key2")
  82. #expect(Array(metadata[stringValues: "key1"]) == ["value1", "value2"])
  83. #expect(Array(metadata[stringValues: "key2"]) == ["value3"])
  84. #expect(metadata.count == 3)
  85. }
  86. @Test("Replace values for existing key")
  87. @available(gRPCSwift 2.0, *)
  88. mutating func replaceValues() async throws {
  89. var metadata: Metadata = ["key1": "value1", "key1": "value2"]
  90. metadata.replaceOrAddString("value3", forKey: "key1")
  91. #expect(Array(metadata[stringValues: "key1"]) == ["value3"])
  92. #expect(metadata.count == 1)
  93. }
  94. }
  95. @Suite("Binary")
  96. struct BinaryValues {
  97. @Test("Add different key")
  98. @available(gRPCSwift 2.0, *)
  99. mutating func addNewKey() async throws {
  100. var metadata: Metadata = ["key1-bin": [0], "key1-bin": [1]]
  101. metadata.replaceOrAddBinary([2], forKey: "key2-bin")
  102. #expect(Array(metadata[binaryValues: "key1-bin"]) == [[0], [1]])
  103. #expect(Array(metadata[binaryValues: "key2-bin"]) == [[2]])
  104. #expect(metadata.count == 3)
  105. }
  106. @Test("Replace values for existing key")
  107. @available(gRPCSwift 2.0, *)
  108. mutating func replaceValues() async throws {
  109. var metadata: Metadata = ["key1-bin": [0], "key1-bin": [1]]
  110. metadata.replaceOrAddBinary([2], forKey: "key1-bin")
  111. #expect(Array(metadata[binaryValues: "key1-bin"]) == [[2]])
  112. #expect(metadata.count == 1)
  113. }
  114. }
  115. }
  116. @Test("Reserve more capacity increases capacity")
  117. @available(gRPCSwift 2.0, *)
  118. func reserveMoreCapacity() {
  119. var metadata = Metadata()
  120. #expect(metadata.capacity == 0)
  121. metadata.reserveCapacity(10)
  122. #expect(metadata.capacity == 10)
  123. }
  124. @Test("Reserve less capacity doesn't reduce capacity")
  125. @available(gRPCSwift 2.0, *)
  126. func reserveCapacity() {
  127. var metadata = Metadata()
  128. #expect(metadata.capacity == 0)
  129. metadata.reserveCapacity(10)
  130. #expect(metadata.capacity == 10)
  131. metadata.reserveCapacity(0)
  132. #expect(metadata.capacity == 10)
  133. }
  134. @Test("Iterate over all values for a key")
  135. @available(gRPCSwift 2.0, *)
  136. func iterateOverValuesForKey() {
  137. let metadata: Metadata = [
  138. "key-bin": "1",
  139. "key-bin": [1],
  140. "key-bin": "2",
  141. "key-bin": [2],
  142. "key-bin": "3",
  143. "key-bin": [3],
  144. ]
  145. #expect(Array(metadata["key-bin"]) == ["1", [1], "2", [2], "3", [3]])
  146. }
  147. @Test("Iterate over string values for a key")
  148. @available(gRPCSwift 2.0, *)
  149. func iterateOverStringsForKey() {
  150. let metadata: Metadata = [
  151. "key-bin": "1",
  152. "key-bin": [1],
  153. "key-bin": "2",
  154. "key-bin": [2],
  155. "key-bin": "3",
  156. "key-bin": [3],
  157. ]
  158. #expect(Array(metadata[stringValues: "key-bin"]) == ["1", "2", "3"])
  159. }
  160. @Test("Iterate over binary values for a key")
  161. @available(gRPCSwift 2.0, *)
  162. func iterateOverBinaryForKey() {
  163. let metadata: Metadata = [
  164. "key-bin": "1",
  165. "key-bin": [1],
  166. "key-bin": "2",
  167. "key-bin": [2],
  168. "key-bin": "3",
  169. "key-bin": [3],
  170. ]
  171. #expect(Array(metadata[binaryValues: "key-bin"]) == [[1], [2], [3]])
  172. }
  173. @Test("Iterate over base64 encoded binary values for a key")
  174. @available(gRPCSwift 2.0, *)
  175. func iterateOverBase64BinaryEncodedValuesForKey() {
  176. let metadata: Metadata = [
  177. "key-bin": "c3RyaW5nMQ==",
  178. "key-bin": .binary(.init("data1".utf8)),
  179. "key-bin": "c3RyaW5nMg==",
  180. "key-bin": .binary(.init("data2".utf8)),
  181. "key-bin": "c3RyaW5nMw==",
  182. "key-bin": .binary(.init("data3".utf8)),
  183. ]
  184. let expected: [[UInt8]] = [
  185. Array("string1".utf8),
  186. Array("data1".utf8),
  187. Array("string2".utf8),
  188. Array("data2".utf8),
  189. Array("string3".utf8),
  190. Array("data3".utf8),
  191. ]
  192. #expect(Array(metadata[binaryValues: "key-bin"]) == expected)
  193. }
  194. @Test("Subscripts are case-insensitive")
  195. @available(gRPCSwift 2.0, *)
  196. func subscriptIsCaseInsensitive() {
  197. let metadata: Metadata = [
  198. "key1": "value1",
  199. "KEY2": "value2",
  200. ]
  201. #expect(Array(metadata[stringValues: "key1"]) == ["value1"])
  202. #expect(Array(metadata[stringValues: "KEY1"]) == ["value1"])
  203. #expect(Array(metadata[stringValues: "key2"]) == ["value2"])
  204. #expect(Array(metadata[stringValues: "KEY2"]) == ["value2"])
  205. }
  206. @Suite("Remove all")
  207. struct RemoveAll {
  208. @Test("Where value matches")
  209. @available(gRPCSwift 2.0, *)
  210. mutating func removeAllWhereValueMatches() async throws {
  211. var metadata: Metadata = ["key1": "value1", "key2": "value2", "key3": "value1"]
  212. metadata.removeAll { _, value in
  213. value == "value1"
  214. }
  215. #expect(metadata == ["key2": "value2"])
  216. }
  217. @Test("Where key matches")
  218. @available(gRPCSwift 2.0, *)
  219. mutating func removeAllWhereKeyMatches() async throws {
  220. var metadata: Metadata = ["key1": "value1", "key2": "value2", "key3": "value1"]
  221. metadata.removeAll { key, _ in
  222. key == "key2"
  223. }
  224. #expect(metadata == ["key1": "value1", "key3": "value1"])
  225. }
  226. }
  227. @Suite("Merge")
  228. struct Merge {
  229. @available(gRPCSwift 2.0, *)
  230. var metadata: Metadata {
  231. [
  232. "key1": "value1-1",
  233. "key2": "value2",
  234. "key3": "value3",
  235. ]
  236. }
  237. @available(gRPCSwift 2.0, *)
  238. var otherMetadata: Metadata {
  239. [
  240. "key4": "value4",
  241. "key5": "value5",
  242. ]
  243. }
  244. @Test("Where key is already present with a different value")
  245. @available(gRPCSwift 2.0, *)
  246. mutating func mergeWhereKeyIsAlreadyPresentWithDifferentValue() async throws {
  247. var otherMetadata = self.otherMetadata
  248. otherMetadata.addString("value1-2", forKey: "key1")
  249. var metadata = metadata
  250. metadata.add(contentsOf: otherMetadata)
  251. #expect(
  252. metadata == [
  253. "key1": "value1-1",
  254. "key2": "value2",
  255. "key3": "value3",
  256. "key4": "value4",
  257. "key5": "value5",
  258. "key1": "value1-2",
  259. ]
  260. )
  261. }
  262. @Test("Where key is already present with same value")
  263. @available(gRPCSwift 2.0, *)
  264. mutating func mergeWhereKeyIsAlreadyPresentWithSameValue() async throws {
  265. var otherMetadata = otherMetadata
  266. otherMetadata.addString("value1-1", forKey: "key1")
  267. var metadata = metadata
  268. metadata.add(contentsOf: otherMetadata)
  269. #expect(
  270. metadata == [
  271. "key1": "value1-1",
  272. "key2": "value2",
  273. "key3": "value3",
  274. "key4": "value4",
  275. "key5": "value5",
  276. "key1": "value1-1",
  277. ]
  278. )
  279. }
  280. @Test("Where key is not already present")
  281. @available(gRPCSwift 2.0, *)
  282. mutating func mergeWhereKeyIsNotAlreadyPresent() async throws {
  283. var metadata = self.metadata
  284. metadata.add(contentsOf: self.otherMetadata)
  285. #expect(
  286. metadata == [
  287. "key1": "value1-1",
  288. "key2": "value2",
  289. "key3": "value3",
  290. "key4": "value4",
  291. "key5": "value5",
  292. ]
  293. )
  294. }
  295. }
  296. @Suite("Description")
  297. struct Description {
  298. @available(gRPCSwift 2.0, *)
  299. var metadata: Metadata {
  300. [
  301. "key1": "value1",
  302. "key2": "value2",
  303. "key-bin": .binary([1, 2, 3]),
  304. ]
  305. }
  306. @Test("Metadata")
  307. @available(gRPCSwift 2.0, *)
  308. func describeMetadata() async throws {
  309. #expect("\(self.metadata)" == #"["key1": "value1", "key2": "value2", "key-bin": [1, 2, 3]]"#)
  310. }
  311. @Test("Metadata.Value")
  312. @available(gRPCSwift 2.0, *)
  313. func describeMetadataValue() async throws {
  314. for (key, value) in self.metadata {
  315. switch key {
  316. case "key1":
  317. #expect("\(value)" == "value1")
  318. case "key2":
  319. #expect("\(value)" == "value2")
  320. case "key-bin":
  321. #expect("\(value)" == "[1, 2, 3]")
  322. default:
  323. Issue.record("Should not have reached this point")
  324. }
  325. }
  326. }
  327. }
  328. }