Skip to content

Commit 9464e11

Browse files
committed
correctly pass live header to request
1 parent 8f4dd15 commit 9464e11

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

lib/phoenix/sync/electric/client_adapter.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule Phoenix.Sync.Electric.ClientAdapter do
2525
method: :get,
2626
offset: params["offset"],
2727
shape_handle: params["handle"],
28-
live: params["live"],
28+
live: live?(params["live"]),
2929
next_cursor: params["cursor"]
3030
)
3131

@@ -44,6 +44,7 @@ defmodule Phoenix.Sync.Electric.ClientAdapter do
4444
end
4545

4646
defp normalise_method(method), do: method |> String.downcase() |> String.to_atom()
47+
defp live?(live), do: live == "true"
4748

4849
defp fetch_upstream(sync_client, conn, request) do
4950
response =

test/phoenix/sync/router_test.exs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,51 @@ defmodule Phoenix.Sync.RouterTest do
257257
%{"headers" => %{"operation" => "insert"}, "value" => %{"title" => "three"}}
258258
] = Jason.decode!(resp.resp_body)
259259
end
260+
261+
@tag table: {
262+
"todos",
263+
[
264+
"id int8 not null primary key generated always as identity",
265+
"title text",
266+
"completed boolean default false"
267+
]
268+
}
269+
@tag data: {
270+
"todos",
271+
["title", "completed"],
272+
[["one", false], ["two", false], ["three", true]]
273+
}
274+
test "cursor header is correctly returned", _ctx do
275+
resp =
276+
Phoenix.ConnTest.build_conn()
277+
|> Phoenix.ConnTest.get("/sync/query-where", %{offset: "-1"})
278+
279+
assert [handle] = Plug.Conn.get_resp_header(resp, "electric-handle")
280+
assert [offset] = Plug.Conn.get_resp_header(resp, "electric-offset")
281+
282+
resp =
283+
Phoenix.ConnTest.build_conn()
284+
|> Phoenix.ConnTest.get("/sync/query-where", %{
285+
offset: offset,
286+
handle: handle,
287+
live: false
288+
})
289+
290+
assert [handle] = Plug.Conn.get_resp_header(resp, "electric-handle")
291+
assert [offset] = Plug.Conn.get_resp_header(resp, "electric-offset")
292+
assert [_] = Plug.Conn.get_resp_header(resp, "electric-up-to-date")
293+
294+
resp =
295+
Phoenix.ConnTest.build_conn()
296+
|> Phoenix.ConnTest.get("/sync/query-where", %{
297+
offset: offset,
298+
handle: handle,
299+
live: true,
300+
cursor: ""
301+
})
302+
303+
assert ["0"] = Plug.Conn.get_resp_header(resp, "electric-cursor")
304+
end
260305
end
261306

262307
describe "Plug.Router - shape/2" do

test/support/electric_helpers.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ defmodule Support.ElectricHelpers do
9090
publication_name: publication_name,
9191
slot_name: "electric_test_slot_#{:erlang.phash2(stack_id)}",
9292
connection_opts: ctx.db_config,
93-
replication_slot_temporary?: true
93+
replication_slot_temporary?: true,
94+
long_poll_timeout: long_poll_timeout(ctx),
95+
allow_shape_deletion?: allow_shape_deletion(ctx)
9496
}
9597

9698
Map.put(config, :stack_config, config)
@@ -180,6 +182,6 @@ defmodule Support.ElectricHelpers do
180182

181183
defp max_age(ctx), do: Access.get(ctx, :max_age, 60)
182184
defp stale_age(ctx), do: Access.get(ctx, :stale_age, 300)
183-
defp long_poll_timeout(ctx), do: Access.get(ctx, :long_poll_timeout, 20_000)
185+
defp long_poll_timeout(ctx), do: Access.get(ctx, :long_poll_timeout, 20)
184186
defp allow_shape_deletion(ctx), do: Access.get(ctx, :allow_shape_deletion, false)
185187
end

0 commit comments

Comments
 (0)