|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.SemanticKernel.Plugins.Web; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace SemanticKernel.Plugins.UnitTests.Web; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Unit tests for <see cref="WebFileDownloadPlugin"/> |
| 14 | +/// </summary> |
| 15 | +public sealed class WebFileDownloadPluginTests : IDisposable |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="WebFileDownloadPluginTests"/> class. |
| 19 | + /// </summary> |
| 20 | + public WebFileDownloadPluginTests() |
| 21 | + { |
| 22 | + this._messageHandlerStub = new MultipleHttpMessageHandlerStub(); |
| 23 | + this._httpClient = new HttpClient(this._messageHandlerStub, disposeHandler: false); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public async Task DownloadToFileSucceedsAsync() |
| 28 | + { |
| 29 | + // Arrange |
| 30 | + this._messageHandlerStub.AddImageResponse(File.ReadAllBytes(SKLogoPng)); |
| 31 | + var uri = new Uri("https://raw.githubusercontent.com/microsoft/semantic-kernel/refs/heads/main/docs/images/sk_logo.png"); |
| 32 | + var folderPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 33 | + var filePath = Path.Combine(folderPath, "sk_logo.png"); |
| 34 | + Directory.CreateDirectory(folderPath); |
| 35 | + |
| 36 | + var webFileDownload = new WebFileDownloadPlugin() |
| 37 | + { |
| 38 | + AllowedDomains = ["raw.githubusercontent.com"], |
| 39 | + AllowedFolders = [folderPath] |
| 40 | + }; |
| 41 | + |
| 42 | + try |
| 43 | + { |
| 44 | + // Act |
| 45 | + await webFileDownload.DownloadToFileAsync(uri, filePath); |
| 46 | + |
| 47 | + // Assert |
| 48 | + Assert.True(Path.Exists(filePath)); |
| 49 | + } |
| 50 | + finally |
| 51 | + { |
| 52 | + if (Path.Exists(folderPath)) |
| 53 | + { |
| 54 | + Directory.Delete(folderPath, true); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public async Task DownloadToFileFailsForInvalidDomainAsync() |
| 61 | + { |
| 62 | + // Arrange |
| 63 | + this._messageHandlerStub.AddImageResponse(File.ReadAllBytes(SKLogoPng)); |
| 64 | + var uri = new Uri("https://raw.githubfakecontent.com/microsoft/semantic-kernel/refs/heads/main/docs/images/sk_logo.png"); |
| 65 | + var folderPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 66 | + var filePath = Path.Combine(folderPath, "sk_logo.png"); |
| 67 | + Directory.CreateDirectory(folderPath); |
| 68 | + |
| 69 | + var webFileDownload = new WebFileDownloadPlugin() |
| 70 | + { |
| 71 | + AllowedDomains = ["raw.githubusercontent.com"], |
| 72 | + AllowedFolders = [folderPath] |
| 73 | + }; |
| 74 | + |
| 75 | + // Act & Assert |
| 76 | + await Assert.ThrowsAsync<InvalidOperationException>(async () => await webFileDownload.DownloadToFileAsync(uri, filePath)); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public void ValidatePluginProperties() |
| 81 | + { |
| 82 | + // Arrange |
| 83 | + var folderPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 84 | + var filePath = Path.Combine(Path.GetTempPath(), "sk_logo.png"); |
| 85 | + |
| 86 | + // Act |
| 87 | + var webFileDownload = new WebFileDownloadPlugin() |
| 88 | + { |
| 89 | + AllowedDomains = ["raw.githubusercontent.com"], |
| 90 | + AllowedFolders = [folderPath], |
| 91 | + MaximumDownloadSize = 100, |
| 92 | + DisableFileOverwrite = true |
| 93 | + }; |
| 94 | + |
| 95 | + // Act & Assert |
| 96 | + Assert.Equal(["raw.githubusercontent.com"], webFileDownload.AllowedDomains); |
| 97 | + Assert.Equal([folderPath], webFileDownload.AllowedFolders); |
| 98 | + Assert.Equal(100, webFileDownload.MaximumDownloadSize); |
| 99 | + Assert.True(webFileDownload.DisableFileOverwrite); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public async Task DownloadToFileFailsForInvalidParametersAsync() |
| 104 | + { |
| 105 | + // Arrange |
| 106 | + this._messageHandlerStub.AddImageResponse(File.ReadAllBytes(SKLogoPng)); |
| 107 | + var validUri = new Uri("https://raw.githubusercontent.com/microsoft/semantic-kernel/refs/heads/main/docs/images/sk_logo.png"); |
| 108 | + var invalidUri = new Uri("https://raw.githubfakecontent.com/microsoft/semantic-kernel/refs/heads/main/docs/images/sk_logo.png"); |
| 109 | + var folderPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 110 | + var validFilePath = Path.Combine(folderPath, "sk_logo.png"); |
| 111 | + var invalidFilePath = Path.Combine(Path.GetTempPath(), "sk_logo.png"); |
| 112 | + Directory.CreateDirectory(folderPath); |
| 113 | + |
| 114 | + var webFileDownload = new WebFileDownloadPlugin() |
| 115 | + { |
| 116 | + AllowedDomains = ["raw.githubusercontent.com"], |
| 117 | + AllowedFolders = [folderPath], |
| 118 | + MaximumDownloadSize = 100 |
| 119 | + }; |
| 120 | + |
| 121 | + // Act & Assert |
| 122 | + await Assert.ThrowsAsync<InvalidOperationException>(async () => await webFileDownload.DownloadToFileAsync(validUri, validFilePath)); |
| 123 | + await Assert.ThrowsAsync<InvalidOperationException>(async () => await webFileDownload.DownloadToFileAsync(invalidUri, validFilePath)); |
| 124 | + await Assert.ThrowsAsync<InvalidOperationException>(async () => await webFileDownload.DownloadToFileAsync(validUri, invalidFilePath)); |
| 125 | + await Assert.ThrowsAsync<ArgumentException>(async () => await webFileDownload.DownloadToFileAsync(validUri, "\\\\UNC\\server\\folder\\myfile.txt")); |
| 126 | + await Assert.ThrowsAsync<ArgumentException>(async () => await webFileDownload.DownloadToFileAsync(validUri, "")); |
| 127 | + await Assert.ThrowsAsync<ArgumentException>(async () => await webFileDownload.DownloadToFileAsync(validUri, "myfile.txt")); |
| 128 | + } |
| 129 | + |
| 130 | + /// <inheritdoc/> |
| 131 | + public void Dispose() |
| 132 | + { |
| 133 | + this._messageHandlerStub.Dispose(); |
| 134 | + this._httpClient.Dispose(); |
| 135 | + GC.SuppressFinalize(this); |
| 136 | + } |
| 137 | + |
| 138 | + #region private |
| 139 | + private const string SKLogoPng = "./TestData/sk_logo.png"; |
| 140 | + |
| 141 | + private readonly MultipleHttpMessageHandlerStub _messageHandlerStub; |
| 142 | + private readonly HttpClient _httpClient; |
| 143 | + #endregion |
| 144 | +} |
0 commit comments