tests WebFinger
This commit is contained in:
parent
b7fae304d3
commit
55341ac717
7 changed files with 141 additions and 24 deletions
24
lib/pleroma/plugs/set_format_plug.ex
Normal file
24
lib/pleroma/plugs/set_format_plug.ex
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Plugs.SetFormatPlug do
|
||||
import Plug.Conn, only: [assign: 3, fetch_query_params: 1]
|
||||
|
||||
def init(_), do: nil
|
||||
|
||||
def call(conn, _) do
|
||||
case get_format(conn) do
|
||||
nil -> conn
|
||||
format -> assign(conn, :format, format)
|
||||
end
|
||||
end
|
||||
|
||||
defp get_format(conn) do
|
||||
conn.private[:phoenix_format] ||
|
||||
case fetch_query_params(conn) do
|
||||
%{query_params: %{"_format" => format}} -> format
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -187,6 +187,7 @@ defmodule Pleroma.Web.WebFinger do
|
|||
end
|
||||
end
|
||||
|
||||
@spec finger(String.t()) :: {:ok, map()} | {:error, any()}
|
||||
def finger(account) do
|
||||
account = String.trim_leading(account, "@")
|
||||
|
||||
|
|
@ -220,8 +221,6 @@ defmodule Pleroma.Web.WebFinger do
|
|||
else
|
||||
with {:ok, doc} <- Jason.decode(body) do
|
||||
webfinger_from_json(doc)
|
||||
else
|
||||
{:error, e} -> e
|
||||
end
|
||||
end
|
||||
else
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ defmodule Pleroma.Web.WebFinger.WebFingerController do
|
|||
|
||||
alias Pleroma.Web.WebFinger
|
||||
|
||||
plug(Pleroma.Plugs.SetFormatPlug)
|
||||
plug(Pleroma.Web.FederatingPlug)
|
||||
|
||||
def host_meta(conn, _params) do
|
||||
|
|
@ -17,30 +18,28 @@ defmodule Pleroma.Web.WebFinger.WebFingerController do
|
|||
|> send_resp(200, xml)
|
||||
end
|
||||
|
||||
def webfinger(conn, %{"resource" => resource}) do
|
||||
case get_format(conn) do
|
||||
n when n in ["xml", "xrd+xml"] ->
|
||||
with {:ok, response} <- WebFinger.webfinger(resource, "XML") do
|
||||
conn
|
||||
|> put_resp_content_type("application/xrd+xml")
|
||||
|> send_resp(200, response)
|
||||
else
|
||||
_e -> send_resp(conn, 404, "Couldn't find user")
|
||||
end
|
||||
|
||||
n when n in ["json", "jrd+json"] ->
|
||||
with {:ok, response} <- WebFinger.webfinger(resource, "JSON") do
|
||||
json(conn, response)
|
||||
else
|
||||
_e -> send_resp(conn, 404, "Couldn't find user")
|
||||
end
|
||||
|
||||
_ ->
|
||||
send_resp(conn, 404, "Unsupported format")
|
||||
def webfinger(%{assigns: %{format: format}} = conn, %{"resource" => resource})
|
||||
when format in ["xml", "xrd+xml"] do
|
||||
with {:ok, response} <- WebFinger.webfinger(resource, "XML") do
|
||||
conn
|
||||
|> put_resp_content_type("application/xrd+xml")
|
||||
|> send_resp(200, response)
|
||||
else
|
||||
_e -> send_resp(conn, 404, "Couldn't find user")
|
||||
end
|
||||
end
|
||||
|
||||
def webfinger(conn, _params) do
|
||||
send_resp(conn, 400, "Bad Request")
|
||||
def webfinger(%{assigns: %{format: format}} = conn, %{"resource" => resource})
|
||||
when format in ["json", "jrd+json"] do
|
||||
with {:ok, response} <- WebFinger.webfinger(resource, "JSON") do
|
||||
json(conn, response)
|
||||
else
|
||||
_e ->
|
||||
conn
|
||||
|> put_status(404)
|
||||
|> json("Couldn't find user")
|
||||
end
|
||||
end
|
||||
|
||||
def webfinger(conn, _params), do: send_resp(conn, 400, "Bad Request")
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue