diff --git a/lib/twirp/plug.ex b/lib/twirp/plug.ex index 7ee4cb9..3f3e31c 100644 --- a/lib/twirp/plug.ex +++ b/lib/twirp/plug.ex @@ -105,7 +105,7 @@ defmodule Twirp.Plug do Telemetry.stop(:call, start, metadata) conn - |> put_resp_content_type(env.content_type) + |> put_resp_content_type(env.content_type, nil) |> send_resp(200, resp) |> halt() else @@ -279,11 +279,10 @@ defmodule Twirp.Plug do end defp send_error(conn, error) do - content_type = Encoder.type(:json) - body = Encoder.encode(error, nil, content_type) + body = Jason.encode!(error) conn - |> put_resp_content_type(content_type) + |> put_resp_content_type("application/json", nil) |> send_resp(Error.code_to_status(error.code), body) |> halt() end diff --git a/test/twirp/plug_test.exs b/test/twirp/plug_test.exs index 737c6b5..6559174 100644 --- a/test/twirp/plug_test.exs +++ b/test/twirp/plug_test.exs @@ -73,10 +73,9 @@ defmodule Twirp.PlugTest do end def content_type(conn) do - conn.resp_headers - |> Enum.find_value(fn {h, v} -> if h == "content-type", do: v, else: false end) - |> String.split(";") # drop the charset if there is one - |> Enum.at(0) + Enum.find_value(conn.resp_headers, fn {h, v} -> + if h == "content-type", do: v, else: false + end) end def call(req, opts \\ @opts) do @@ -112,6 +111,16 @@ defmodule Twirp.PlugTest do assert conn == req end + test "does not include the __exception__ field" do + req = conn(:get, "/twirp/plug.test.Haberdasher/MakeHat") + conn = call(req) + + assert conn.status == 404 + assert content_type(conn) == "application/json" + body = Jason.decode!(conn.resp_body) + refute Map.has_key?(body, "__exception__") + end + test "not a POST" do req = conn(:get, "/twirp/plug.test.Haberdasher/MakeHat") conn = call(req) @@ -299,7 +308,7 @@ defmodule Twirp.PlugTest do resp = Jason.decode!(conn.resp_body) assert resp["code"] == "internal" assert resp["msg"] == "Blow this ish up" - assert resp["meta"] == %{} + refute Map.has_key?(resp, "meta") end describe "before" do