Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5474 from XiangRongLin/expires_header
Respect expires header when checking for new versions
- Loading branch information
Showing
4 changed files
with
143 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.schabi.newpipe | ||
|
||
import java.time.Instant | ||
import java.time.ZonedDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
class NewVersionManager { | ||
|
||
fun isExpired(expiry: Long): Boolean { | ||
return Instant.ofEpochSecond(expiry).isBefore(Instant.now()) | ||
} | ||
|
||
/** | ||
* Coerce expiry date time in between 6 hours and 72 hours from now | ||
* | ||
* @return Epoch second of expiry date time | ||
*/ | ||
fun coerceExpiry(expiryString: String?): Long { | ||
val now = ZonedDateTime.now() | ||
return expiryString?.let { | ||
|
||
var expiry = ZonedDateTime.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(expiryString)) | ||
expiry = maxOf(expiry, now.plusHours(6)) | ||
expiry = minOf(expiry, now.plusHours(72)) | ||
expiry.toEpochSecond() | ||
} ?: now.plusHours(6).toEpochSecond() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/src/test/java/org/schabi/newpipe/NewVersionManagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.schabi.newpipe | ||
|
||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
import java.time.Instant | ||
import java.time.ZoneId | ||
import java.time.format.DateTimeFormatter | ||
import kotlin.math.abs | ||
|
||
class NewVersionManagerTest { | ||
|
||
private lateinit var manager: NewVersionManager | ||
|
||
@Before | ||
fun setup() { | ||
manager = NewVersionManager() | ||
} | ||
|
||
@Test | ||
fun `Expiry is reached`() { | ||
val oneHourEarlier = Instant.now().atZone(ZoneId.of("GMT")).minusHours(1) | ||
|
||
val expired = manager.isExpired(oneHourEarlier.toEpochSecond()) | ||
|
||
assertTrue(expired) | ||
} | ||
|
||
@Test | ||
fun `Expiry is not reached`() { | ||
val oneHourLater = Instant.now().atZone(ZoneId.of("GMT")).plusHours(1) | ||
|
||
val expired = manager.isExpired(oneHourLater.toEpochSecond()) | ||
|
||
assertFalse(expired) | ||
} | ||
|
||
/** | ||
* Equal within a range of 5 seconds | ||
*/ | ||
private fun assertNearlyEqual(a: Long, b: Long) { | ||
assertTrue(abs(a - b) < 5) | ||
} | ||
|
||
@Test | ||
fun `Expiry must be returned as is because it is inside the acceptable range of 6-72 hours`() { | ||
val sixHoursLater = Instant.now().atZone(ZoneId.of("GMT")).plusHours(6) | ||
|
||
val coerced = manager.coerceExpiry(DateTimeFormatter.RFC_1123_DATE_TIME.format(sixHoursLater)) | ||
|
||
assertNearlyEqual(sixHoursLater.toEpochSecond(), coerced) | ||
} | ||
|
||
@Test | ||
fun `Expiry must be increased to 6 hours if below`() { | ||
val tooLow = Instant.now().atZone(ZoneId.of("GMT")).plusHours(5) | ||
|
||
val coerced = manager.coerceExpiry(DateTimeFormatter.RFC_1123_DATE_TIME.format(tooLow)) | ||
|
||
assertNearlyEqual(tooLow.plusHours(1).toEpochSecond(), coerced) | ||
} | ||
|
||
@Test | ||
fun `Expiry must be decreased to 72 hours if above`() { | ||
val tooHigh = Instant.now().atZone(ZoneId.of("GMT")).plusHours(73) | ||
|
||
val coerced = manager.coerceExpiry(DateTimeFormatter.RFC_1123_DATE_TIME.format(tooHigh)) | ||
|
||
assertNearlyEqual(tooHigh.minusHours(1).toEpochSecond(), coerced) | ||
} | ||
} |