Add Pleroma.Support.Helpers.uri_query_sort/1 for easy sorting of a URL's query parameters

This commit is contained in:
Mark Felder 2024-01-15 17:07:13 -05:00
parent fc910f9bb9
commit e7c6410192

View File

@ -12,20 +12,26 @@ defmodule Pleroma.Tests.Helpers do
@doc "Accepts two URLs/URIs and sorts the query parameters before comparing" @doc "Accepts two URLs/URIs and sorts the query parameters before comparing"
def uri_equal?(a, b) do def uri_equal?(a, b) do
a_parsed = URI.parse(a) a_sorted = uri_query_sort(a)
b_parsed = URI.parse(b) b_sorted = uri_query_sort(b)
query_sort = fn query -> String.split(query, "&") |> Enum.sort() |> Enum.join("&") end
a_sorted_query = query_sort.(a_parsed.query)
b_sorted_query = query_sort.(b_parsed.query)
a_sorted = Map.put(a_parsed, :query, a_sorted_query)
b_sorted = Map.put(b_parsed, :query, b_sorted_query)
match?(^a_sorted, b_sorted) match?(^a_sorted, b_sorted)
end end
@doc "Accepts a URL/URI and sorts the query parameters"
def uri_query_sort(uri) do
parsed = URI.parse(uri)
sorted_query =
String.split(parsed.query, "&")
|> Enum.sort()
|> Enum.join("&")
parsed
|> Map.put(:query, sorted_query)
|> URI.to_string()
end
defmacro clear_config(config_path) do defmacro clear_config(config_path) do
quote do quote do
clear_config(unquote(config_path)) do clear_config(unquote(config_path)) do