Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UtilsSpecs: Unit test corrected + compatibility issues with Klondike NuGet server #997

Merged
merged 2 commits into from
Aug 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/Paket.Core/NuGetV2.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Contains NuGet support.
/// Contains NuGet support.
module Paket.NuGetV2

open System
Expand Down Expand Up @@ -30,7 +30,7 @@ type NugetPackageCache =

let rec private followODataLink getUrlContents url =
async {
let! raw = getUrlContents url
let! raw = getUrlContents url acceptXml
let doc = XmlDocument()
doc.LoadXml raw
let feed =
Expand Down Expand Up @@ -83,8 +83,8 @@ let getAllVersionsFromNuGet2(auth,nugetURL,package) =
async {
let url = sprintf "%s/package-versions/%s?includePrerelease=true" nugetURL package
verbosefn "getAllVersionsFromNuGet2 from url '%s'" url
let! raw = safeGetFromUrl(auth, url)
let getUrlContents url = getFromUrl(auth, url)
let! raw = safeGetFromUrl(auth, url, acceptJson)
let getUrlContents url acceptJson = getFromUrl(auth, url, acceptJson)
match raw with
| None -> let! result = getAllVersionsFromNugetOData(getUrlContents, nugetURL, package)
return result
Expand Down Expand Up @@ -215,15 +215,15 @@ let getDetailsFromNuGetViaODataFast auth nugetURL package (version:SemVerInfo) =
async {
try
let url = sprintf "%s/Packages?$filter=Id eq '%s' and NormalizedVersion eq '%s'" nugetURL package (version.Normalize())
let! raw = getFromUrl(auth,url)
let! raw = getFromUrl(auth,url,acceptXml)
if verbose then
tracefn "Response from %s:" url
tracefn ""
tracefn "%s" raw
return parseODataDetails(nugetURL,package,version,raw)
with _ ->
let url = sprintf "%s/Packages?$filter=Id eq '%s' and Version eq '%s'" nugetURL package (version.ToString())
let! raw = getFromUrl(auth,url)
let! raw = getFromUrl(auth,url,acceptXml)
if verbose then
tracefn "Response from %s:" url
tracefn ""
Expand All @@ -238,7 +238,7 @@ let getDetailsFromNuGetViaOData auth nugetURL package (version:SemVerInfo) =
return! getDetailsFromNuGetViaODataFast auth nugetURL package version
with _ ->
let url = sprintf "%s/Packages(Id='%s',Version='%s')" nugetURL package (version.ToString())
let! response = safeGetFromUrl(auth,url)
let! response = safeGetFromUrl(auth,url,acceptXml)

let! raw =
match response with
Expand Down
6 changes: 3 additions & 3 deletions src/Paket.Core/NuGetV3.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let getSearchAPI(auth,nugetUrl) =
| None -> None
| Some v3Path ->
let serviceData =
safeGetFromUrl(auth,v3Path)
safeGetFromUrl(auth,v3Path,acceptJson)
|> Async.RunSynchronously

match serviceData with
Expand All @@ -62,7 +62,7 @@ let internal findVersionsForPackage(auth, nugetURL, package, includingPrerelease
async {
match getSearchAPI(auth,nugetURL) with
| Some url ->
let! response = safeGetFromUrl(auth,sprintf "%s?id=%s&take=%d%s" url package (max maxResults 100000) (if includingPrereleases then "&prerelease=true" else "")) // Nuget is showing old versions first
let! response = safeGetFromUrl(auth,sprintf "%s?id=%s&take=%d%s" url package (max maxResults 100000) (if includingPrereleases then "&prerelease=true" else ""), acceptXml) // Nuget is showing old versions first
match response with
| Some text ->
let versions =
Expand Down Expand Up @@ -94,7 +94,7 @@ let private getPackages(auth, nugetURL, packageNamePrefix, maxResults) = async {
match getSearchAPI(auth,nugetURL) with
| Some url ->
let query = sprintf "%s?q=%s&take=%d" url packageNamePrefix maxResults
let! response = safeGetFromUrl(auth,query)
let! response = safeGetFromUrl(auth,query,acceptJson)
match response with
| Some text -> return extractPackages text
| None -> return [||]
Expand Down
8 changes: 4 additions & 4 deletions src/Paket.Core/RemoteDownload.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ let getSHA1OfBranch origin owner project branch =
match origin with
| ModuleResolver.SingleSourceFileOrigin.GitHubLink ->
let url = sprintf "https://api.github.com/repos/%s/%s/commits/%s" owner project branch
let! document = getFromUrl(None, url)
let! document = getFromUrl(None, url, null)
let json = JObject.Parse(document)
return json.["sha"].ToString()
| ModuleResolver.SingleSourceFileOrigin.GistLink ->
let url = sprintf "https://api.github.com/gists/%s/%s" project branch
let! document = getFromUrl(None, url)
let! document = getFromUrl(None, url, null)
let json = JObject.Parse(document)
let latest = json.["history"].First.["version"]
return latest.ToString()
Expand All @@ -44,7 +44,7 @@ let downloadDependenciesFile(rootPath,parserF,remoteFile:ModuleResolver.Resolved
| ModuleResolver.GistLink ->
rawGistFileUrl remoteFile.Owner remoteFile.Project dependenciesFileName
| ModuleResolver.HttpLink url -> url.Replace(remoteFile.Name,Constants.DependenciesFileName)
let! result = safeGetFromUrl(None,url)
let! result = safeGetFromUrl(None,url,null)

match result with
| Some text when parserF text ->
Expand Down Expand Up @@ -83,7 +83,7 @@ let downloadRemoteFiles(remoteFile:ResolvedSourceFile,destination) = async {
let projectPath = fi.Directory.FullName

let url = sprintf "https://api.github.com/gists/%s" remoteFile.Project
let! document = getFromUrl(None, url)
let! document = getFromUrl(None, url, null)
let json = JObject.Parse(document)
let files = json.["files"] |> Seq.map (fun i -> i.First.["filename"].ToString(), i.First.["raw_url"].ToString())

Expand Down
15 changes: 13 additions & 2 deletions src/Paket.Core/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ open Paket.Logging
open Chessie.ErrorHandling
open Paket.Domain

let acceptXml = "application/atom+xml,application/xml"
let acceptJson = "application/atom+json,application/json"

let notNullOrEmpty = not << System.String.IsNullOrEmpty

type Auth =
{ Username : string
Password : string }
Expand Down Expand Up @@ -168,10 +173,12 @@ let downloadFromUrl (auth:Auth option, url : string) (filePath: string) =
}

/// [omit]
let getFromUrl (auth:Auth option, url : string) =
let getFromUrl (auth:Auth option, url : string, contentType : string) =
async {
try
use client = createWebClient(url,auth)
if notNullOrEmpty contentType then
client.Headers.Add(HttpRequestHeader.Accept, contentType)
let s = client.DownloadStringTaskAsync(Uri(url)) |> Async.AwaitTask
return! s
with
Expand Down Expand Up @@ -200,10 +207,14 @@ let getXmlFromUrl (auth:Auth option, url : string) =
}

/// [omit]
let safeGetFromUrl (auth:Auth option, url : string) =
let safeGetFromUrl (auth:Auth option, url : string, contentType : string) =
async {
try
use client = createWebClient(url,auth)

if notNullOrEmpty contentType then
client.Headers.Add(HttpRequestHeader.Accept, contentType)

let s = client.DownloadStringTaskAsync(Uri(url)) |> Async.AwaitTask
let! raw = s
return Some raw
Expand Down
2 changes: 1 addition & 1 deletion tests/Paket.Tests/NuGetOData/ODataSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ let ``can calculate v3 path``() =

[<Test>]
let ``can read all versions from single page with multiple entries``() =
let getUrlContentsStub _ = async { return File.ReadAllText "NuGetOData/NUnit.xml" }
let getUrlContentsStub _ _ = async { return File.ReadAllText "NuGetOData/NUnit.xml" }

let versions = getAllVersionsFromNugetOData(getUrlContentsStub, fakeUrl, "NUnit")
|> Async.RunSynchronously
Expand Down
2 changes: 1 addition & 1 deletion tests/Paket.Tests/UtilsSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let ``createRelativePath should handle spaces``() =
let ``normalize path with home directory``() =
"~/data"
|> Utils.normalizeLocalPath
|> shouldEqual (AbsolutePath (GetHomeDirectory() + Path.DirectorySeparatorChar.ToString() + "data"))
|> shouldEqual (AbsolutePath (Path.Combine(GetHomeDirectory(), "data")))

[<Test>]
let ``relative local path is returned as is``() =
Expand Down