Repository: wojtekmach/req (elixir)

Issue:
`auth`: Support bearer auth
curl example:

```bash
$ curl --fail -H "Authorization: Bearer foo" https://httpbin.org/bearer
{
  "authenticated": true,
  "token": "foo"
}
```

We should support:

```elixir
Req.get!("https://httpbin.org/bearer", auth: {:bearer, "foo"})
```

Pull request description:
support bearer auth
Closes #20.

Patch:
diff --git a/lib/req/steps.ex b/lib/req/steps.ex
index a201a0c..5fe682f 100644
--- a/lib/req/steps.ex
+++ b/lib/req/steps.ex
@@ -144,6 +144,7 @@ defmodule Req.Steps do
   `auth` can be one of:
 
     * `{username, password}` - uses Basic HTTP authentication
+    * `{:bearer, token}` - uses Bearer HTTP authentication
 
   ## Examples
 
@@ -152,10 +153,19 @@ defmodule Req.Steps do
       iex> Req.get!("https://httpbin.org/basic-auth/foo/bar", auth: {"foo", "bar"}).status
       200
 
+      iex> Req.get!("https://httpbin.org/bearer", auth: {:bearer, ""}).status
+      401
+      iex> Req.get!("https://httpbin.org/bearer", auth: {:bearer, "foo"}).status
+      200
+
   """
   @doc step: :request
   def auth(request, auth)
 
+  def auth(request, {:bearer, token}) when is_binary(token) do
+    put_new_header(request, "authorization", "Bearer #{token}")
+  end
+
   def auth(request, {username, password}) when is_binary(username) and is_binary(password) do
     value = Base.encode64("#{username}:#{password}")
     put_new_header(request, "authorization", "Basic #{value}")


Test patch:
diff --git a/test/req/steps_test.exs b/test/req/steps_test.exs
index e1734a9..4b1cbf5 100644
--- a/test/req/steps_test.exs
+++ b/test/req/steps_test.exs
@@ -17,7 +17,7 @@ defmodule Req.StepsTest do
     assert Req.get!("", base_url: c.url).status == 200
   end
 
-  test "auth/2", c do
+  test "auth/2: basic", c do
     Bypass.expect(c.bypass, "GET", "/auth", fn conn ->
       expected = "Basic " <> Base.encode64("foo:bar")
 
@@ -34,6 +34,23 @@ defmodule Req.StepsTest do
     assert Req.get!(c.url <> "/auth", auth: {"foo", "bar"}).status == 200
   end
 
+  test "auth/2: bearer", c do
+    Bypass.expect(c.bypass, "GET", "/auth", fn conn ->
+      expected = "Bearer valid_token"
+
+      case Plug.Conn.get_req_header(conn, "authorization") do
+        [^expected] ->
+          Plug.Conn.send_resp(conn, 200, "ok")
+
+        _ ->
+          Plug.Conn.send_resp(conn, 401, "unauthorized")
+      end
+    end)
+
+    assert Req.get!(c.url <> "/auth", auth: {:bearer, "bad_token"}).status == 401
+    assert Req.get!(c.url <> "/auth", auth: {:bearer, "valid_token"}).status == 200
+  end
+
   test "encode_headers/1", c do
     pid = self()