From ebaaddf076097268a3430728695eca146395165d Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Wed, 5 Aug 2020 00:04:58 -0400 Subject: check that the server response has a JSON content-type --- lib/polyjuice/client/endpoint.ex | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/polyjuice/client/endpoint.ex b/lib/polyjuice/client/endpoint.ex index b75ee6c..41c0a86 100644 --- a/lib/polyjuice/client/endpoint.ex +++ b/lib/polyjuice/client/endpoint.ex @@ -87,8 +87,14 @@ defmodule Polyjuice.Client.Endpoint do ) :: any def parse_response(%{} = endpoint_args, status_code, headers, body) when is_integer(status_code) and is_list(headers) and is_binary(body) do - # FIXME: check if content type is "application/json" - with {:ok, json} <- Jason.decode(body) do + # make sure it's JSON content + with {_, "application/json"} <- + Enum.find( + headers, + {nil, nil}, + fn {name, _} -> String.downcase(name, :ascii) == "content-type" end + ), + {:ok, json} <- Jason.decode(body) do case status_code do 200 -> Polyjuice.Client.Endpoint.BodyParser.parse(endpoint_args, json) -- cgit v1.2.3