Skip to content

Commit

Permalink
Url combining with System.Uri does not do what you expect
Browse files Browse the repository at this point in the history
references #303
  • Loading branch information
pvandervelde committed Jun 28, 2017
1 parent 45febb4 commit b96e6ce
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
28 changes: 14 additions & 14 deletions src/nbuildkit/tasks/Test.Unit.MsBuild.Tasks/Web/WebUploadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public void Execute()
{
var fileToUpload = Assembly.GetExecutingAssembly().LocalFilePath();

var targetUri = "http://www.example.com";
var targetUri = "http://www.example.com/mypath";

var webClient = new Mock<IInternalWebClient>();
{
webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>()))
.Callback<Uri, string>(
(uri, path) =>
webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()))
.Callback<Uri, string, string>(
(uri, method, path) =>
{
Assert.AreEqual(new Uri(new Uri(targetUri), Path.GetFileName(path)), uri);
Assert.AreEqual(new Uri(targetUri + "/" + Path.GetFileName(path)), uri);
Assert.AreEqual(fileToUpload, path);
})
.Verifiable();
Expand All @@ -57,7 +57,7 @@ public void Execute()
var result = task.Execute();
Assert.IsTrue(result, "Expected the task to finish successfully");

webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>()), Times.Once());
webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once());

VerifyNumberOfLogMessages(numberOfErrorMessages: 0, numberOfWarningMessages: 0, numberOfNormalMessages: 2);
}
Expand All @@ -69,7 +69,7 @@ public void ExecuteWithEmptyUrl()

var webClient = new Mock<IInternalWebClient>();
{
webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>()))
webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()))
.Verifiable();
}

Expand All @@ -86,7 +86,7 @@ public void ExecuteWithEmptyUrl()
var result = task.Execute();
Assert.IsFalse(result, "Expected the task to not finish successfully");

webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>()), Times.Never());
webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never());

VerifyNumberOfLogMessages(numberOfErrorMessages: 1, numberOfWarningMessages: 0, numberOfNormalMessages: 0);
}
Expand All @@ -98,7 +98,7 @@ public void ExecuteWithInvalidUrl()

var webClient = new Mock<IInternalWebClient>();
{
webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>()))
webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()))
.Verifiable();
}

Expand All @@ -115,7 +115,7 @@ public void ExecuteWithInvalidUrl()
var result = task.Execute();
Assert.IsFalse(result, "Expected the task to not finish successfully");

webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>()), Times.Never());
webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never());

VerifyNumberOfLogMessages(numberOfErrorMessages: 1, numberOfWarningMessages: 0, numberOfNormalMessages: 0);
}
Expand All @@ -125,7 +125,7 @@ public void ExecuteWithoutItems()
{
var webClient = new Mock<IInternalWebClient>();
{
webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>()))
webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()))
.Verifiable();
}

Expand All @@ -142,7 +142,7 @@ public void ExecuteWithoutItems()
var result = task.Execute();
Assert.IsFalse(result, "Expected the task to not finish successfully");

webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>()), Times.Never());
webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never());

VerifyNumberOfLogMessages(numberOfErrorMessages: 1, numberOfWarningMessages: 0, numberOfNormalMessages: 0);
}
Expand All @@ -154,7 +154,7 @@ public void ExecuteWithoutUrl()

var webClient = new Mock<IInternalWebClient>();
{
webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>()))
webClient.Setup(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()))
.Verifiable();
}

Expand All @@ -170,7 +170,7 @@ public void ExecuteWithoutUrl()
var result = task.Execute();
Assert.IsFalse(result, "Expected the task to not finish successfully");

webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>()), Times.Never());
webClient.Verify(w => w.UploadFile(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never());

VerifyNumberOfLogMessages(numberOfErrorMessages: 1, numberOfWarningMessages: 0, numberOfNormalMessages: 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public interface IInternalWebClient : IDisposable
/// Uploads the specified local file to a resource with the specified URI.
/// </summary>
/// <param name="address">The URI of the resource to receive the file. For example, ftp://localhost/samplefile.txt.</param>
/// <param name="method">The method that should be used for the upload.</param>
/// <param name="fileName">The file to send to the resource. For example, "samplefile.txt".</param>
void UploadFile(Uri address, string fileName);
void UploadFile(Uri address, string method, string fileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ public void DownloadFile(Uri address, string fileName)
/// Uploads the specified local file to a resource with the specified URI.
/// </summary>
/// <param name="address">The URI of the resource to receive the file. For example, ftp://localhost/samplefile.txt.</param>
/// <param name="method">The method that should be used for the upload.</param>
/// <param name="fileName">The file to send to the resource. For example, "samplefile.txt".</param>
public void UploadFile(Uri address, string fileName)
public void UploadFile(Uri address, string method, string fileName)
{
_webClient.UploadFile(address, fileName);
_webClient.UploadFile(address, method, fileName);
}
}
}
5 changes: 3 additions & 2 deletions src/nbuildkit/tasks/nBuildKit.MsBuild.Tasks/Web/WebUpload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using Flurl;
using Microsoft.Build.Framework;
using NBuildKit.MsBuild.Tasks.Core;

Expand Down Expand Up @@ -152,15 +153,15 @@ public override bool Execute()
}

var itemPath = GetAbsolutePath(item);
var targetUri = new Uri(baseUri, Path.GetFileName(itemPath));
var targetUri = new Uri(Url.Combine(baseUri.ToString(), Path.GetFileName(itemPath)));
try
{
Log.LogMessage(
MessageImportance.Normal,
"Uploading from: {0}. To: {1}",
itemPath,
targetUri);
client.UploadFile(targetUri, itemPath);
client.UploadFile(targetUri, "PUT", itemPath);
}
catch (WebException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<HintPath>..\..\..\..\packages\Consul.0.6.4.7\lib\net45\Consul.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Flurl, Version=2.3.0.0, Culture=neutral, PublicKeyToken=1308302a96879dfb, processorArchitecture=MSIL">
<HintPath>..\..\..\..\packages\Flurl.Signed.2.3.0\lib\portable40-net40+sl5+win8+wp8+wpa81\Flurl.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.Build.Framework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.Build.Utilities.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Consul" version="0.6.4.7" targetFramework="net452" />
<package id="Flurl.Signed" version="2.3.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net452" />
<package id="Nuclei" version="0.9.2" targetFramework="net452" />
<package id="Nuclei.AppDomains" version="0.9.0" targetFramework="net452" />
Expand Down

0 comments on commit b96e6ce

Please sign in to comment.