HeartbeatsPayloadTests.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import XCTest
  15. @testable import FirebaseCoreInternal
  16. import HeartbeatLoggingTestUtils
  17. class HeartbeatsPayloadTests: XCTestCase {
  18. func testPayloadCurrentVersion() throws {
  19. XCTAssertEqual(HeartbeatsPayload().version, 2)
  20. }
  21. func testEmptyPayload() throws {
  22. XCTAssertEqual(
  23. HeartbeatsPayload.emptyPayload,
  24. HeartbeatsPayload(userAgentPayloads: [])
  25. )
  26. }
  27. func testDateFormatterUses_YYYY_MM_dd_Format() throws {
  28. // Given
  29. let date = Date(timeIntervalSince1970: 1_635_739_200) // 2021-11-01
  30. // When
  31. let dateString = HeartbeatsPayload.dateFormatter.string(from: date)
  32. // Then
  33. XCTAssertEqual(dateString, "2021-11-01")
  34. }
  35. func testEncodeAndDecode() throws {
  36. // Given
  37. let heartbeatsPayload = HeartbeatsPayload(
  38. userAgentPayloads: [
  39. .init(agent: "agent_1", dates: [Date()]),
  40. ]
  41. )
  42. // When
  43. let encodedPayload = try JSONEncoder().encode(heartbeatsPayload)
  44. let decodedPayload = try JSONDecoder().decode(HeartbeatsPayload.self, from: encodedPayload)
  45. // Then
  46. XCTAssertEqual(decodedPayload, heartbeatsPayload)
  47. }
  48. func testGetHeaderValue() throws {
  49. // Given
  50. let date1 = Date(timeIntervalSince1970: 1_635_739_200) // 2021-11-01
  51. let date2 = date1.addingTimeInterval(60 * 60 * 24) // 2021-11-02
  52. let date3 = date2.addingTimeInterval(60 * 60 * 24) // 2021-11-03
  53. let date4 = date3.addingTimeInterval(60 * 60 * 24) // 2021-11-04
  54. let date5 = date4.addingTimeInterval(60 * 60 * 24) // 2021-11-05
  55. let heartbeatsPayload = HeartbeatsPayload(
  56. userAgentPayloads: [
  57. .init(agent: "agent_1", dates: [date1, date2]),
  58. .init(agent: "agent_2", dates: [date3, date4]),
  59. .init(agent: "agent_3", dates: [date5]),
  60. ]
  61. )
  62. // When
  63. let headerValue = heartbeatsPayload.headerValue()
  64. // Then
  65. try HeartbeatLoggingTestUtils.assertEqualPayloadStrings(
  66. headerValue,
  67. """
  68. {
  69. "version": 2,
  70. "heartbeats": [
  71. {
  72. "agent": "agent_1",
  73. "dates": ["2021-11-01", "2021-11-02"]
  74. },
  75. {
  76. "agent": "agent_2",
  77. "dates": ["2021-11-03", "2021-11-04"]
  78. },
  79. {
  80. "agent": "agent_3",
  81. "dates": ["2021-11-05"]
  82. }
  83. ]
  84. }
  85. """
  86. )
  87. }
  88. func testGetHeaderValue_WhenEmpty_ReturnsEmptyString() throws {
  89. // Given
  90. let heartbeatsPayload = HeartbeatsPayload.emptyPayload
  91. // When
  92. let headerValue = heartbeatsPayload.headerValue()
  93. // Then
  94. try HeartbeatLoggingTestUtils.assertEqualPayloadStrings(
  95. headerValue,
  96. """
  97. {
  98. "version": 2,
  99. "heartbeats": []
  100. }
  101. """
  102. )
  103. }
  104. }