|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using Moq; |
| 10 | +using NuGet.Services.Metadata.Catalog; |
| 11 | +using NuGet.Services.Metadata.Catalog.Dnx; |
| 12 | +using NuGet.Services.Metadata.Catalog.Persistence; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace CatalogTests.Dnx |
| 16 | +{ |
| 17 | + public class DnxPackageVersionIndexCacheControlTests |
| 18 | + { |
| 19 | + [Fact] |
| 20 | + public async Task LoadPackageIdsToIncludeAsync_BlobDoesNotExist() |
| 21 | + { |
| 22 | + var storage = new Mock<IStorage>(); |
| 23 | + storage.Setup(s => s.Exists(It.IsAny<string>())).Returns(false); |
| 24 | + |
| 25 | + DnxPackageVersionIndexCacheControl.PackageIdsToInclude = new HashSet<string>(); |
| 26 | + |
| 27 | + await DnxPackageVersionIndexCacheControl.LoadPackageIdsToIncludeAsync(storage.Object, Mock.Of<ILogger>(), It.IsAny<CancellationToken>()); |
| 28 | + |
| 29 | + Assert.Empty(DnxPackageVersionIndexCacheControl.PackageIdsToInclude); |
| 30 | + } |
| 31 | + |
| 32 | + [Theory] |
| 33 | + [InlineData("{\"ids\":[]}", 0)] |
| 34 | + [InlineData("{\"ids\":[\"PackageId1\",\"packageid1\"]}", 1)] |
| 35 | + [InlineData("{\"ids\":[\"PackageId1\",\"PackageId2\"]}", 2)] |
| 36 | + public async Task LoadPackageIdsToIncludeAsync_BlobExists(string json, int count) |
| 37 | + { |
| 38 | + var storage = new Mock<IStorage>(); |
| 39 | + storage.Setup(s => s.Exists(It.IsAny<string>())).Returns(true); |
| 40 | + storage.Setup(x => x.LoadStringAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>())).ReturnsAsync(json); |
| 41 | + |
| 42 | + DnxPackageVersionIndexCacheControl.PackageIdsToInclude = new HashSet<string>(); |
| 43 | + |
| 44 | + await DnxPackageVersionIndexCacheControl.LoadPackageIdsToIncludeAsync(storage.Object, Mock.Of<ILogger>(), It.IsAny<CancellationToken>()); |
| 45 | + |
| 46 | + Assert.Equal(count, DnxPackageVersionIndexCacheControl.PackageIdsToInclude.Count); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void GetCacheControl() |
| 51 | + { |
| 52 | + DnxPackageVersionIndexCacheControl.PackageIdsToInclude = new HashSet<string>() { "packageid1" }; |
| 53 | + |
| 54 | + Assert.Equal("max-age=10", DnxPackageVersionIndexCacheControl.GetCacheControl("packageid1", Mock.Of<ILogger>())); |
| 55 | + Assert.Equal(Constants.NoStoreCacheControl, DnxPackageVersionIndexCacheControl.GetCacheControl("packageid2", Mock.Of<ILogger>())); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments