Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Fix bug in SimpleWebSource and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Mar 18, 2022
1 parent 6c19593 commit da50aa8
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 29 deletions.
16 changes: 12 additions & 4 deletions src/Squirrel/Sources/SimpleWebSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,22 @@ public virtual async Task<ReleaseEntry[]> GetReleaseFeed(Guid? stagingId = null,
/// <inheritdoc />
public virtual Task DownloadReleaseEntry(ReleaseEntry releaseEntry, string localFile, Action<int> progress)
{
var baseUri = Utility.EnsureTrailingSlash(BaseUri);
if (releaseEntry == null) throw new ArgumentNullException(nameof(releaseEntry));
if (localFile == null) throw new ArgumentNullException(nameof(localFile));


var releaseUri = releaseEntry.BaseUrl == null
? releaseEntry.Filename
: Utility.AppendPathToUri(new Uri(releaseEntry.BaseUrl), releaseEntry.Filename).ToString();

var uri = Utility.AppendPathToUri(new Uri(releaseEntry.BaseUrl), releaseEntry.Filename).ToString();
if (!String.IsNullOrEmpty(releaseEntry.Query)) {
uri += releaseEntry.Query;
releaseUri += releaseEntry.Query;
}

var source = new Uri(baseUri, uri).AbsolutePath;
// releaseUri can be a relative url (eg. "MyPackage.nupkg") or it can be an
// absolute url (eg. "https://example.com/MyPackage.nupkg"). In the former case
var sourceBaseUri = Utility.EnsureTrailingSlash(BaseUri);
var source = new Uri(sourceBaseUri, releaseUri).ToString();

this.Log().Info($"Downloading '{releaseEntry.Filename}' from '{source}'.");
return Downloader.DownloadFile(source, localFile, progress);
Expand Down
20 changes: 1 addition & 19 deletions test/ApplyReleasesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,6 @@

namespace Squirrel.Tests
{
public class FakeUrlDownloader : Sources.IFileDownloader
{
public Task<byte[]> DownloadBytes(string url, string auth, string acc)
{
return Task.FromResult(new byte[0]);
}

public Task DownloadFile(string url, string targetFile, Action<int> progress, string auth, string acc)
{
return Task.CompletedTask;
}

public Task<string> DownloadString(string url, string auth, string acc)
{
return Task.FromResult("");
}
}

public class ApplyReleasesTests : IEnableLogger
{
[Fact]
Expand Down Expand Up @@ -481,7 +463,7 @@ public async Task CreateFullPackagesFromDeltaSmokeTest()
"Squirrel.Core.1.1.0.0-delta.nupkg"
}.ForEach(x => File.Copy(IntegrationTestHelper.GetPath("fixtures", x), Path.Combine(tempDir, "theApp", "packages", x)));

var urlDownloader = new FakeUrlDownloader();
var urlDownloader = new FakeDownloader();
var fixture = new UpdateManager.ApplyReleasesImpl(appDir);

var baseEntry = ReleaseEntry.GenerateFromFile(Path.Combine(tempDir, "theApp", "packages", "Squirrel.Core.1.0.0.0-full.nupkg"));
Expand Down
22 changes: 21 additions & 1 deletion test/CheckForUpdateTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Squirrel.Tests.TestHelpers;
using Xunit;

Expand Down Expand Up @@ -123,5 +124,24 @@ public void IfLocalAndRemoteAreEqualThenDoNothing()
{
throw new NotImplementedException();
}

[Theory]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec MyCoolApp-1.0.nupkg 1004502", "MyCoolApp", null)]
[InlineData(@"0000000000000000000000000000000000000000 https://www.test.org/Folder/MyCoolApp-1.2-delta.nupkg?query=param 1231953", "MyCoolApp", "https://www.test.org/Folder")]
public async Task WebSourceRequestsExpectedUrls(string releaseEntry, string releaseName, string baseUrl)
{
baseUrl = baseUrl ?? "https://example.com/files";
var dl = new FakeDownloader();
var source = new Sources.SimpleWebSource("https://example.com/files", dl);

dl.MockedResponseBytes = Encoding.UTF8.GetBytes(releaseEntry);
var releases = await source.GetReleaseFeed(null, null);
Assert.True(releases.Count() == 1);
Assert.Equal(releaseName, releases[0].PackageName);
Assert.Equal("https://example.com/files/RELEASES", new Uri(dl.LastUrl).GetLeftPart(UriPartial.Path));

await source.DownloadReleaseEntry(releases[0], "test", null);
Assert.Equal(baseUrl + "/" + releases[0].Filename, new Uri(dl.LastUrl).GetLeftPart(UriPartial.Path));
}
}
}
4 changes: 2 additions & 2 deletions test/ReleaseEntryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void ParseValidReleaseEntryLinesWithDots(string releaseEntry, string pack

[Theory]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec My-Cool-App-1.0-full.nupkg 1004502", "My-Cool-App")]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec My-Cool-App-1.1.nupkg 1004502", "My-Cool-App")]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec http://test.org/Folder/My-Cool-App-1.2.nupkg?query=param 1231953", "My-Cool-App")]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec My.Cool-App-1.1.nupkg 1004502", "My.Cool-App")]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec http://test.org/Folder/My.Cool-App-1.2.nupkg?query=param 1231953", "My.Cool-App")]
public void ParseValidReleaseEntryLinesWithDashes(string releaseEntry, string packageName)
{
var fixture = ReleaseEntry.ParseReleaseEntry(releaseEntry);
Expand Down
42 changes: 42 additions & 0 deletions test/TestHelpers/FakeDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Squirrel.Tests
{
public class FakeDownloader : Sources.IFileDownloader
{
public string LastUrl { get; private set; }
public string LastLocalFile { get; private set; }
public string LastAuthHeader { get; private set; }
public string LastAcceptHeader { get; private set; }
public byte[] MockedResponseBytes { get; set; } = new byte[0];
public bool WriteMockLocalFile { get; set; } = false;

public Task<byte[]> DownloadBytes(string url, string auth, string acc)
{
LastUrl = url;
LastAuthHeader = auth;
LastAcceptHeader = acc;
return Task.FromResult(MockedResponseBytes);
}

public async Task DownloadFile(string url, string targetFile, Action<int> progress, string auth, string acc)
{
LastLocalFile = targetFile;
var resp = await DownloadBytes(url, auth, acc);
progress?.Invoke(25);
progress?.Invoke(50);
progress?.Invoke(75);
progress?.Invoke(100);
if (WriteMockLocalFile)
File.WriteAllBytes(targetFile, resp);
}

public async Task<string> DownloadString(string url, string auth, string acc)
{
return Encoding.UTF8.GetString(await DownloadBytes(url, auth, acc));
}
}
}
6 changes: 3 additions & 3 deletions test/UpdateManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public async Task WhenBothFilesAreInSyncNoUpdatesAreApplied()

// check for an update
UpdateInfo updateInfo;
using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeUrlDownloader())) {
using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeDownloader())) {
updateInfo = await mgr.CheckForUpdate();
}

Expand Down Expand Up @@ -217,7 +217,7 @@ public async Task WhenRemoteReleasesDoNotHaveDeltasNoUpdatesAreApplied()
ReleaseEntry.BuildReleasesFile(remotePackages);

UpdateInfo updateInfo;
using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeUrlDownloader())) {
using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeDownloader())) {
updateInfo = await mgr.CheckForUpdate();
}

Expand Down Expand Up @@ -258,7 +258,7 @@ public async Task WhenTwoRemoteUpdatesAreAvailableChoosesDeltaVersion()
await fixture.updateLocalReleasesFile();
ReleaseEntry.BuildReleasesFile(remotePackages);

using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeUrlDownloader())) {
using (var mgr = new UpdateManager(remotePackages, "theApp", tempDir, new FakeDownloader())) {
UpdateInfo updateInfo;
updateInfo = await mgr.CheckForUpdate();
Assert.True(updateInfo.ReleasesToApply.First().IsDelta);
Expand Down

0 comments on commit da50aa8

Please sign in to comment.