Skip to content

Commit cf22762

Browse files
authored
controller, router: Add content-type headers to embedded resposes (#35)
Fixes #29
1 parent cbf73e7 commit cf22762

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

lib/phoenix/sync/electric.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,13 @@ if Code.ensure_loaded?(Electric.Shapes.Api) do
568568
case Shapes.Api.validate(api, params) do
569569
{:ok, request} ->
570570
conn
571+
|> content_type()
571572
|> Plug.Conn.assign(:request, request)
572573
|> Shapes.Api.serve_shape_log(request)
573574

574575
{:error, response} ->
575576
conn
577+
|> content_type()
576578
|> Shapes.Api.Response.send(response)
577579
|> Plug.Conn.halt()
578580
end
@@ -582,11 +584,13 @@ if Code.ensure_loaded?(Electric.Shapes.Api) do
582584
case Shapes.Api.validate_for_delete(api, params) do
583585
{:ok, request} ->
584586
conn
587+
|> content_type()
585588
|> Plug.Conn.assign(:request, request)
586589
|> Shapes.Api.delete_shape(request)
587590

588591
{:error, response} ->
589592
conn
593+
|> content_type()
590594
|> Shapes.Api.Response.send(response)
591595
|> Plug.Conn.halt()
592596
end
@@ -595,5 +599,9 @@ if Code.ensure_loaded?(Electric.Shapes.Api) do
595599
def call(_api, %{method: "OPTIONS"} = conn, _params) do
596600
Shapes.Api.options(conn)
597601
end
602+
603+
defp content_type(conn) do
604+
Plug.Conn.put_resp_content_type(conn, "application/json")
605+
end
598606
end
599607
end

test/phoenix/sync/client_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ defmodule Phoenix.Sync.ClientTest do
44
parameterize: [
55
%{
66
sync_config: [
7+
env: :test,
78
mode: :embedded,
89
pool_opts: [backoff_type: :stop, max_restarts: 0, pool_size: 2]
910
]
1011
},
1112
%{
1213
sync_config: [
14+
env: :test,
1315
mode: :http,
1416
url: "http://localhost:3000",
1517
pool_opts: [backoff_type: :stop, max_restarts: 0, pool_size: 2]

test/phoenix/sync/controller_test.exs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ defmodule Phoenix.Sync.ControllerTest do
44
parameterize: [
55
%{
66
sync_config: [
7+
env: :test,
78
mode: :embedded,
89
pool_opts: [backoff_type: :stop, max_restarts: 0, pool_size: 2]
910
]
1011
},
1112
%{
1213
sync_config: [
1314
mode: :http,
15+
env: :test,
1416
url: "http://localhost:3000",
1517
pool_opts: [backoff_type: :stop, max_restarts: 0, pool_size: 2]
1618
]
@@ -156,8 +158,7 @@ defmodule Phoenix.Sync.ControllerTest do
156158

157159
describe "plug: sync_render/3" do
158160
setup(ctx) do
159-
opts = Phoenix.Sync.plug_opts(electric_opts(ctx))
160-
[plug_opts: [phoenix_sync: opts]]
161+
[plug_opts: [phoenix_sync: Phoenix.Sync.plug_opts(ctx.electric_opts)]]
161162
end
162163

163164
test "returns the sync events", ctx do
@@ -174,5 +175,15 @@ defmodule Phoenix.Sync.ControllerTest do
174175
%{"headers" => %{"operation" => "insert"}, "value" => %{"title" => "three"}}
175176
] = Jason.decode!(resp.resp_body)
176177
end
178+
179+
test "includes content-type header", ctx do
180+
conn = conn(:get, "/shape/todos", %{"offset" => "-1"})
181+
182+
resp = PlugRouter.call(conn, PlugRouter.init(ctx.plug_opts))
183+
184+
assert Plug.Conn.get_resp_header(resp, "content-type") == [
185+
"application/json; charset=utf-8"
186+
]
187+
end
177188
end
178189
end

test/phoenix/sync/electric_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ defmodule Phoenix.Sync.ElectricTest do
44
parameterize: [
55
%{
66
sync_config: [
7+
env: :test,
78
mode: :embedded,
89
pool_opts: [backoff_type: :stop, max_restarts: 0, pool_size: 2]
910
]
1011
},
1112
%{
1213
sync_config: [
14+
env: :test,
1315
mode: :http,
1416
url: "http://localhost:3000",
1517
pool_opts: [backoff_type: :stop, max_restarts: 0, pool_size: 2]

test/phoenix/sync/router_test.exs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ defmodule Phoenix.Sync.RouterTest do
66
parameterize: [
77
%{
88
sync_config: [
9+
env: :test,
910
mode: :embedded,
1011
pool_opts: @pool_opts
1112
]
1213
},
1314
%{
1415
sync_config: [
16+
env: :test,
1517
mode: :http,
1618
url: "http://localhost:3000",
1719
pool_opts: @pool_opts
@@ -135,6 +137,29 @@ defmodule Phoenix.Sync.RouterTest do
135137
] = Jason.decode!(resp.resp_body)
136138
end
137139

140+
@tag table: {
141+
"todos",
142+
[
143+
"id int8 not null primary key generated always as identity",
144+
"title text",
145+
"completed boolean default false"
146+
]
147+
}
148+
@tag data: {"todos", ["title"], [["one"], ["two"], ["three"]]}
149+
150+
test "returns a correct content-type header", _ctx do
151+
resp =
152+
Phoenix.ConnTest.build_conn()
153+
|> Phoenix.ConnTest.get("/sync/things-to-do", %{offset: "-1"})
154+
155+
assert resp.status == 200
156+
assert Plug.Conn.get_resp_header(resp, "electric-offset") == ["0_0"]
157+
158+
assert Plug.Conn.get_resp_header(resp, "content-type") == [
159+
"application/json; charset=utf-8"
160+
]
161+
end
162+
138163
@tag table: {
139164
"ideas",
140165
[
@@ -362,9 +387,7 @@ defmodule Phoenix.Sync.RouterTest do
362387
end
363388

364389
setup(ctx) do
365-
opts = Phoenix.Sync.plug_opts(electric_opts(ctx))
366-
367-
[plug_opts: [phoenix_sync: opts]]
390+
[plug_opts: [phoenix_sync: Phoenix.Sync.plug_opts(ctx.electric_opts)]]
368391
end
369392

370393
test "raises compile-time error if Plug.Router is not configured to copy_opts_to_assign" do

test/support/electric_helpers.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ defmodule Support.ElectricHelpers do
133133
end
134134

135135
def start_embedded(%{stack_config: stack_config, sync_config: sync_config} = ctx) do
136-
config = Keyword.merge(sync_config, Enum.to_list(stack_config))
136+
config = Keyword.merge(Enum.to_list(stack_config), sync_config)
137137

138138
{:ok, children} = Phoenix.Sync.Application.children(config)
139139

@@ -152,7 +152,11 @@ defmodule Support.ElectricHelpers do
152152
if endpoint == @endpoint && ctx.async,
153153
do: raise(RuntimeError, message: "do not use configure_endpoint in async tests")
154154

155-
Phoenix.Config.put(endpoint, :phoenix_sync, Phoenix.Sync.Application.plug_opts(electric_opts))
155+
Phoenix.Config.put(
156+
endpoint,
157+
:phoenix_sync,
158+
Phoenix.Sync.Application.plug_opts(electric_opts)
159+
)
156160

157161
[endpoint: endpoint]
158162
end

0 commit comments

Comments
 (0)