Skip to content

Commit 18cb6e2

Browse files
authored
Avoid connection pool issue between cloudflare and neurow (#41)
1 parent 0034888 commit 18cb6e2

File tree

4 files changed

+71
-55
lines changed

4 files changed

+71
-55
lines changed

neurow/integration_test/message_history_test.exs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,23 @@ defmodule Neurow.IntegrationTest.MessageHistoryTest do
7373
{"cache-control", "no-cache"},
7474
{"connection", "close"},
7575
{"content-type", "text/event-stream"},
76-
{"transfer-encoding", "chunked"}
7776
])
7877

79-
assert_receive %HTTPoison.AsyncChunk{chunk: error_sse_event}
78+
assert_receive %HTTPoison.AsyncChunk{chunk: body}
8079

81-
json_event = parse_sse_json_event(error_sse_event)
80+
json_event = parse_sse_json_event(body)
8281

83-
assert json_event.event == "neurow_error_bad_request"
82+
assert json_event.event == "neurow_error_400"
8483

85-
assert json_event.data == %{
86-
"errors" => [
87-
%{
88-
"error_code" => "invalid_last_event_id",
89-
"error_message" => "Wrong value for last-event-id"
90-
}
91-
]
92-
}
84+
assert json_event.data ==
85+
%{
86+
"errors" => [
87+
%{
88+
"error_code" => "invalid_last_event_id",
89+
"error_message" => "Wrong value for last-event-id"
90+
}
91+
]
92+
}
9393

9494
assert_receive %HTTPoison.AsyncEnd{}
9595
end,

neurow/lib/neurow/public_api/endpoint.ex

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ defmodule Neurow.PublicApi.Endpoint do
6161
:error ->
6262
conn
6363
|> send_http_error(
64-
:bad_request,
64+
400,
6565
:invalid_last_event_id,
6666
"Wrong value for last-event-id"
6767
)
@@ -83,7 +83,7 @@ defmodule Neurow.PublicApi.Endpoint do
8383
end
8484

8585
def send_forbidden(conn, error_code, error_message) do
86-
send_http_error(conn, :forbidden, error_code, error_message)
86+
send_http_error(conn, 403, error_code, error_message)
8787
end
8888

8989
def preflight_request(conn, _opts) do
@@ -133,16 +133,13 @@ defmodule Neurow.PublicApi.Endpoint do
133133

134134
now = :os.system_time(:seconds)
135135

136-
{:ok, conn} =
137-
conn
138-
|> put_resp_header("content-type", "text/event-stream")
139-
|> put_resp_header("access-control-allow-origin", origin)
140-
|> put_resp_header("cache-control", "no-cache")
141-
|> put_resp_header("connection", "close")
142-
|> send_chunked(http_status)
143-
|> chunk("id:#{now}\nevent: neurow_error_#{http_status}\ndata: #{response}\n\n")
144-
145136
conn
137+
|> put_resp_header("content-type", "text/event-stream")
138+
|> put_resp_header("access-control-allow-origin", origin)
139+
|> send_resp(
140+
http_status,
141+
"id:#{now}\nevent: neurow_error_#{http_status}\ndata: #{response}\n\n"
142+
)
146143
end
147144

148145
defp extract_last_event_id(conn) do

neurow/test/neurow/public_api/endpoint_test.exs

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,22 @@ defmodule Neurow.PublicApi.EndpointTest do
1212
conn(:get, "/v1/subscribe")
1313

1414
call(Neurow.PublicApi.Endpoint, conn, fn ->
15-
assert_receive {:send_chunked, 403}
16-
assert_receive {:chunk, body}
17-
18-
sse_event = parse_sse_json_event(body)
19-
20-
assert sse_event.event == "neurow_error_forbidden"
21-
22-
assert sse_event.data == %{
23-
"errors" => [
24-
%{
25-
"error_code" => "invalid_authorization_header",
26-
"error_message" => "Invalid authorization header"
27-
}
28-
]
29-
}
15+
assert_receive {:send_resp_status, 403}
16+
assert_receive {:send_resp_body, body}
17+
18+
json_event = parse_sse_json_event(body)
19+
20+
assert json_event.event == "neurow_error_403"
21+
22+
assert json_event.data ==
23+
%{
24+
"errors" => [
25+
%{
26+
"error_code" => "invalid_authorization_header",
27+
"error_message" => "Invalid authorization header"
28+
}
29+
]
30+
}
3031
end)
3132
end
3233

