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,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

View file

@ -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

View file

@ -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)