Skip to content

Commit

Permalink
Merge pull request #663 from Vidarls/endpointselect
Browse files Browse the repository at this point in the history
Better url and endpoint handling
  • Loading branch information
forki committed Feb 27, 2015
2 parents 1cb0a76 + 69e4d0d commit 7bd2d8b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/Paket.Core/PublicAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
RemoteUpload.Push maxTrials urlWithEndpoint apiKey packageFileName
24 changes: 23 additions & 1 deletion src/Paket.Core/RemoteUpload.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/Paket.Tests/Paket.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<Compile Include="TemplateFileParsing.fs" />
<Compile Include="NuspecWriterSpecs.fs" />
<Compile Include="PackageProcessSpecs.fs" />
<Compile Include="RemotePushUrlSpecs.fs" />
<Content Include="Nuspec\FSharp.Data.nuspec">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
55 changes: 55 additions & 0 deletions tests/Paket.Tests/RemotePushUrlSpecs.fs
Original file line number Diff line number Diff line change
@@ -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)

[<Test>]
let ``default result is nuget host with default endpoint`` () =
GetUrlWithEndpoint None None
|> shouldEqual (nugetUrl + defaultEndpoint)

[<Test>]
let ``no host with custom endpoint yields nuget host with default endpoint`` () =
GetUrlWithEndpoint None (Some customEndpoint)
|> shouldEqual (nugetUrl + defaultEndpoint)

[<Test>]
let ``custom host with no endpoint yields custom host with default endpoint`` () =
GetUrlWithEndpoint (Some customHost) None
|> shouldEqual (customHost + defaultEndpoint)

[<Test>]
let ``custom host with custom endpoint yields custom host with custom endpoint`` () =
GetUrlWithEndpoint (Some customHost) (Some customEndpoint)
|> shouldEqual (customHost + customEndpoint)

[<Test>]
let ``custom host that includes endpoint and no custom enpoint does not append default endpoint`` () =
GetUrlWithEndpoint (Some customHostWithEndpoint) None
|> shouldEqual customHostWithEndpoint

[<Test>]
let ``custom host that includes endpoint and custom endpoint yields host + customendpoint`` () =
GetUrlWithEndpoint (Some customHostWithEndpoint) (Some customEndpoint)
|> shouldEqual (customHostWithEndpoint + customEndpoint)

[<Test>]
let ``can combine host and endpoint with missing leading slash on endpoint`` () =
let noSlashes = "my/feed"
GetUrlWithEndpoint (Some customHost) (Some noSlashes)
|> shouldEqual (customHost + "/" + noSlashes)

[<Test>]
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)

0 comments on commit 7bd2d8b

Please sign in to comment.