MetadataTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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("Iterate over unpadded base64 encoded binary values for a key")
  195. @available(gRPCSwift 2.0, *)
  196. func iterateOverUnpaddedBase64BinaryEncodedValuesForKey() {
  197. let metadata: Metadata = [
  198. "key-bin": "YQ==",
  199. "key-bin": "YQ",
  200. ]
  201. let expected: [[UInt8]] = [[UInt8(ascii: "a")], [UInt8(ascii: "a")]]
  202. #expect(Array(metadata[binaryValues: "key-bin"]) == expected)
  203. }
  204. @Test("Subscripts are case-insensitive")
  205. @available(gRPCSwift 2.0, *)
  206. func subscriptIsCaseInsensitive() {
  207. let metadata: Metadata = [
  208. "key1": "value1",
  209. "KEY2": "value2",
  210. ]
  211. #expect(Array(metadata[stringValues: "key1"]) == ["value1"])
  212. #expect(Array(metadata[stringValues: "KEY1"]) == ["value1"])
  213. #expect(Array(metadata[stringValues: "key2"]) == ["value2"])
  214. #expect(Array(metadata[stringValues: "KEY2"]) == ["value2"])
  215. }
  216. @Suite("Remove all")
  217. struct RemoveAll {
  218. @Test("Where value matches")
  219. @available(gRPCSwift 2.0, *)
  220. mutating func removeAllWhereValueMatches() async throws {
  221. var metadata: Metadata = ["key1": "value1", "key2": "value2", "key3": "value1"]
  222. metadata.removeAll { _, value in
  223. value == "value1"
  224. }
  225. #expect(metadata == ["key2": "value2"])
  226. }
  227. @Test("Where key matches")
  228. @available(gRPCSwift 2.0, *)
  229. mutating func removeAllWhereKeyMatches() async throws {
  230. var metadata: Metadata = ["key1": "value1", "key2": "value2", "key3": "value1"]
  231. metadata.removeAll { key, _ in
  232. key == "key2"
  233. }
  234. #expect(metadata == ["key1": "value1", "key3": "value1"])
  235. }
  236. }
  237. @Suite("Merge")
  238. struct Merge {
  239. @available(gRPCSwift 2.0, *)
  240. var metadata: Metadata {
  241. [
  242. "key1": "value1-1",
  243. "key2": "value2",
  244. "key3": "value3",
  245. ]
  246. }
  247. @available(gRPCSwift 2.0, *)
  248. var otherMetadata: Metadata {
  249. [
  250. "key4": "value4",
  251. "key5": "value5",
  252. ]
  253. }
  254. @Test("Where key is already present with a different value")
  255. @available(gRPCSwift 2.0, *)
  256. mutating func mergeWhereKeyIsAlreadyPresentWithDifferentValue() async throws {
  257. var otherMetadata = self.otherMetadata
  258. otherMetadata.addString("value1-2", forKey: "key1")
  259. var metadata = metadata
  260. metadata.add(contentsOf: otherMetadata)
  261. #expect(
  262. metadata == [
  263. "key1": "value1-1",
  264. "key2": "value2",
  265. "key3": "value3",
  266. "key4": "value4",
  267. "key5": "value5",
  268. "key1": "value1-2",
  269. ]
  270. )
  271. }
  272. @Test("Where key is already present with same value")
  273. @available(gRPCSwift 2.0, *)
  274. mutating func mergeWhereKeyIsAlreadyPresentWithSameValue() async throws {
  275. var otherMetadata = otherMetadata
  276. otherMetadata.addString("value1-1", forKey: "key1")
  277. var metadata = metadata
  278. metadata.add(contentsOf: otherMetadata)
  279. #expect(
  280. metadata == [
  281. "key1": "value1-1",
  282. "key2": "value2",
  283. "key3": "value3",
  284. "key4": "value4",
  285. "key5": "value5",
  286. "key1": "value1-1",
  287. ]
  288. )
  289. }
  290. @Test("Where key is not already present")
  291. @available(gRPCSwift 2.0, *)
  292. mutating func mergeWhereKeyIsNotAlreadyPresent() async throws {
  293. var metadata = self.metadata
  294. metadata.add(contentsOf: self.otherMetadata)
  295. #expect(
  296. metadata == [
  297. "key1": "value1-1",
  298. "key2": "value2",
  299. "key3": "value3",
  300. "key4": "value4",
  301. "key5": "value5",
  302. ]
  303. )
  304. }
  305. }
  306. @Suite("Description")
  307. struct Description {
  308. @available(gRPCSwift 2.0, *)
  309. var metadata: Metadata {
  310. [
  311. "key1": "value1",
  312. "key2": "value2",
  313. "key-bin": .binary([1, 2, 3]),
  314. ]
  315. }
  316. @Test("Metadata")
  317. @available(gRPCSwift 2.0, *)
  318. func describeMetadata() async throws {
  319. #expect("\(self.metadata)" == #"["key1": "value1", "key2": "value2", "key-bin": [1, 2, 3]]"#)
  320. }
  321. @Test("Metadata.Value")
  322. @available(gRPCSwift 2.0, *)
  323. func describeMetadataValue() async throws {
  324. for (key, value) in self.metadata {
  325. switch key {
  326. case "key1":
  327. #expect("\(value)" == "value1")
  328. case "key2":
  329. #expect("\(value)" == "value2")
  330. case "key-bin":
  331. #expect("\(value)" == "[1, 2, 3]")
  332. default:
  333. Issue.record("Should not have reached this point")
  334. }
  335. }
  336. }
  337. }
  338. }