Skip to content

Commit

Permalink
Merge 8acf949 into 9f50116
Browse files Browse the repository at this point in the history
  • Loading branch information
iceniveth committed Dec 23, 2019
2 parents 9f50116 + 8acf949 commit c744168
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/accent/plugs/response.ex
Expand Up @@ -43,6 +43,7 @@ defmodule Accent.Plug.Response do
def init(opts \\ []) do
%{
header: opts[:header] || "accent",
default_case: opts[:default_case],
json_decoder:
opts[:json_decoder] ||
raise(ArgumentError, "Accent.Plug.Response expects a :json_decoder option"),
Expand All @@ -66,16 +67,16 @@ defmodule Accent.Plug.Response do
# private

defp before_send_callback(conn, opts) do
response_content_type =
request_content_type =
conn
|> get_resp_header("content-type")
|> get_req_header("content-type")
|> Enum.at(0)

# Note - we don't support "+json" content types, and probably shouldn't add
# as a general feature because they may have specifications for the param
# names - e.g. https://tools.ietf.org/html/rfc7265#page-6 that mean the
# translation would be inappropriate
is_json_response = String.contains?(response_content_type || "", "application/json")
is_json_response = String.contains?(request_content_type || "", "application/json")

if is_json_response do
json_decoder = opts[:json_decoder]
Expand Down Expand Up @@ -107,9 +108,16 @@ defmodule Accent.Plug.Response do
end

defp select_transformer(conn, opts) do
accent = get_req_header(conn, opts[:header]) |> Enum.at(0)
accent =
case get_req_header(conn, opts[:header]) do
[] -> opts[:default_case]
[accent] -> accent
end

supported_cases = opts[:supported_cases]

supported_cases[accent]
case_not_supported = is_nil(supported_cases[accent])

if case_not_supported, do: supported_cases[opts[:default_case]], else: supported_cases[accent]
end
end
44 changes: 44 additions & 0 deletions test/accent/plugs/response_test.exs
Expand Up @@ -63,6 +63,12 @@ defmodule Accent.Plug.ResponseTest do
}
} = opts
end

test "sets the \"default_case\" option to the value passed in" do
opts = Accent.Plug.Response.init(@default_opts ++ [default_case: "pascal"])

assert %{default_case: "pascal"} = opts
end
end

describe "call/2" do
Expand Down Expand Up @@ -112,5 +118,43 @@ defmodule Accent.Plug.ResponseTest do

assert conn.resp_body == "<p>This is not JSON, but it includes some hello_world</p>"
end

test "uses default case if specified" do
opts = Accent.Plug.Response.init(@default_opts ++ [default_case: "pascal"])

conn =
conn(:post, "/")
|> put_req_header("content-type", "application/json")
|> Accent.Plug.Response.call(opts)
|> Plug.Conn.send_resp(200, "{\"hello_world\":\"value\"}")

assert conn.resp_body == "{\"helloWorld\":\"value\"}"
end

test "does not use default case if header is supplied" do
opts = Accent.Plug.Response.init(@default_opts ++ [default_case: "pascal"])

conn =
conn(:post, "/")
|> put_req_header("content-type", "application/json")
|> put_req_header("accent", "snake")
|> Accent.Plug.Response.call(opts)
|> Plug.Conn.send_resp(200, "{\"hello_world\":\"value\"}")

assert conn.resp_body == "{\"hello_world\":\"value\"}"
end

test "uses default case if the specified case is not supported" do
opts = Accent.Plug.Response.init(@default_opts ++ [default_case: "pascal"])

conn =
conn(:post, "/")
|> put_req_header("content-type", "application/json")
|> put_req_header("accent", "snakes")
|> Accent.Plug.Response.call(opts)
|> Plug.Conn.send_resp(200, "{\"hello_world\":\"value\"}")

assert conn.resp_body == "{\"helloWorld\":\"value\"}"
end
end
end

0 comments on commit c744168

Please sign in to comment.