@@ -36,13 +37,14 @@ defmodule Neurow.PublicApi.EndpointTest do
3637
|> put_req_header("authorization", "Bearer bad_token")
3738

3839
call(Neurow.PublicApi.Endpoint, conn, fn ->
39-
assert_receive {:send_chunked, 403}
40-
assert_receive {:chunk, body}
41-
event = parse_sse_json_event(body)
40+
assert_receive {:send_resp_status, 403}
41+
assert_receive {:send_resp_body, body}
42+
43+
json_event = parse_sse_json_event(body)
4244

43-
assert event.event == "neurow_error_forbidden"
45+
assert json_event.event == "neurow_error_403"
4446

45-
assert event.data == %{
47+
assert json_event.data == %{
4648
"errors" => [
4749
%{
4850
"error_code" => "invalid_jwt_token",
@@ -122,13 +124,14 @@ defmodule Neurow.PublicApi.EndpointTest do
122124
)
123125

124126
call(Neurow.PublicApi.Endpoint, conn, fn ->
125-
assert_receive {:send_chunked, 400}
126-
assert_receive {:chunk, body}
127-
event = parse_sse_json_event(body)
127+
assert_receive {:send_resp_status, 400}
128+
assert_receive {:send_resp_body, body}
128129

129-
assert event.event == "neurow_error_bad_request"
130+
json_event = parse_sse_json_event(body)
130131

131-
assert event.data == %{
132+
assert json_event.event == "neurow_error_400"
133+
134+
assert json_event.data == %{
132135
"errors" => [
133136
%{
134137
"error_code" => "invalid_last_event_id",
@@ -466,12 +469,22 @@ defmodule Neurow.PublicApi.EndpointTest do
466469
conn(:get, "/v1/subscribe")
467470

468471
call(Neurow.PublicApi.Endpoint, conn, fn ->
469-
assert_receive {:send_chunked, 403}
470-
assert_receive {:chunk, body}
471-
472-
sse_event = parse_sse_event(body)
473-
474-
assert sse_event.event == "neurow_error_forbidden"
472+
assert_receive {:send_resp_status, 403}
473+
assert_receive {:send_resp_body, body}
474+
475+
json_event = parse_sse_json_event(body)
476+
477+
assert json_event.event == "neurow_error_403"
478+
479+
assert json_event.data ==
480+
%{
481+
"errors" => [
482+
%{
483+
"error_code" => "invalid_authorization_header",
484+
"error_message" => "Invalid authorization header"
485+
}
486+
]
487+
}
475488
end)
476489
end
477490

neurow/test/sse_helper.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,18 @@ defmodule SseHelper do
5858
send(state[:owner], {:chunk, body})
5959
Plug.Adapters.Test.Conn.chunk(state, body)
6060
end
61+
62+
def send_resp(state, status, headers, body) do
63+
send(state[:owner], {:send_resp_status, status})
64+
send(state[:owner], {:send_resp_body, body})
65+
Plug.Adapters.Test.Conn.send_resp(state, status, headers, body)
66+
end
6167
end
6268

6369
@doc """
6470
Tests interaction with a SSE request by:
6571
- Calling the plug endpoint in a children task,
66-
- Intercepts calls to `send_chunk` and `chunk` from the application plugs to send messages to the test process
72+
- Intercepts calls to `send_chunk`, `chunk` and `send_resp` from the application plugs to send messages to the test process
6773
"""
6874
def call(plug_endpoint, conn, assertion_fn, options \\ []) do
6975
instrumented_conn = conn |> instrument()

0 commit comments

Comments
 (0)