From 69e4d0dacf832a6b056d04ddbba7c4434c5e4bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vidar=20L=2E=20S=C3=B8mme?= Date: Thu, 26 Feb 2015 22:13:10 +0100 Subject: [PATCH] Better url and endpoint handling --- src/Paket.Core/PublicAPI.fs | 7 ++-- src/Paket.Core/RemoteUpload.fs | 24 ++++++++++- tests/Paket.Tests/Paket.Tests.fsproj | 1 + tests/Paket.Tests/RemotePushUrlSpecs.fs | 55 +++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 tests/Paket.Tests/RemotePushUrlSpecs.fs diff --git a/src/Paket.Core/PublicAPI.fs b/src/Paket.Core/PublicAPI.fs index f58d1816c4..2b91b14e85 100644 --- a/src/Paket.Core/PublicAPI.fs +++ b/src/Paket.Core/PublicAPI.fs @@ -256,11 +256,10 @@ type Dependencies(dependenciesFileName: string) = PackageProcess.Pack(dependenciesFile, outputPath, buildConfig, version, releaseNotes) // Push a nupkg file. - member this.Push(packageFileName, ?url, ?apiKey, ?endPoint, ?maxTrials) = - let endPoint = defaultArg endPoint "/api/v2/package" + member this.Push(packageFileName, ?url, ?apiKey, (?endPoint: string), ?maxTrials) = + let urlWithEndpoint = RemoteUpload.GetUrlWithEndpoint url endPoint let apiKey = defaultArg apiKey (Environment.GetEnvironmentVariable("nugetkey")) if String.IsNullOrEmpty apiKey then failwithf "Could not push package %s. Please specify an NuGet API key via environment variable \"nugetkey\"." packageFileName - let url = defaultArg url "https://nuget.org" let maxTrials = defaultArg maxTrials 5 - RemoteUpload.Push maxTrials (url + endPoint) apiKey packageFileName \ No newline at end of file + RemoteUpload.Push maxTrials urlWithEndpoint apiKey packageFileName \ No newline at end of file diff --git a/src/Paket.Core/RemoteUpload.fs b/src/Paket.Core/RemoteUpload.fs index b8838208a6..d52f8d8519 100644 --- a/src/Paket.Core/RemoteUpload.fs +++ b/src/Paket.Core/RemoteUpload.fs @@ -25,7 +25,29 @@ type System.Net.WebClient with stream.Write(newlineBytes, 0, newlineBytes.Length) stream.Write(trailerbytes, 0, trailerbytes.Length) () - + +let GetUrlWithEndpoint (url: string option) (endPoint: string option) = + let (|UrlWithEndpoint|_|) url = + match url with + | Some url when not (String.IsNullOrEmpty(Uri(url).AbsolutePath.TrimStart('/'))) -> Some(Uri(url)) + | _ -> None + + let (|IsUrl|_|) (url: string option) = + match url with + | Some url -> Uri(url.TrimEnd('/') + "/") |> Some + | _ -> None + + let defaultEndpoint = "/api/v2/package" + let urlWithEndpoint = + match (url, endPoint) with + | None , _ -> Uri(Uri("https://nuget.org"), defaultEndpoint) + | IsUrl baseUrl , Some customEndpoint -> Uri(baseUrl, customEndpoint.TrimStart('/')) + | UrlWithEndpoint baseUrl, _ -> baseUrl + | IsUrl baseUrl , None -> Uri(baseUrl, defaultEndpoint) + | Some whyIsThisNeeded , _ -> failwith "Url and endpoint combination not supported" + urlWithEndpoint.ToString () + + let Push maxTrials url apiKey packageFileName = let rec push trial = tracefn "Pushing package %s to %s - trial %d" packageFileName url trial diff --git a/tests/Paket.Tests/Paket.Tests.fsproj b/tests/Paket.Tests/Paket.Tests.fsproj index cef35650f7..89726c6bd6 100644 --- a/tests/Paket.Tests/Paket.Tests.fsproj +++ b/tests/Paket.Tests/Paket.Tests.fsproj @@ -81,6 +81,7 @@ + Always diff --git a/tests/Paket.Tests/RemotePushUrlSpecs.fs b/tests/Paket.Tests/RemotePushUrlSpecs.fs new file mode 100644 index 0000000000..4559652eea --- /dev/null +++ b/tests/Paket.Tests/RemotePushUrlSpecs.fs @@ -0,0 +1,55 @@ +module Paket.RemotePushUrlSpecs + +open Paket.RemoteUpload +open NUnit.Framework +open System.Xml +open FsUnit + +let nugetUrl = "https://nuget.org" +let defaultEndpoint = "/api/v2/package" +let customHost = "http://my.host.com" +let customEndpoint = "/my/feed" +let customHostWithEndpoint = (customHost + customEndpoint) + +[] +let ``default result is nuget host with default endpoint`` () = + GetUrlWithEndpoint None None + |> shouldEqual (nugetUrl + defaultEndpoint) + +[] +let ``no host with custom endpoint yields nuget host with default endpoint`` () = + GetUrlWithEndpoint None (Some customEndpoint) + |> shouldEqual (nugetUrl + defaultEndpoint) + +[] +let ``custom host with no endpoint yields custom host with default endpoint`` () = + GetUrlWithEndpoint (Some customHost) None + |> shouldEqual (customHost + defaultEndpoint) + +[] +let ``custom host with custom endpoint yields custom host with custom endpoint`` () = + GetUrlWithEndpoint (Some customHost) (Some customEndpoint) + |> shouldEqual (customHost + customEndpoint) + +[] +let ``custom host that includes endpoint and no custom enpoint does not append default endpoint`` () = + GetUrlWithEndpoint (Some customHostWithEndpoint) None + |> shouldEqual customHostWithEndpoint + +[] +let ``custom host that includes endpoint and custom endpoint yields host + customendpoint`` () = + GetUrlWithEndpoint (Some customHostWithEndpoint) (Some customEndpoint) + |> shouldEqual (customHostWithEndpoint + customEndpoint) + +[] +let ``can combine host and endpoint with missing leading slash on endpoint`` () = + let noSlashes = "my/feed" + GetUrlWithEndpoint (Some customHost) (Some noSlashes) + |> shouldEqual (customHost + "/" + noSlashes) + +[] +let ``can combine host and endpoint with leading slash on endpoint and trailing slash on host`` () = + let slashyHost = customHost + "/" + GetUrlWithEndpoint (Some slashyHost) (Some customEndpoint) + |> shouldEqual (customHost + customEndpoint) +