Skip to content

Commit 960b937

Browse files
authored
Merge pull request #202 from pow-auth/fix-req-response-headers
Normalize response header names in Assent.HTTPAdapter
2 parents d0f34d8 + 11b4586 commit 960b937

7 files changed

Lines changed: 61 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Now requires Elixir 1.15 / OTP 26.
1717
### Bug fixes
1818

1919
* `Assent.Strategy.OAuth` no longer lowercases the request path for signature base string, per RFC 5849 3.4.1.2
20+
* `Assent.HTTPAdapter.Req` now returns the response headers instead of the request headers
2021

2122
## v0.3.1 (2025-06-20)
2223

lib/assent/http_adapter.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ defmodule Assent.HTTPAdapter do
103103
|> http_adapter.request(url, body, headers, http_adapter_opts)
104104
|> case do
105105
{:ok, response} ->
106-
decode_response(response, opts)
106+
response
107+
|> normalize_headers()
108+
|> decode_response(opts)
107109

108110
{:error, error} ->
109111
{:error,
@@ -142,6 +144,10 @@ defmodule Assent.HTTPAdapter do
142144
end
143145
end
144146

147+
defp normalize_headers(%HTTPResponse{headers: headers} = response) do
148+
%{response | headers: Enum.map(headers, fn {key, value} -> {String.downcase(key), value} end)}
149+
end
150+
145151
@doc """
146152
Decodes request response body.
147153

lib/assent/http_adapter/httpc.ex

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ if Code.ensure_loaded?(:httpc) do
7979
end
8080

8181
defp format_response({:ok, {{_, status, _}, headers, body}}) do
82-
headers =
83-
Enum.map(headers, fn {key, value} ->
84-
{String.downcase(to_string(key)), to_string(value)}
85-
end)
86-
82+
headers = for {key, value} <- headers, do: {to_string(key), to_string(value)}
8783
body = IO.iodata_to_binary(body)
8884

8985
{:ok, %HTTPResponse{status: status, headers: headers, body: body}}

lib/assent/http_adapter/req.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ if Code.ensure_loaded?(Req) do
3434
|> Req.request()
3535
|> case do
3636
{:ok, response} ->
37-
headers =
38-
Enum.map(headers, fn {key, value} ->
39-
{String.downcase(to_string(key)), to_string(value)}
40-
end)
37+
headers = for {key, values} <- response.headers, value <- values, do: {key, value}
4138

4239
{:ok, %HTTPResponse{status: response.status, headers: headers, body: response.body}}
4340

test/assent/http_adapter/httpc_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ defmodule Assent.HTTPAdapter.HttpcTest do
102102
Httpc.request(:get, TestServer.url("/get?a=1"), nil, [])
103103
end
104104

105+
test "with response headers" do
106+
TestServer.add("/",
107+
via: :get,
108+
to: fn conn ->
109+
conn
110+
|> Plug.Conn.put_resp_header("x-header", "value")
111+
|> Plug.Conn.send_resp(200, "")
112+
end
113+
)
114+
115+
assert {:ok, %HTTPResponse{headers: headers}} =
116+
Httpc.request(:get, TestServer.url(), nil, [])
117+
118+
assert {"x-header", "value"} in headers
119+
end
120+
105121
test "with POST request" do
106122
TestServer.add("/post",
107123
via: :post,

test/assent/http_adapter/req_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ defmodule Assent.HTTPAdapter.ReqTest do
3030
Req.request(:get, url, nil, [], retry: false)
3131
end
3232

33+
test "with response headers" do
34+
TestServer.add("/",
35+
via: :get,
36+
to: fn conn ->
37+
conn
38+
|> Plug.Conn.put_resp_header("x-header", "value")
39+
|> Plug.Conn.send_resp(200, "")
40+
end
41+
)
42+
43+
assert {:ok, %HTTPResponse{headers: headers}} =
44+
Req.request(:get, TestServer.url(), nil, [], @req_opts)
45+
46+
assert {"x-header", "value"} in headers
47+
end
48+
3349
test "with POST request" do
3450
TestServer.add("/post",
3551
via: :post,

test/assent/http_adapter_test.exs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ defmodule Assent.HTTPAdapterTest do
5151
{:ok, %HTTPResponse{status: 200, headers: [], body: @json_library.encode!(%{"a" => 1})}}
5252
end
5353

54+
def request(:get, "uppercase-header-names", nil, [], nil) do
55+
{:ok,
56+
%HTTPResponse{
57+
status: 200,
58+
headers: [{"Content-Type", "application/json"}],
59+
body: @json_library.encode!(%{"a" => 1})
60+
}}
61+
end
62+
5463
def request(:get, "form-data-body", nil, [], nil) do
5564
{:ok,
5665
%HTTPResponse{
@@ -140,6 +149,16 @@ defmodule Assent.HTTPAdapterTest do
140149
request_url: "json-no-headers"
141150
}}
142151

152+
assert HTTPAdapter.request(:get, "uppercase-header-names", nil, [], http_adapter: HTTPMock) ==
153+
{:ok,
154+
%HTTPResponse{
155+
status: 200,
156+
headers: [{"content-type", "application/json"}],
157+
body: %{"a" => 1},
158+
http_adapter: HTTPMock,
159+
request_url: "uppercase-header-names"
160+
}}
161+
143162
assert HTTPAdapter.request(:get, "form-data-body", nil, [], http_adapter: HTTPMock) ==
144163
{:ok,
145164
%HTTPResponse{

0 commit comments

Comments
 (0)