Improve design so existing tests do not break
This commit is contained in:
parent
11d27349e3
commit
4217ababfc
3 changed files with 28 additions and 14 deletions
|
|
@ -115,11 +115,20 @@ 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
|
||||||
default_middleware()
|
# them hard to read. Tests will opt-in when we want to validate
|
||||||
else
|
# the encoding is being done correctly.
|
||||||
extra_middleware
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,15 +17,7 @@ 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)
|
||||||
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()
|
|
||||||
|
|
||||||
env = %{env | url: url}
|
env = %{env | url: url}
|
||||||
|
|
||||||
|
|
@ -35,6 +27,17 @@ defmodule Pleroma.Tesla.Middleware.EncodeUrl do
|
||||||
end
|
end
|
||||||
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(nil), do: nil
|
||||||
|
|
||||||
defp encode_path(path) when is_binary(path) do
|
defp encode_path(path) when is_binary(path) do
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue