Repository: phoenixframework/phoenix
Pull request #4992 (2022-09-30 16:06:45)

Add test for static paths
This is a just an extra passing test - I thought I found a bug here but I was confused -- having an explicit test here might be useful for the future.


Patch:
diff --git a/installer/templates/phx_web/components.ex b/installer/templates/phx_web/components.ex
index 2b304be2d..abe0a881f 100644
--- a/installer/templates/phx_web/components.ex
+++ b/installer/templates/phx_web/components.ex
@@ -245,7 +245,7 @@ defmodule <%= @web_namespace %>.Components do
   attr :type, :string,
     default: "text",
     values: ~w(checkbox color date datetime-local email file hidden month number password
-               range radio search tel text time url week)
+               range radio search select tel text time url week)
 
   attr :value, :any
   attr :field, :any, doc: "a %Phoenix.HTML.Form{}/field name tuple, for example: {f, :email}"
diff --git a/priv/templates/phx.gen.live/components.ex b/priv/templates/phx.gen.live/components.ex
index 2b304be2d..abe0a881f 100644
--- a/priv/templates/phx.gen.live/components.ex
+++ b/priv/templates/phx.gen.live/components.ex
@@ -245,7 +245,7 @@ defmodule <%= @web_namespace %>.Components do
   attr :type, :string,
     default: "text",
     values: ~w(checkbox color date datetime-local email file hidden month number password
-               range radio search tel text time url week)
+               range radio search select tel text time url week)
 
   attr :value, :any
   attr :field, :any, doc: "a %Phoenix.HTML.Form{}/field name tuple, for example: {f, :email}"


Test patch:
diff --git a/test/phoenix/verified_routes_test.exs b/test/phoenix/verified_routes_test.exs
index b2d29e060..0016ec349 100644
--- a/test/phoenix/verified_routes_test.exs
+++ b/test/phoenix/verified_routes_test.exs
@@ -65,11 +65,19 @@ defmodule Phoenix.VerifiedRoutesTest do
     def static_path(path), do: "/api" <> path
   end
 
-  defp conn_with_endpoint do
-    conn(:get, "/") |> Plug.Conn.put_private(:phoenix_endpoint, Endpoint)
+  defmodule StaticPath do
+    def url, do: "https://example.com"
+    def static_url, do: "https://example.com"
+    def path(path), do: path
+    def static_path(path), do: "/static" <> path
+    def static_integrity(_path), do: nil
   end
 
-  defp socket_with_endpoint, do: %Phoenix.Socket{endpoint: Endpoint}
+  defp conn_with_endpoint(endpoint \\ Endpoint) do
+    conn(:get, "/") |> Plug.Conn.put_private(:phoenix_endpoint, endpoint)
+  end
+
+  defp socket_with_endpoint(endpoint \\ Endpoint), do: %Phoenix.Socket{endpoint: endpoint}
 
   def conn_with_script_name(script_name \\ ~w(api)) do
     conn = Plug.Conn.put_private(conn(:get, "/"), :phoenix_endpoint, ScriptName)
@@ -321,6 +329,26 @@ defmodule Phoenix.VerifiedRoutesTest do
     assert ~p"/posts/5/?#{%{"id" => "foo bar"}}" == "/posts/5/?id=foo+bar"
   end
 
+  describe "with static path" do
+    @endpoint StaticPath
+    @router Router
+    test "paths use static prefix" do
+      assert ~p"/images/foo.png" == "/static/images/foo.png"
+
+      assert path(conn_with_endpoint(StaticPath), ~p"/images/foo.png") ==
+               "/static/images/foo.png"
+
+      assert path(socket_with_endpoint(StaticPath), ~p"/images/foo.png") ==
+               "/static/images/foo.png"
+
+      assert url(conn_with_endpoint(StaticPath), ~p"/images/foo.png") ==
+               "https://example.com/static/images/foo.png"
+
+      assert url(socket_with_endpoint(StaticPath), ~p"/images/foo.png") ==
+               "https://example.com/static/images/foo.png"
+    end
+  end
+
   describe "with script name" do
     @endpoint ScriptName
     @router Router