diff --git a/lib/plug/debugger.ex b/lib/plug/debugger.ex index 4f69d7bf..a05d37f9 100644 --- a/lib/plug/debugger.ex +++ b/lib/plug/debugger.ex @@ -299,8 +299,23 @@ defmodule Plug.Debugger do defp accepts_html?(_accept_header = []), do: false - defp accepts_html?(_accept_header = [header | _]), - do: String.contains?(header, ["*/*", "text/*", "text/html"]) + defp accepts_html?(_accept_header = [header | _]) do + header + |> String.split(",", trim: true) + |> Enum.map(&Plug.Conn.Utils.media_type/1) + |> Enum.any?(fn + {:ok, type, subtype, params} -> + accept_html_media_type?(type, subtype, Map.get(params, "q", 1)) + + _ -> + false + end) + end + + defp accept_html_media_type?("text", "html", 1), do: true + defp accept_html_media_type?("text", "*", 1), do: true + defp accept_html_media_type?("*", "*", 1), do: true + defp accept_html_media_type?(_type, _subtype, _q), do: false defp maybe_fetch_session(conn) do if conn.private[:plug_session_fetch] do diff --git a/test/plug/debugger_test.exs b/test/plug/debugger_test.exs index b76388d0..c1a868a9 100644 --- a/test/plug/debugger_test.exs +++ b/test/plug/debugger_test.exs @@ -628,4 +628,31 @@ defmodule Plug.DebuggerTest do String.to_existing_atom("not_existing_atom_for_test_does_not_create_new_atom") end end + + test "render markdown with text/html;q=0.5" do + conn = + conn(:get, "/") + |> put_req_header("accept", "text/html;q=0.5") + |> render([], fn -> raise "oops" end) + + assert get_resp_header(conn, "content-type") == ["text/markdown; charset=utf-8"] + end + + test "render markdown with text/*;q=0.5" do + conn = + conn(:get, "/") + |> put_req_header("accept", "text/*;q=0.5") + |> render([], fn -> raise "oops" end) + + assert get_resp_header(conn, "content-type") == ["text/markdown; charset=utf-8"] + end + + test "render markdown with */*;q=0.5" do + conn = + conn(:get, "/") + |> put_req_header("accept", "*/*;q=0.5") + |> render([], fn -> raise "oops" end) + + assert get_resp_header(conn, "content-type") == ["text/markdown; charset=utf-8"] + end end