Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make Router.reachable/2 less strict #698

Merged
merged 7 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/beacon/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,24 @@ defmodule Beacon.Router do
# match and invalidate the `beacon_site` mount.
def reachable?(%Beacon.Config{} = config, opts \\ []) do
%{site: site, endpoint: endpoint, router: router} = config

host = Keyword.get_lazy(opts, :host, fn -> endpoint.host() end)
prefix = router.__beacon_scoped_prefix_for_site__(site)

prefix =
Keyword.get_lazy(opts, :prefix, fn ->
router.__beacon_scoped_prefix_for_site__(site)
end)

case Phoenix.Router.route_info(router, "GET", prefix, host) do
# bypass and allow booting beacon sites even though there's a route conflict
# but only for root paths, for example:
# live /
# beacon_site /
# that's because even though they share the same prefix,
# beacon can still serve pages at /:page
%{route: "/"} ->
true

%{phoenix_live_view: {Beacon.Web.PageLive, _, _, %{extra: %{session: %{"beacon_site" => ^site}}}}} ->
true

Expand Down
22 changes: 18 additions & 4 deletions test/beacon/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,28 @@ defmodule Beacon.RouterTest do
end

describe "reachable?" do
test "test" do
setup do
site = :host_test
config = Beacon.Config.fetch!(site)
valid_host = "host.com"
[config: config]
end

test "match existing host", %{config: config} do
valid_host = "host.com"
assert Router.reachable?(config, host: valid_host)
refute Router.reachable?(%{config | site: :my_site}, host: valid_host)
refute Router.reachable?(config, host: "other.com")
end

test "existing nested conflicting route", %{config: config} do
valid_host = "host.com"
refute Router.reachable?(config, host: valid_host, prefix: "/nested/some_page")
end

test "with no specific host", %{config: config} do
assert Router.reachable?(config, host: nil)
end

test "do not match any existing host/path", %{config: config} do
refute Router.reachable?(config, host: nil, prefix: "/nested/invalid")
end
end
end
4 changes: 4 additions & 0 deletions test/support/live_views.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Beacon.BeaconTest.LiveViews.FooBarLive do
use Phoenix.LiveView
def render(assigns), do: ~H""
end
11 changes: 10 additions & 1 deletion test/support/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ defmodule Beacon.BeaconTest.Router do
beacon_site "/media", site: :s3_site
end

# test :host
scope "/" do
pipe_through :browser

live_session :default do
live "/", Beacon.BeaconTest.LiveViews.FooBarLive
live "/nested/:page", Beacon.BeaconTest.LiveViews.FooBarLive
end
end

scope path: "/", host: "host.com" do
pipe_through :browser
beacon_site "/", site: :host_test
end

Expand Down
Loading