Skip to content

Commit

Permalink
Fix Hackney adapter
Browse files Browse the repository at this point in the history
Because header keys are case-insensitive in both HTTP/1.1 and HTTP/2, it is recommended for header keys to be in lowercase, to avoid sending duplicate keys in a request.
  • Loading branch information
amandasposito committed Feb 3, 2022
1 parent 6140bd4 commit f80d200
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/twirp/client/hackney.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule Twirp.Client.Hackney do

with {:ok, status, headers, ref} <- :hackney.request(:post, path, ctx.headers, payload, options),
{:ok, body} <- :hackney.body(ref) do
{:ok, %{status: status, headers: headers, body: body}}
{:ok, %{status: status, headers: format_headers(headers), body: body}}
else
{:error, :timeout} ->
{:error, %{reason: :timeout}}
Expand All @@ -43,4 +43,10 @@ defmodule Twirp.Client.Hackney do
error
end
end

defp format_headers(headers) do
for {key, value} <- headers do
{String.downcase(to_string(key)), to_string(value)}
end
end
end
3 changes: 1 addition & 2 deletions lib/twirp/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ defmodule Twirp.Plug do

def call(%{path_info: [full_name, method]}=conn, {%{full_name: full_name}=service, handler, hooks}) do
env = %{}
metadata = %{
}
metadata = %{}
start = Telemetry.start(:call, metadata)

try do
Expand Down
17 changes: 17 additions & 0 deletions test/twirp/client/hackney_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ defmodule Twirp.Client.HackneyTest do
assert match?(%Error{code: :internal}, resp)
end

test "normalize headers", %{service: service} do
Bypass.expect(service, fn conn ->
assert Plug.Conn.get_req_header(conn, "content-type") == ["application/protobuf"]
{:ok, body, conn} = Plug.Conn.read_body(conn)
assert %Req{msg: "test"} == Req.decode(body)

body = Resp.encode(Resp.new(msg: "test"))

conn
|> Plug.Conn.put_resp_header("Content-Type", "application/protobuf")
|> Plug.Conn.resp(200, body)
end)

resp = Client.echo(Req.new(msg: "test"))
assert {:ok, Resp.new(msg: "test")} == resp
end

test "no headers are returned", %{service: service} do
Bypass.expect(service, fn conn ->
conn
Expand Down

0 comments on commit f80d200

Please sign in to comment.