Skip to content

Commit

Permalink
Try to use .netrc if available and enable session cookies by default (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion authored Apr 14, 2021
1 parent 199f9b5 commit 644d7d8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Curl/Easy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ function set_defaults(easy::Easy)
setopt(easy, CURLOPT_MAXREDIRS, 50)
setopt(easy, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL)
setopt(easy, CURLOPT_USERAGENT, USER_AGENT)
setopt(easy, CURLOPT_NETRC, CURL_NETRC_OPTIONAL)
setopt(easy, CURLOPT_COOKIEFILE, "")

# ssh-related options
setopt(easy, CURLOPT_SSH_PRIVATE_KEYFILE, ssh_key_path())
setopt(easy, CURLOPT_SSH_PUBLIC_KEYFILE, ssh_pub_key_path())
Expand Down
46 changes: 46 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,52 @@ include("setup.jl")
end
end

@testset "session support" begin
downloader = Downloader()

# This url will redirect to /cookies, which echoes the set cookies as json
set_cookie_url = "$server/cookies/set?k1=v1&k2=v2"
cookies = download_json(set_cookie_url, downloader=downloader)
@test get(cookies, "k1", "") == "v1"
@test get(cookies, "k2", "") == "v2"

# As the handle is destroyed, subsequent requests have no cookies
cookie_url = "$server/cookies"
cookies = download_json(cookie_url, downloader=downloader)
@test isempty(cookies)
end

@testset "netrc support" begin

auth_url = "$server/basic-auth/user/passwd"
resp = request(auth_url)
@test resp isa Response
@test resp.status == 401 # no succesful authentication

# Setup .netrc
servername = split(server, "/")[end] # strip https://
netrc = tempname()
open(netrc, "w") do io
write(io, "machine $servername login user password passwd\n")
end

# Setup config to point to custom .netrc (normally in ~/.netrc)
downloader = Downloads.Downloader()
easy_hook = (easy, info) -> begin
Downloads.Curl.setopt(
easy,
Downloads.Curl.CURLOPT_NETRC_FILE, netrc)
end
downloader.easy_hook = easy_hook

resp = request(auth_url, throw=false, downloader=downloader)
@test resp isa Response
@test resp.status == 200 # succesful authentication

# Cleanup
rm(netrc) # isn't cleaned automatically on Julia 1.3
end

@testset "file protocol" begin
@testset "success" begin
path = tempname()
Expand Down

0 comments on commit 644d7d8

Please sign in to comment.