diff --git a/lib/curl_req/macro.ex b/lib/curl_req/macro.ex index 84d3691..108ad94 100644 --- a/lib/curl_req/macro.ex +++ b/lib/curl_req/macro.ex @@ -29,7 +29,8 @@ defmodule CurlReq.Macro do proxy: :string, proxy_user: :string, user: :string, - netrc: :boolean + netrc: :boolean, + netrc_file: :string ], aliases: [ H: :header, @@ -177,15 +178,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 diff --git a/test/curl_req/macro_test.exs b/test/curl_req/macro_test.exs index cf91ba7..59b5d35 100644 --- a/test/curl_req/macro_test.exs +++ b/test/curl_req/macro_test.exs @@ -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"), @@ -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{