logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/
commit: 4217ababfc0f75559d48e58cc9d966aae5059476
parent 11d27349e32d23649dd4e5ba6a3597f62199e6e5
Author: Mark Felder <feld@feld.me>
Date:   Wed, 30 Jul 2025 13:17:50 -0700

Improve design so existing tests do not break

Diffstat:

Mlib/pleroma/http.ex19++++++++++++++-----
Mlib/pleroma/tesla/middleware/encode_url.ex21++++++++++++---------
Mtest/pleroma/http_test.exs2++
3 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/lib/pleroma/http.ex b/lib/pleroma/http.ex @@ -115,11 +115,20 @@ defmodule Pleroma.HTTP do end defp adapter_middlewares(_, extra_middleware) do - if Pleroma.Config.get(:env) == :test do - # Emulate redirects in test env, which are handled by adapters in other environments - default_middleware() - else - extra_middleware + # A lot of tests are written expecting unencoded URLs + # and the burden of fixing that is high. Also it makes + # them hard to read. Tests will opt-in when we want to validate + # the encoding is being done correctly. + cond do + Pleroma.Config.get(:env) == :test and Pleroma.Config.get(:test_url_encoding) -> + default_middleware() + + Pleroma.Config.get(:env) == :test -> + # Emulate redirects in test env, which are handled by adapters in other environments + [Tesla.Middleware.FollowRedirects] + + true -> + extra_middleware end end diff --git a/lib/pleroma/tesla/middleware/encode_url.ex b/lib/pleroma/tesla/middleware/encode_url.ex @@ -17,15 +17,7 @@ defmodule Pleroma.Tesla.Middleware.EncodeUrl do @impl Tesla.Middleware def call(%Tesla.Env{url: url} = env, next, _) do - url = - URI.parse(url) - |> then(fn parsed -> - path = encode_path(parsed.path) - query = encode_query(parsed.query) - - %{parsed | path: path, query: query} - end) - |> URI.to_string() + url = encode_url(url) env = %{env | url: url} @@ -35,6 +27,17 @@ defmodule Pleroma.Tesla.Middleware.EncodeUrl do end end + defp encode_url(url) when is_binary(url) do + URI.parse(url) + |> then(fn parsed -> + path = encode_path(parsed.path) + query = encode_query(parsed.query) + + %{parsed | path: path, query: query} + end) + |> URI.to_string() + end + defp encode_path(nil), do: nil defp encode_path(path) when is_binary(path) do diff --git a/test/pleroma/http_test.exs b/test/pleroma/http_test.exs @@ -72,6 +72,8 @@ defmodule Pleroma.HTTPTest do end test "URL encoding properly encodes URLs with spaces" do + clear_config(:test_url_encoding, true) + url_with_space = "https://tsundere.love/emoji/Pack 1/koronebless.png" result = HTTP.get(url_with_space)