Skip to content

Commit 8a8b0c4

Browse files
author
Kevin Bader
committed
Format the kafka response_from tests a bit
1 parent 63bece3 commit 8a8b0c4

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

test/rig_tests/proxy/response_from/kafka_test.exs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,20 @@ defmodule RigTests.Proxy.ResponseFrom.KafkaTest do
4343
end
4444

4545
@tag :kafka
46-
test_with_server "Given response_from is set to Kafka and response is in binary mode, the custom http response code - 201 - should be used." do
47-
test_name = "proxy-http-response-from-kafka-binary-status-code"
46+
test_with_server "Given response_from is set to Kafka, the http response (code, content-type, body) is taken from the Kafka response topic instead of forwarding the backend's original response." do
47+
test_name = "proxy-http-response-from-kafka"
4848
kafka_config = kafka_config()
4949
{:ok, kafka_client} = RigKafka.start(kafka_config)
50+
%{response_topic: kafka_topic} = config()
5051

5152
api_id = "mock-#{test_name}-api"
5253
endpoint_id = "mock-#{test_name}-endpoint"
53-
%{response_topic: kafka_topic} = config()
5454
endpoint_path = "/#{endpoint_id}"
55+
5556
sync_response = %{"message" => "the client never sees this response"}
5657
async_response = %{"message" => "this is the async response that reaches the client instead"}
5758

58-
# The following service fake also shows how a real service should
59-
# wrap its response in a CloudEvent:
59+
# The fake backend service:
6060
route(endpoint_path, fn %{query: %{"correlation" => correlation_id}} ->
6161
kafka_config = kafka_config()
6262

@@ -77,7 +77,7 @@ defmodule RigTests.Proxy.ResponseFrom.KafkaTest do
7777
Response.ok!(sync_response, %{"content-type" => "application/json"})
7878
end)
7979

80-
# We register the endpoint with the proxy:
80+
# We register this endpoint with the proxy:
8181
rig_api_url = "http://localhost:#{@api_port}/v2/apis"
8282
rig_proxy_url = "http://localhost:#{@proxy_port}"
8383

@@ -109,17 +109,15 @@ defmodule RigTests.Proxy.ResponseFrom.KafkaTest do
109109
# The client calls the proxy endpoint:
110110
request_url = rig_proxy_url <> endpoint_path
111111

112+
# The client sees only the asynchronous response:
112113
%HTTPoison.Response{status_code: res_status, body: res_body, headers: headers} =
113114
HTTPoison.get!(request_url)
114115

115-
# Now we can assert that...
116-
# ...the fake backend service has been called:
117-
assert FakeServer.hits() == 1
118-
# ...the connection is closed and the status is OK:
116+
# The response code is taken from the async response (201) and not from the sync one (200):
119117
assert res_status == 201
120-
# ...extra headers are present
118+
# Extra headers are present:
121119
assert Enum.member?(headers, {"content-type", "application/json;charset=utf-8"})
122-
# ...but the client got the response sent to the Kafka topic:
120+
# The body is taken from the async response as well:
123121
assert Jason.decode!(res_body) == async_response
124122

125123
RigKafka.Client.stop_supervised(kafka_client)
@@ -202,9 +200,10 @@ defmodule RigTests.Proxy.ResponseFrom.KafkaTest do
202200
end
203201

204202
@tag :kafka
205-
test_with_server "Given response_from and target are set to Kafka, the http response is taken from the Kafka response topic instead of forwarding the backend's original response." do
203+
test_with_server "response_from and target can both be set to Kafka." do
206204
test_name = "proxy-http-response-from-kafka-target"
207205
topic = "rig-test"
206+
%{response_topic: response_topic} = config()
208207

209208
api_id = "mock-#{test_name}-api"
210209
endpoint_id = "mock-#{test_name}-endpoint"
@@ -219,7 +218,7 @@ defmodule RigTests.Proxy.ResponseFrom.KafkaTest do
219218
assert :ok ==
220219
RigKafka.produce(
221220
kafka_config,
222-
config().response_topic,
221+
response_topic,
223222
"",
224223
"response",
225224
Jason.encode!(async_response),
@@ -281,19 +280,17 @@ defmodule RigTests.Proxy.ResponseFrom.KafkaTest do
281280
}
282281
})
283282

284-
# consumer start tends to be slower sometimes, therefore we give it some time before producing
283+
# Wait for the Kafka consumer to get its assignments..
285284
:timer.sleep(15_000)
286285

287286
%HTTPoison.Response{status_code: res_status, body: res_body, headers: headers} =
288287
HTTPoison.post!(request_url, req_body)
289288

290-
# Now we can assert that...
291-
# ...the connection is closed and the status is OK:
289+
# Status and body are both taken from the Kafka response:
292290
assert res_status == 201
293-
# ...extra headers are present
294-
assert Enum.member?(headers, {"content-type", "application/json;charset=utf-8"})
295-
# ...but the client got the response sent to the Kafka topic:
296291
assert Jason.decode!(res_body) == async_response
292+
# Extra headers are present:
293+
assert Enum.member?(headers, {"content-type", "application/json;charset=utf-8"})
297294

298295
RigKafka.Client.stop_supervised(kafka_client)
299296
end

0 commit comments

Comments
 (0)