diff --git a/src/FilterLists.Services/Snapshot/SnapshotWayback.cs b/src/FilterLists.Services/Snapshot/SnapshotWayback.cs index 6543e10b8..7070e03c6 100644 --- a/src/FilterLists.Services/Snapshot/SnapshotWayback.cs +++ b/src/FilterLists.Services/Snapshot/SnapshotWayback.cs @@ -27,7 +27,7 @@ public override async Task TrySaveAsync() private async Task UpdateWaybackData() { - var snapshotMeta = await WaybackService.GetMostRecentSnapshotMeta(ListUrl); + var snapshotMeta = await WaybackService.GetMostRecentSnapshotMetaAsync(ListUrl); if (snapshotMeta != null) { ListUrl = SnapEntity.WaybackUrl = snapshotMeta.UrlRaw; diff --git a/src/FilterLists.Services/Wayback/WaybackService.cs b/src/FilterLists.Services/Wayback/WaybackService.cs index 0276f7d03..e5755e9eb 100644 --- a/src/FilterLists.Services/Wayback/WaybackService.cs +++ b/src/FilterLists.Services/Wayback/WaybackService.cs @@ -11,7 +11,7 @@ public static class WaybackService { private const string WaybackAvailabilityApiUrlPrefix = "https://archive.org/wayback/available?url="; - public static async Task GetMostRecentSnapshotMeta(string url) + public static async Task GetMostRecentSnapshotMetaAsync(string url) { var closest = (await GetWaybackAvailability(url))?.ArchivedSnapshots?.Closest; return closest != null diff --git a/tests/FilterLists.Services.Tests/Wayback/GetMostRecentSnapshotMetaAsyncShould.cs b/tests/FilterLists.Services.Tests/Wayback/GetMostRecentSnapshotMetaAsyncShould.cs new file mode 100644 index 000000000..8929a7286 --- /dev/null +++ b/tests/FilterLists.Services.Tests/Wayback/GetMostRecentSnapshotMetaAsyncShould.cs @@ -0,0 +1,39 @@ +using System; +using System.Net.Http; +using FilterLists.Services.Wayback; +using Xunit; + +namespace FilterLists.Services.Tests.Wayback +{ + public class GetMostRecentSnapshotMetaAsyncShould + { + [Fact] + public async void ReturnAccessibleUrlRawIfInWaybackMachine() + { + const string url = "https://github.com/collinbarrett/FilterLists"; + var meta = await WaybackService.GetMostRecentSnapshotMetaAsync(url); + using (var client = new HttpClient()) + { + var response = await client.GetAsync(meta.UrlRaw); + Assert.True(response.IsSuccessStatusCode); + } + } + + [Fact] + public async void ReturnNullIfUrlNotInWaybackMachine() + { + const string url = "https://raw.githubusercontent.com/collinbarrett/FilterLists/master/doesnotexist"; + var meta = await WaybackService.GetMostRecentSnapshotMetaAsync(url); + Assert.Null(meta); + } + + [Fact] + public async void ReturnRecentTimestampUtcIfInWaybackMachine() + { + const string url = "https://github.com/collinbarrett/FilterLists"; + var meta = await WaybackService.GetMostRecentSnapshotMetaAsync(url); + Assert.True(meta.TimestampUtc > DateTime.Now.AddYears(-5).ToUniversalTime() && + meta.TimestampUtc < DateTime.UtcNow); + } + } +} \ No newline at end of file