Skip to content

Commit 1b2e98b

Browse files
committed
221 - added forwarding of extra headers for response_from
1 parent f235177 commit 1b2e98b

File tree

6 files changed

+28
-10
lines changed

6 files changed

+28
-10
lines changed

lib/rig_api/v3/responses.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ defmodule RigApi.V3.Responses do
3737
"""
3838
def create(%{req_headers: req_headers} = conn, message) do
3939
case ResponseFromParser.parse(req_headers, message) do
40-
{deserialized_pid, response_code, response} ->
40+
{deserialized_pid, response_code, response, extra_headers} ->
4141
Logger.debug(fn ->
4242
"HTTP response via internal HTTP to #{inspect(deserialized_pid)}: #{inspect(message)}"
4343
end)
4444

45-
send(deserialized_pid, {:response_received, response, response_code})
45+
send(deserialized_pid, {:response_received, response, response_code, extra_headers})
4646
send_resp(conn, :accepted, "message sent to correlated reverse proxy request")
4747

4848
err ->

lib/rig_inbound_gateway/api_proxy/handler/http.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ defmodule RigInboundGateway.ApiProxy.Handler.Http do
103103
end
104104

105105
receive do
106-
{:response_received, response, response_code} ->
106+
{:response_received, response, response_code, extra_headers} ->
107107
ProxyMetrics.count_proxy_request(
108108
conn.method,
109109
conn.request_path,
@@ -115,6 +115,9 @@ defmodule RigInboundGateway.ApiProxy.Handler.Http do
115115
conn
116116
|> with_cors()
117117
|> Tracing.Plug.put_resp_header(Tracing.context())
118+
|> Map.update!(:resp_headers, fn existing_headers ->
119+
existing_headers ++ Map.to_list(extra_headers)
120+
end)
118121
|> Conn.put_resp_content_type("application/json")
119122
|> Conn.send_resp(response_code, response)
120123
after

lib/rig_inbound_gateway/api_proxy/handler/kafka.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ defmodule RigInboundGateway.ApiProxy.Handler.Kafka do
4444
any()}
4545
def kafka_handler(message, headers) do
4646
case ResponseFromParser.parse(headers, message) do
47-
{deserialized_pid, response_code, response} ->
47+
{deserialized_pid, response_code, response, extra_headers} ->
4848
Logger.debug(fn ->
4949
"HTTP response via Kafka to #{inspect(deserialized_pid)}: #{inspect(message)}"
5050
end)
5151

52-
send(deserialized_pid, {:response_received, response, response_code})
52+
send(deserialized_pid, {:response_received, response, response_code, extra_headers})
5353

5454
err ->
5555
Logger.warn(fn -> "Parse error #{inspect(err)} for #{inspect(message)}" end)
@@ -257,7 +257,7 @@ defmodule RigInboundGateway.ApiProxy.Handler.Kafka do
257257
conf = config()
258258

259259
receive do
260-
{:response_received, response, response_code} ->
260+
{:response_received, response, response_code, extra_headers} ->
261261
ProxyMetrics.count_proxy_request(
262262
conn.method,
263263
conn.request_path,
@@ -268,6 +268,9 @@ defmodule RigInboundGateway.ApiProxy.Handler.Kafka do
268268

269269
conn
270270
|> Tracing.Plug.put_resp_header(Tracing.context())
271+
|> Map.update!(:resp_headers, fn existing_headers ->
272+
existing_headers ++ Map.to_list(extra_headers)
273+
end)
271274
|> Conn.put_resp_content_type("application/json")
272275
|> Conn.send_resp(response_code, response)
273276
after

lib/rig_inbound_gateway/api_proxy/response_from_parser.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ defmodule RigInboundGateway.ApiProxy.ResponseFromParser do
2121
{:ok, correlation_id} <- Map.fetch(headers_map, "rig-correlation"),
2222
{:ok, deserialized_pid} <- Codec.deserialize(correlation_id),
2323
{:ok, raw_response_code} <- Map.fetch(headers_map, "rig-response-code"),
24+
# forward extra haeders such as content-type
25+
extra_headers <- Map.drop(headers_map, ["rig-correlation", "rig-response-code"]),
2426
# convert status code to int if needed, HTTP headers can't have number as a value
2527
response_code <- to_int(raw_response_code),
2628
response_body <- try_encode(message) do
2729
Logger.debug(fn ->
28-
"Parsed binary HTTP response: body=#{inspect(response_body)}, code=#{inspect(response_code)}"
30+
"Parsed binary HTTP response: body=#{inspect(response_body)}, code=#{
31+
inspect(response_code)
32+
}, headers=#{inspect(extra_headers)}"
2933
end)
3034

31-
{deserialized_pid, response_code, response_body}
35+
{deserialized_pid, response_code, response_body, extra_headers}
3236
else
3337
err -> err
3438
end
@@ -51,6 +55,7 @@ defmodule RigInboundGateway.ApiProxy.ResponseFromParser do
5155
# ---
5256

5357
defp to_int(value) when is_integer(value), do: value
58+
5459
defp to_int(string) when is_binary(string) do
5560
String.to_integer(string)
5661
rescue

test/rig_tests/proxy/response_from/async_http_test.exs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ defmodule RigTests.Proxy.ResponseFrom.AsyncHttpTest do
7777

7878
# The client calls the proxy endpoint:
7979
request_url = rig_proxy_url <> endpoint_path
80-
%HTTPoison.Response{status_code: res_status, body: res_body} = HTTPoison.get!(request_url)
80+
81+
%HTTPoison.Response{status_code: res_status, body: res_body, headers: headers} =
82+
HTTPoison.get!(request_url)
8183

8284
# Now we can assert that...
8385
# ...the fake backend service has been called:
8486
assert FakeServer.hits() == 1
8587
# ...the connection is closed and the status is OK:
8688
assert res_status == 201
89+
# ...extra headers are present
90+
assert Enum.member?(headers, {"content-type", "application/json; charset=utf-8"})
8791
# ...but the client got the response sent to the HTTP internal endpoint:
8892
assert Jason.decode!(res_body) == async_response
8993
end

test/rig_tests/proxy/response_from/kafka_test.exs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,16 @@ defmodule RigTests.Proxy.ResponseFrom.KafkaTest do
108108
# The client calls the proxy endpoint:
109109
request_url = rig_proxy_url <> endpoint_path
110110

111-
%HTTPoison.Response{status_code: res_status, body: res_body} = HTTPoison.get!(request_url)
111+
%HTTPoison.Response{status_code: res_status, body: res_body, headers: headers} =
112+
HTTPoison.get!(request_url)
112113

113114
# Now we can assert that...
114115
# ...the fake backend service has been called:
115116
assert FakeServer.hits() == 1
116117
# ...the connection is closed and the status is OK:
117118
assert res_status == 201
119+
# ...extra headers are present
120+
assert Enum.member?(headers, {"content-type", "application/json; charset=utf-8"})
118121
# ...but the client got the response sent to the Kafka topic:
119122
assert Jason.decode!(res_body) == async_response
120123

0 commit comments

Comments
 (0)