|
| 1 | +using System.Net.Http.Headers; |
| 2 | +using StabilityMatrix.Core.Models.Packages; |
| 3 | + |
| 4 | +namespace StabilityMatrix.Tests.Models.Packages; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Tests that URL links on Packages should be valid. Requires internet connection. |
| 8 | +/// </summary> |
| 9 | +[TestClass] |
| 10 | +public sealed class PackageLinkTests |
| 11 | +{ |
| 12 | + private static HttpClient HttpClient { get; } = |
| 13 | + new() { DefaultRequestHeaders = { { "User-Agent", "StabilityMatrix/2.0" } } }; |
| 14 | + |
| 15 | + private static IEnumerable<object[]> PackagesData => |
| 16 | + PackageHelper.GetPackages().Select(p => new object[] { p }); |
| 17 | + |
| 18 | + [TestMethod] |
| 19 | + [DynamicData(nameof(PackagesData))] |
| 20 | + public async Task TestPreviewImageUri(BasePackage package) |
| 21 | + { |
| 22 | + var imageUri = package.PreviewImageUri; |
| 23 | + |
| 24 | + // Test http head is successful |
| 25 | + var response = await HttpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, imageUri)); |
| 26 | + |
| 27 | + Assert.IsTrue( |
| 28 | + response.IsSuccessStatusCode, |
| 29 | + "Failed to get PreviewImageUri at {0}: {1}", |
| 30 | + imageUri, |
| 31 | + response |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + [TestMethod] |
| 36 | + [DynamicData(nameof(PackagesData))] |
| 37 | + public async Task TestLicenseUrl(BasePackage package) |
| 38 | + { |
| 39 | + if (string.IsNullOrEmpty(package.LicenseUrl)) |
| 40 | + { |
| 41 | + Assert.Inconclusive(); |
| 42 | + } |
| 43 | + |
| 44 | + var licenseUri = package.LicenseUrl; |
| 45 | + |
| 46 | + // Test http head is successful |
| 47 | + var response = await HttpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, licenseUri)); |
| 48 | + |
| 49 | + Assert.IsTrue( |
| 50 | + response.IsSuccessStatusCode, |
| 51 | + "Failed to get LicenseUrl at {0}: {1}", |
| 52 | + licenseUri, |
| 53 | + response |
| 54 | + ); |
| 55 | + } |
| 56 | +} |
0 commit comments