HeartbeatsPayloadTests.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. class HeartbeatsPayloadTests: XCTestCase {
  17. func testPayloadCurrentVersion() throws {
  18. XCTAssertEqual(HeartbeatsPayload().version, 2)
  19. }
  20. func testEmptyPayload() throws {
  21. XCTAssertEqual(
  22. HeartbeatsPayload.emptyPayload,
  23. HeartbeatsPayload(userAgentPayloads: [])
  24. )
  25. }
  26. func testDateFormatterUses_yyyy_MM_dd_Format() throws {
  27. // Given
  28. let date = Date(timeIntervalSince1970: 1_635_739_200) // 2021-11-01
  29. // When
  30. let dateString = HeartbeatsPayload.dateFormatter.string(from: date)
  31. // Then
  32. XCTAssertEqual(dateString, "2021-11-01")
  33. }
  34. func testDateFormatterDoesNotUseWeekYear() throws {
  35. // Given
  36. // The "week year" of `2018-12-31` is 2019, so we want to ensure that such
  37. // dates are formatted using the "calendar year" (2018).
  38. let date = Date(timeIntervalSince1970: 1_546_275_600) // 2018-12-31
  39. // When
  40. let dateString = HeartbeatsPayload.dateFormatter.string(from: date)
  41. // Then
  42. XCTAssertEqual(dateString, "2018-12-31")
  43. }
  44. func testEncodeAndDecode() throws {
  45. // Given
  46. let heartbeatsPayload = HeartbeatsPayload(
  47. userAgentPayloads: [
  48. .init(agent: "agent_1", dates: [Date()]),
  49. ]
  50. )
  51. // When
  52. let encodedPayload = try JSONEncoder().encode(heartbeatsPayload)
  53. let decodedPayload = try JSONDecoder().decode(HeartbeatsPayload.self, from: encodedPayload)
  54. // Then
  55. XCTAssertEqual(decodedPayload, heartbeatsPayload)
  56. }
  57. func testGetHeaderValue() throws {
  58. // Given
  59. let date1 = Date(timeIntervalSince1970: 1_635_739_200) // 2021-11-01
  60. let date2 = date1.addingTimeInterval(60 * 60 * 24) // 2021-11-02
  61. let date3 = date2.addingTimeInterval(60 * 60 * 24) // 2021-11-03
  62. let date4 = date3.addingTimeInterval(60 * 60 * 24) // 2021-11-04
  63. let date5 = date4.addingTimeInterval(60 * 60 * 24) // 2021-11-05
  64. let heartbeatsPayload = HeartbeatsPayload(
  65. userAgentPayloads: [
  66. .init(agent: "agent_1", dates: [date1, date2]),
  67. .init(agent: "agent_2", dates: [date3, date4]),
  68. .init(agent: "agent_3", dates: [date5]),
  69. ]
  70. )
  71. // When
  72. let headerValue = heartbeatsPayload.headerValue()
  73. // Then
  74. try HeartbeatLoggingTestUtils.assertEqualPayloadStrings(
  75. headerValue,
  76. """
  77. {
  78. "version": 2,
  79. "heartbeats": [
  80. {
  81. "agent": "agent_1",
  82. "dates": ["2021-11-01", "2021-11-02"]
  83. },
  84. {
  85. "agent": "agent_2",
  86. "dates": ["2021-11-03", "2021-11-04"]
  87. },
  88. {
  89. "agent": "agent_3",
  90. "dates": ["2021-11-05"]
  91. }
  92. ]
  93. }
  94. """
  95. )
  96. }
  97. func testGetHeaderValue_WhenEmpty_ReturnsEmptyString() throws {
  98. // Given
  99. let heartbeatsPayload = HeartbeatsPayload.emptyPayload
  100. // When
  101. let headerValue = heartbeatsPayload.headerValue()
  102. // Then
  103. try HeartbeatLoggingTestUtils.assertEqualPayloadStrings(
  104. headerValue,
  105. """
  106. {
  107. "version": 2,
  108. "heartbeats": []
  109. }
  110. """
  111. )
  112. }
  113. }