Skip to content

Commit 319958d

Browse files
authored
Merge pull request #242 from tallarium/no-merge-cookies-edited
fix(req): do not merge set-cookie response headers
2 parents 420e97a + fbcf484 commit 319958d

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/reverse_proxy_plug/http_client/adapters/req.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,16 @@ if Code.ensure_loaded?(Req) do
118118
end
119119

120120
defp normalize_headers(headers) do
121-
Enum.map(headers, fn {k, v} -> {k, v |> List.wrap() |> Enum.join(", ")} end)
121+
Enum.flat_map(headers, fn
122+
{"set-cookie", values} when is_list(values) ->
123+
Enum.map(values, &{"set-cookie", &1})
124+
125+
{"set-cookie", value} ->
126+
[{"set-cookie", value}]
127+
128+
{k, v} ->
129+
[{k, v |> List.wrap() |> Enum.join(", ")}]
130+
end)
122131
end
123132
end
124133
end

test/http_client/adapters/req_test.exs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,44 @@ defmodule ReverseProxyPlug.HTTPClient.Adapters.ReqTest do
7373

7474
assert {:error, %Error{reason: :econnrefused}} == ReqClient.request(req)
7575
end
76+
77+
test "should not merge set-cookie headers with method #{method}", %{bypass: bypass} do
78+
path = "/my-resource"
79+
80+
req = %Request{
81+
method: unquote(method),
82+
options: [finch: FinchTest],
83+
url: "http://localhost:8000#{path}"
84+
}
85+
86+
Bypass.expect_once(bypass, fn %Plug.Conn{} = conn ->
87+
assert conn.method == req.method |> to_string() |> String.upcase()
88+
assert conn.request_path == path
89+
90+
conn
91+
|> Plug.Conn.prepend_resp_headers([{"set-cookie", "foo=bar; Path=/"}])
92+
|> Plug.Conn.prepend_resp_headers([{"set-cookie", "baz=quux; Path=/"}])
93+
|> Plug.Conn.send_resp(204, "")
94+
end)
95+
96+
assert {:ok, stream} = ReqClient.request_stream(req)
97+
98+
assert [
99+
{:status, 204},
100+
{:headers, headers}
101+
] = Enum.to_list(stream)
102+
103+
set_cookie_headers =
104+
Enum.filter(headers, fn
105+
{"set-cookie", _} -> true
106+
_ -> false
107+
end)
108+
109+
assert set_cookie_headers == [
110+
{"set-cookie", "foo=bar; Path=/"},
111+
{"set-cookie", "baz=quux; Path=/"}
112+
]
113+
end
76114
end
77115
end
78116
end

0 commit comments

Comments
 (0)