Skip to content

Commit

Permalink
Merge 062b59a into d6a3be5
Browse files Browse the repository at this point in the history
  • Loading branch information
imranismail committed Jan 16, 2017
2 parents d6a3be5 + 062b59a commit cee375a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/maxwell/middleware/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ defmodule Maxwell.Middleware.DecodeJson do
def response(%Maxwell.Conn{} = conn, {decode_fun, valid_content_types}) do
case Maxwell.Conn.get_resp_header(conn, "content-type") do
{_, content_type} ->
case is_json_content(content_type, conn.resp_body, valid_content_types) do
case valid_content?(content_type, conn.resp_body, valid_content_types) do
true ->
case decode_fun.(conn.resp_body) do
{:ok, resp_body} -> %{conn | resp_body: resp_body}
Expand All @@ -158,12 +158,16 @@ defmodule Maxwell.Middleware.DecodeJson do
end
end

defp is_json_content(content_type, body, valid_types) do
defp valid_content?(content_type, body, valid_types) do
valid_types = ["application/json", "text/javascript"| valid_types]
is_valid_type = Enum.find(valid_types, fn(x) -> String.starts_with?(content_type, x) end)
is_valid_type && (is_binary(body) || is_list(body))
valid_type? = Enum.find(valid_types, fn(x) -> String.starts_with?(content_type, x) end)
valid_type? && (is_binary(body) || is_list(body)) && present?(body)
end

defp present?(""), do: false
defp present?([]), do: false
defp present?(term) when is_binary(term) or is_list(term), do: true

defp check_opts(opts) do
for {key, value} <- opts do
case key do
Expand Down

0 comments on commit cee375a

Please sign in to comment.