Improve design so existing tests do not break

This commit is contained in:
Mark Felder 2025-07-30 13:17:50 -07:00
commit 4217ababfc
3 changed files with 28 additions and 14 deletions

View file

@ -115,10 +115,19 @@ defmodule Pleroma.HTTP do
end end
defp adapter_middlewares(_, extra_middleware) do defp adapter_middlewares(_, extra_middleware) do
if Pleroma.Config.get(:env) == :test do # A lot of tests are written expecting unencoded URLs
# Emulate redirects in test env, which are handled by adapters in other environments # 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() default_middleware()
else
Pleroma.Config.get(:env) == :test ->
# Emulate redirects in test env, which are handled by adapters in other environments
[Tesla.Middleware.FollowRedirects]
true ->
extra_middleware extra_middleware
end end
end end

View file

@ -17,7 +17,17 @@ defmodule Pleroma.Tesla.Middleware.EncodeUrl do
@impl Tesla.Middleware @impl Tesla.Middleware
def call(%Tesla.Env{url: url} = env, next, _) do def call(%Tesla.Env{url: url} = env, next, _) do
url = url = encode_url(url)
env = %{env | url: url}
case Tesla.run(env, next) do
{:ok, env} -> {:ok, env}
err -> err
end
end
defp encode_url(url) when is_binary(url) do
URI.parse(url) URI.parse(url)
|> then(fn parsed -> |> then(fn parsed ->
path = encode_path(parsed.path) path = encode_path(parsed.path)
@ -26,13 +36,6 @@ defmodule Pleroma.Tesla.Middleware.EncodeUrl do
%{parsed | path: path, query: query} %{parsed | path: path, query: query}
end) end)
|> URI.to_string() |> URI.to_string()
env = %{env | url: url}
case Tesla.run(env, next) do
{:ok, env} -> {:ok, env}
err -> err
end
end end
defp encode_path(nil), do: nil defp encode_path(nil), do: nil

View file

@ -72,6 +72,8 @@ defmodule Pleroma.HTTPTest do
end end
test "URL encoding properly encodes URLs with spaces" do 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" url_with_space = "https://tsundere.love/emoji/Pack 1/koronebless.png"
result = HTTP.get(url_with_space) result = HTTP.get(url_with_space)