From 3979168d57666b3c91424fcb45a272f9af1bc3cd Mon Sep 17 00:00:00 2001 From: YeJun Su Date: Wed, 3 Apr 2019 00:53:25 +0800 Subject: [PATCH] always parse body by serializer if it exists --- lib/oauth2/response.ex | 6 +++--- test/oauth2/response_test.exs | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/oauth2/response.ex b/lib/oauth2/response.ex index 0b6e958..7a5d3d4 100644 --- a/lib/oauth2/response.ex +++ b/lib/oauth2/response.ex @@ -47,6 +47,9 @@ defmodule OAuth2.Response do defp decode_response_body("", _type, _), do: "" defp decode_response_body(" ", _type, _), do: "" + defp decode_response_body(body, _type, serializer) when serializer != nil do + serializer.decode!(body) + end # Facebook sends text/plain tokens!? defp decode_response_body(body, "text/plain", _) do case URI.decode_query(body) do @@ -60,7 +63,4 @@ defmodule OAuth2.Response do defp decode_response_body(body, _mime, nil) do body end - defp decode_response_body(body, _type, serializer) do - serializer.decode!(body) - end end diff --git a/test/oauth2/response_test.exs b/test/oauth2/response_test.exs index 2fae594..0aa46e2 100644 --- a/test/oauth2/response_test.exs +++ b/test/oauth2/response_test.exs @@ -19,4 +19,10 @@ defmodule OAuth2.ResponseTest do response = Response.new(%OAuth2.Client{}, 200, [{"content-type", "text/plain"}], "hello") assert response.body == "hello" end + + test "always parse body by serializer if it exists" do + client = OAuth2.Client.put_serializer(%OAuth2.Client{}, "text/plain", Jason) + response = Response.new(client, 200, [{"content-type", "text/plain"}], ~S({"hello": "world"})) + assert response.body == %{"hello" => "world"} + end end