Skip to content
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
19 changes: 17 additions & 2 deletions lib/plug/debugger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/plug/debugger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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