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

Update OAuth2.Client.get_token! errors #98

Merged
merged 3 commits into from
Oct 6, 2017
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
17 changes: 15 additions & 2 deletions lib/oauth2/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,21 @@ defmodule OAuth2.Client do
@spec get_token!(t, params, headers, Keyword.t) :: Client.t | Error.t
def get_token!(client, params \\ [], headers \\ [], opts \\ []) do
case get_token(client, params, headers, opts) do
{:ok, client} -> client
{:error, error} -> raise error
{:ok, client} ->
client
{:error, %Response{status_code: code, headers: headers, body: body}} ->
raise %Error{reason: """
Server responded with status: #{code}

Headers:

#{Enum.reduce(headers, "", fn {k, v}, acc -> acc <> "#{k}: #{v}\n" end)}
Body:

#{inspect body}
"""}
{:error, error} ->
raise error
end
end

Expand Down
27 changes: 27 additions & 0 deletions test/oauth2/client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule OAuth2.ClientTest do
import OAuth2.TestHelpers

alias OAuth2.Client
alias OAuth2.Response

setup do
server = Bypass.open
Expand Down Expand Up @@ -59,6 +60,24 @@ defmodule OAuth2.ClientTest do
assert token.access_token == "test1234"
end

test "get_token, get_token! when response error", %{client: client, server: server} do
code = [code: "code1234"]
headers = [{"accept", "application/json"}]

bypass server, "POST", "/oauth/token", fn conn ->
assert conn.query_string == ""
send_resp(conn, 500, ~s({"error":"missing_client_id"}))
end

assert {:error, error} = Client.get_token(client, code, headers)
assert %Response{body: body, status_code: 500} = error
assert body == %{"error" => "missing_client_id"}

assert_raise OAuth2.Error, ~r/Body/, fn ->
Client.get_token!(client, code, headers)
end
end

test "refresh_token and refresh_token! with a POST", %{server: server, client_with_token: client} do
bypass server, "POST", "/oauth/token", fn conn ->
assert get_req_header(conn, "authorization") == []
Expand Down Expand Up @@ -124,6 +143,14 @@ defmodule OAuth2.ClientTest do
assert {"content-type", "application/xml"} = List.keyfind(client.headers, "content-type", 0)
end

test "basic_auth", %{client: client} do
%OAuth2.Client{client_id: id, client_secret: secret} = client
client = basic_auth(client)

assert {"authorization", value} = List.keyfind(client.headers, "authorization", 0)
assert value == "Basic " <> Base.encode64(id <> ":" <> secret)
end

## GET

test "GET", %{server: server, client_with_token: client} do
Expand Down