// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. #nullable disable using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using NuGet.Common; using NuGet.Configuration; using NuGet.Protocol; using NuGet.Protocol.Core.Types; namespace Test.Utility { /// /// HttpSource with file caching disabled /// public class TestHttpSource : HttpSource { public TestHttpSource(PackageSource source, Dictionary responses, string errorContent = "", bool throwOperationCancelledException = false) : base( source, () => Task.FromResult( new TestHttpHandler( new TestMessageHandler(responses, errorContent))), NullThrottle.Instance) { _throwOperationCancelledException = throwOperationCancelledException; } public TestHttpSource(PackageSource source, Dictionary>> responses, bool throwOperationCancelledException = false) : base( source, () => Task.FromResult( new TestHttpHandler(new TestMessageHandler(responses))), NullThrottle.Instance) { _throwOperationCancelledException = throwOperationCancelledException; } /// /// Modify or wrap the returned stream. /// By default do nothing. /// public Func StreamWrapper { get; set; } = (stream) => stream; public Action HttpSourceCachedRequestInspector { get; set; } public bool DisableCaching { get; set; } = true; public int CacheHits { get; private set; } public int CacheMisses { get; private set; } private readonly bool _throwOperationCancelledException; public override Task GetAsync(HttpSourceCachedRequest request, Func> processAsync, ILogger log, CancellationToken token) { if (_throwOperationCancelledException) { throw new OperationCanceledException(token); } HttpSourceCachedRequestInspector?.Invoke(request); return base.GetAsync(request, processAsync, log, token); } protected override Stream TryReadCacheFile(string uri, TimeSpan maxAge, string cacheFile) { if (DisableCaching) { return null; } var result = base.TryReadCacheFile(uri, maxAge, cacheFile); if (result == null) { CacheMisses++; } else { CacheHits++; } if (result != null) { result = StreamWrapper(result); } return result; } } }