Skip to content

Commit

Permalink
add additional auth steps to parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinschweikert committed Dec 5, 2024
1 parent d36e4b5 commit 0941b71
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
33 changes: 29 additions & 4 deletions lib/curl_req/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ defmodule CurlReq.Macro do
user: :string,
compressed: :boolean,
user: :string,
netrc: :boolean
netrc: :boolean,
netrc_file: :string
],
aliases: [
H: :header,
Expand Down Expand Up @@ -153,15 +154,39 @@ defmodule CurlReq.Macro do
end

defp add_auth(req, options) do
case Keyword.get(options, :user) do
req =
case Keyword.get(options, :user) do
nil ->
req

credentials ->
req
|> Req.Request.register_options([:auth])
|> Req.Request.prepend_request_steps(auth: &Req.Steps.auth/1)
|> Req.merge(auth: {:basic, credentials})
end

req =
case Keyword.get(options, :netrc) do
nil ->
req

true ->
req
|> Req.Request.register_options([:auth])
|> Req.Request.prepend_request_steps(auth: &Req.Steps.auth/1)
|> Req.merge(auth: :netrc)
end

case Keyword.get(options, :netrc_file) do
nil ->
req

credentials ->
path ->
req
|> Req.Request.register_options([:auth])
|> Req.Request.prepend_request_steps(auth: &Req.Steps.auth/1)
|> Req.merge(auth: {:basic, credentials})
|> Req.merge(auth: {:netrc, path})
end
end

Expand Down
26 changes: 25 additions & 1 deletion test/curl_req/macro_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ defmodule CurlReq.MacroTest do
}
end

test "auth" do
test "basic auth" do
assert ~CURL(curl http://example.com -u user:pass) ==
%Req.Request{
url: URI.parse("http://example.com"),
Expand Down Expand Up @@ -171,6 +171,30 @@ defmodule CurlReq.MacroTest do
}
end

test "netrc auth" do
assert ~CURL(curl http://example.com -n) ==
%Req.Request{
url: URI.parse("http://example.com"),
body: nil,
registered_options: MapSet.new([:auth]),
options: %{auth: :netrc},
current_request_steps: [:auth],
request_steps: [auth: &Req.Steps.auth/1]
}
end

test "netrc file auth" do
assert ~CURL(curl http://example.com --netrc-file "./mynetrc") ==
%Req.Request{
url: URI.parse("http://example.com"),
body: nil,
registered_options: MapSet.new([:auth]),
options: %{auth: {:netrc, "./mynetrc"}},
current_request_steps: [:auth],
request_steps: [auth: &Req.Steps.auth/1]
}
end

test "compressed" do
assert ~CURL(curl --compressed http://example.com) ==
%Req.Request{
Expand Down

0 comments on commit 0941b71

Please sign in to comment.