Skip to content

Commit 63bece3

Browse files
committed
221 - fixed failing test and updated response code converting function
1 parent 1b2e98b commit 63bece3

File tree

6 files changed

+244
-108
lines changed

6 files changed

+244
-108
lines changed

docs/api-gateway.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ Configuration of such API endpoint might look like this:
212212
- Kinesis -> **not supported**
213213
- Nats -> `nats`
214214
215-
`http_async` means that correlated response has to be sent to internal `:4010/v2/responses` `POST` endpoint.
215+
`http_async` means that correlated response has to be sent to internal `:4010/v3/responses` `POST` endpoint.
216216
217217
#### Supported formats
218218
@@ -223,9 +223,10 @@ Message headers:
223223
```plaintext
224224
rig-correlation: "correlation_id_sent_by_rig"
225225
rig-response-code: "201"
226+
content-type: "application/json"
226227
```
227228
228-
> Both headers are required.
229+
> All headers are required.
229230
230231
Message body:
231232

lib/rig_inbound_gateway/api_proxy/handler/http.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ defmodule RigInboundGateway.ApiProxy.Handler.Http do
118118
|> Map.update!(:resp_headers, fn existing_headers ->
119119
existing_headers ++ Map.to_list(extra_headers)
120120
end)
121-
|> Conn.put_resp_content_type("application/json")
122121
|> Conn.send_resp(response_code, response)
123122
after
124123
response_timeout ->

lib/rig_inbound_gateway/api_proxy/handler/kafka.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ defmodule RigInboundGateway.ApiProxy.Handler.Kafka do
271271
|> Map.update!(:resp_headers, fn existing_headers ->
272272
existing_headers ++ Map.to_list(extra_headers)
273273
end)
274-
|> Conn.put_resp_content_type("application/json")
275274
|> Conn.send_resp(response_code, response)
276275
after
277276
conf.response_timeout ->

lib/rig_inbound_gateway/api_proxy/response_from_parser.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule RigInboundGateway.ApiProxy.ResponseFromParser do
2424
# forward extra haeders such as content-type
2525
extra_headers <- Map.drop(headers_map, ["rig-correlation", "rig-response-code"]),
2626
# convert status code to int if needed, HTTP headers can't have number as a value
27-
response_code <- to_int(raw_response_code),
27+
{:ok, response_code} <- to_int(raw_response_code),
2828
response_body <- try_encode(message) do
2929
Logger.debug(fn ->
3030
"Parsed binary HTTP response: body=#{inspect(response_body)}, code=#{
@@ -54,10 +54,10 @@ defmodule RigInboundGateway.ApiProxy.ResponseFromParser do
5454

5555
# ---
5656

57-
defp to_int(value) when is_integer(value), do: value
57+
defp to_int(value) when is_integer(value), do: {:ok, value}
5858

5959
defp to_int(string) when is_binary(string) do
60-
String.to_integer(string)
60+
{:ok, String.to_integer(string)}
6161
rescue
6262
ArgumentError -> {:error, {:not_an_integer, string}}
6363
end

test/rig_tests/proxy/response_from/async_http_test.exs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,70 @@ defmodule RigTests.Proxy.ResponseFrom.AsyncHttpTest do
8787
# ...the connection is closed and the status is OK:
8888
assert res_status == 201
8989
# ...extra headers are present
90-
assert Enum.member?(headers, {"content-type", "application/json; charset=utf-8"})
90+
assert Enum.member?(headers, {"content-type", "application/json;charset=utf-8"})
9191
# ...but the client got the response sent to the HTTP internal endpoint:
9292
assert Jason.decode!(res_body) == async_response
9393
end
94+
95+
test_with_server "Given response_from is set to http_async and response code is incorrect, the calling service should receive 400 and originating request should timeout." do
96+
test_name = "proxy-http-response-from-http-internal-binary-timeout"
97+
98+
api_id = "mock-#{test_name}-api"
99+
endpoint_id = "mock-#{test_name}-endpoint"
100+
endpoint_path = "/#{endpoint_id}"
101+
sync_response = %{"this response" => "the client never sees this response"}
102+
async_response = %{"message" => "this is the async response that reaches the client instead"}
103+
104+
route(endpoint_path, fn %{query: %{"correlation" => correlation_id}} ->
105+
%Plug.Conn{status: res_status, resp_body: res_body} =
106+
build_conn()
107+
|> put_req_header("rig-correlation", correlation_id)
108+
|> put_req_header("rig-response-code", "abc201")
109+
|> put_req_header("content-type", "application/json;charset=utf-8")
110+
|> post("/v3/responses", Jason.encode!(async_response))
111+
112+
assert res_status == 400
113+
assert res_body == "Failed to parse request body: {:error, {:not_an_integer, \"abc201\"}}"
114+
115+
Response.ok!(sync_response, %{"content-type" => "application/json"})
116+
end)
117+
118+
# We register the endpoint with the proxy:
119+
rig_api_url = "http://localhost:#{@api_port}/v3/apis"
120+
rig_proxy_url = "http://localhost:#{@proxy_port}"
121+
122+
body =
123+
Jason.encode!(%{
124+
id: api_id,
125+
name: "Mock API",
126+
version_data: %{
127+
default: %{
128+
endpoints: [
129+
%{
130+
id: endpoint_id,
131+
method: "GET",
132+
path: endpoint_path,
133+
response_from: "http_async"
134+
}
135+
]
136+
}
137+
},
138+
proxy: %{
139+
target_url: "localhost",
140+
port: FakeServer.port()
141+
}
142+
})
143+
144+
headers = [{"content-type", "application/json"}]
145+
HTTPoison.post!(rig_api_url, body, headers)
146+
147+
# The client calls the proxy endpoint:
148+
request_url = rig_proxy_url <> endpoint_path
149+
150+
assert catch_error(HTTPoison.get!(request_url)) == %HTTPoison.Error{id: nil, reason: :timeout}
151+
152+
# Now we can assert that...
153+
# ...the fake backend service has been called:
154+
assert FakeServer.hits() == 1
155+
end
94156
end

0 commit comments

Comments
 (0)