headers standardisation for tesla

This commit is contained in:
Alex S 2019-08-07 20:06:43 +03:00
parent b233875ef5
commit b383d85b9b
8 changed files with 72 additions and 55 deletions

View File

@ -48,7 +48,7 @@ defmodule Pleroma.HTTP.RequestBuilder do
def headers(request, header_list) do def headers(request, header_list) do
header_list = header_list =
if Pleroma.Config.get([:http, :send_user_agent]) do if Pleroma.Config.get([:http, :send_user_agent]) do
header_list ++ [{"User-Agent", Pleroma.Application.user_agent()}] header_list ++ [{"user-agent", Pleroma.Application.user_agent()}]
else else
header_list header_list
end end

View File

@ -95,7 +95,7 @@ defmodule Pleroma.Object.Fetcher do
date: date date: date
}) })
[{:Signature, signature}] [{"signature", signature}]
end end
defp sign_fetch(headers, id, date) do defp sign_fetch(headers, id, date) do
@ -108,7 +108,7 @@ defmodule Pleroma.Object.Fetcher do
defp maybe_date_fetch(headers, date) do defp maybe_date_fetch(headers, date) do
if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do if Pleroma.Config.get([:activitypub, :sign_object_fetches]) do
headers ++ [{:Date, date}] headers ++ [{"date", date}]
else else
headers headers
end end
@ -122,7 +122,7 @@ defmodule Pleroma.Object.Fetcher do
|> Timex.format!("{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT") |> Timex.format!("{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
headers = headers =
[{:Accept, "application/activity+json"}] [{"accept", "application/activity+json"}]
|> maybe_date_fetch(date) |> maybe_date_fetch(date)
|> sign_fetch(id, date) |> sign_fetch(id, date)

View File

@ -373,7 +373,7 @@ defmodule Pleroma.Web.OStatus do
{:ok, %{body: body, status: code}} when code in 200..299 <- {:ok, %{body: body, status: code}} when code in 200..299 <-
HTTP.get( HTTP.get(
url, url,
[{:Accept, "application/atom+xml"}] [{"accept", "application/atom+xml"}]
) do ) do
Logger.debug("Got document from #{url}, handling...") Logger.debug("Got document from #{url}, handling...")
handle_incoming(body, options) handle_incoming(body, options)

View File

@ -217,7 +217,7 @@ defmodule Pleroma.Web.WebFinger do
with response <- with response <-
HTTP.get( HTTP.get(
address, address,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
), ),
{:ok, %{status: status, body: body}} when status in 200..299 <- response do {:ok, %{status: status, body: body}} when status in 200..299 <- response do
doc = XML.parse_document(body) doc = XML.parse_document(body)

View File

@ -18,7 +18,7 @@ defmodule Pleroma.HTTP.RequestBuilderTest do
Pleroma.Config.put([:http, :send_user_agent], true) Pleroma.Config.put([:http, :send_user_agent], true)
assert RequestBuilder.headers(%{}, []) == %{ assert RequestBuilder.headers(%{}, []) == %{
headers: [{"User-Agent", Pleroma.Application.user_agent()}] headers: [{"user-agent", Pleroma.Application.user_agent()}]
} }
end end
end end

View File

@ -88,7 +88,7 @@ defmodule HttpRequestMock do
"https://osada.macgirvin.com/.well-known/webfinger?resource=acct:mike@osada.macgirvin.com", "https://osada.macgirvin.com/.well-known/webfinger?resource=acct:mike@osada.macgirvin.com",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -101,7 +101,7 @@ defmodule HttpRequestMock do
"https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/29191", "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/29191",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -122,7 +122,7 @@ defmodule HttpRequestMock do
"https://pawoo.net/.well-known/webfinger?resource=acct:https://pawoo.net/users/pekorino", "https://pawoo.net/.well-known/webfinger?resource=acct:https://pawoo.net/users/pekorino",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -148,7 +148,7 @@ defmodule HttpRequestMock do
"https://social.stopwatchingus-heidelberg.de/.well-known/webfinger?resource=acct:https://social.stopwatchingus-heidelberg.de/user/18330", "https://social.stopwatchingus-heidelberg.de/.well-known/webfinger?resource=acct:https://social.stopwatchingus-heidelberg.de/user/18330",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -169,7 +169,7 @@ defmodule HttpRequestMock do
"https://mamot.fr/.well-known/webfinger?resource=acct:https://mamot.fr/users/Skruyb", "https://mamot.fr/.well-known/webfinger?resource=acct:https://mamot.fr/users/Skruyb",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -182,7 +182,7 @@ defmodule HttpRequestMock do
"https://social.heldscal.la/.well-known/webfinger?resource=nonexistant@social.heldscal.la", "https://social.heldscal.la/.well-known/webfinger?resource=nonexistant@social.heldscal.la",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -195,7 +195,7 @@ defmodule HttpRequestMock do
"https://squeet.me/xrd/?uri=lain@squeet.me", "https://squeet.me/xrd/?uri=lain@squeet.me",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -208,7 +208,7 @@ defmodule HttpRequestMock do
"https://mst3k.interlinked.me/users/luciferMysticus", "https://mst3k.interlinked.me/users/luciferMysticus",
_, _,
_, _,
Accept: "application/activity+json" [{"accept", "application/activity+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -229,7 +229,7 @@ defmodule HttpRequestMock do
"https://hubzilla.example.org/channel/kaniini", "https://hubzilla.example.org/channel/kaniini",
_, _,
_, _,
Accept: "application/activity+json" [{"accept", "application/activity+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -238,7 +238,7 @@ defmodule HttpRequestMock do
}} }}
end end
def get("https://niu.moe/users/rye", _, _, Accept: "application/activity+json") do def get("https://niu.moe/users/rye", _, _, [{"accept", "application/activity+json"}]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -246,7 +246,7 @@ defmodule HttpRequestMock do
}} }}
end end
def get("https://n1u.moe/users/rye", _, _, Accept: "application/activity+json") do def get("https://n1u.moe/users/rye", _, _, [{"accept", "application/activity+json"}]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -265,7 +265,7 @@ defmodule HttpRequestMock do
}} }}
end end
def get("https://puckipedia.com/", _, _, Accept: "application/activity+json") do def get("https://puckipedia.com/", _, _, [{"accept", "application/activity+json"}]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -321,7 +321,9 @@ defmodule HttpRequestMock do
}} }}
end end
def get("http://mastodon.example.org/users/admin", _, _, Accept: "application/activity+json") do def get("http://mastodon.example.org/users/admin", _, _, [
{"accept", "application/activity+json"}
]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -329,7 +331,9 @@ defmodule HttpRequestMock do
}} }}
end end
def get("http://mastodon.example.org/users/gargron", _, _, Accept: "application/activity+json") do def get("http://mastodon.example.org/users/gargron", _, _, [
{"accept", "application/activity+json"}
]) do
{:error, :nxdomain} {:error, :nxdomain}
end end
@ -337,7 +341,7 @@ defmodule HttpRequestMock do
"http://mastodon.example.org/@admin/99541947525187367", "http://mastodon.example.org/@admin/99541947525187367",
_, _,
_, _,
Accept: "application/activity+json" [{"accept", "application/activity+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -354,7 +358,7 @@ defmodule HttpRequestMock do
}} }}
end end
def get("https://mstdn.io/users/mayuutann", _, _, Accept: "application/activity+json") do def get("https://mstdn.io/users/mayuutann", _, _, [{"accept", "application/activity+json"}]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -366,7 +370,7 @@ defmodule HttpRequestMock do
"https://mstdn.io/users/mayuutann/statuses/99568293732299394", "https://mstdn.io/users/mayuutann/statuses/99568293732299394",
_, _,
_, _,
Accept: "application/activity+json" [{"accept", "application/activity+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -386,7 +390,7 @@ defmodule HttpRequestMock do
}} }}
end end
def get(url, _, _, Accept: "application/xrd+xml,application/jrd+json") def get(url, _, _, [{"accept", "application/xrd+xml,application/jrd+json"}])
when url in [ when url in [
"https://pleroma.soykaf.com/.well-known/webfinger?resource=acct:https://pleroma.soykaf.com/users/lain", "https://pleroma.soykaf.com/.well-known/webfinger?resource=acct:https://pleroma.soykaf.com/users/lain",
"https://pleroma.soykaf.com/.well-known/webfinger?resource=https://pleroma.soykaf.com/users/lain" "https://pleroma.soykaf.com/.well-known/webfinger?resource=https://pleroma.soykaf.com/users/lain"
@ -413,7 +417,7 @@ defmodule HttpRequestMock do
"https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/1", "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/1",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -457,7 +461,7 @@ defmodule HttpRequestMock do
"https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/5381", "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/5381",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -510,7 +514,7 @@ defmodule HttpRequestMock do
"https://social.sakamoto.gq/.well-known/webfinger?resource=https://social.sakamoto.gq/users/eal", "https://social.sakamoto.gq/.well-known/webfinger?resource=https://social.sakamoto.gq/users/eal",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -523,7 +527,7 @@ defmodule HttpRequestMock do
"https://social.sakamoto.gq/objects/0ccc1a2c-66b0-4305-b23a-7f7f2b040056", "https://social.sakamoto.gq/objects/0ccc1a2c-66b0-4305-b23a-7f7f2b040056",
_, _,
_, _,
Accept: "application/atom+xml" [{"accept", "application/atom+xml"}]
) do ) do
{:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/sakamoto.atom")}} {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/sakamoto.atom")}}
end end
@ -540,7 +544,7 @@ defmodule HttpRequestMock do
"https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/lambadalambda", "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/lambadalambda",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -562,7 +566,7 @@ defmodule HttpRequestMock do
"http://gs.example.org/.well-known/webfinger?resource=http://gs.example.org:4040/index.php/user/1", "http://gs.example.org/.well-known/webfinger?resource=http://gs.example.org:4040/index.php/user/1",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -576,7 +580,7 @@ defmodule HttpRequestMock do
"http://gs.example.org:4040/index.php/user/1", "http://gs.example.org:4040/index.php/user/1",
_, _,
_, _,
Accept: "application/activity+json" [{"accept", "application/activity+json"}]
) do ) do
{:ok, %Tesla.Env{status: 406, body: ""}} {:ok, %Tesla.Env{status: 406, body: ""}}
end end
@ -612,7 +616,7 @@ defmodule HttpRequestMock do
"https://squeet.me/xrd?uri=lain@squeet.me", "https://squeet.me/xrd?uri=lain@squeet.me",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -625,7 +629,7 @@ defmodule HttpRequestMock do
"https://social.heldscal.la/.well-known/webfinger?resource=shp@social.heldscal.la", "https://social.heldscal.la/.well-known/webfinger?resource=shp@social.heldscal.la",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -638,7 +642,7 @@ defmodule HttpRequestMock do
"https://social.heldscal.la/.well-known/webfinger?resource=invalid_content@social.heldscal.la", "https://social.heldscal.la/.well-known/webfinger?resource=invalid_content@social.heldscal.la",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, %Tesla.Env{status: 200, body: ""}} {:ok, %Tesla.Env{status: 200, body: ""}}
end end
@ -655,7 +659,7 @@ defmodule HttpRequestMock do
"http://framatube.org/main/xrd?uri=framasoft@framatube.org", "http://framatube.org/main/xrd?uri=framasoft@framatube.org",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -677,7 +681,7 @@ defmodule HttpRequestMock do
"http://gnusocial.de/main/xrd?uri=winterdienst@gnusocial.de", "http://gnusocial.de/main/xrd?uri=winterdienst@gnusocial.de",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -714,7 +718,7 @@ defmodule HttpRequestMock do
"https://gerzilla.de/xrd/?uri=kaniini@gerzilla.de", "https://gerzilla.de/xrd/?uri=kaniini@gerzilla.de",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -772,7 +776,7 @@ defmodule HttpRequestMock do
{:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/lambadalambda.json")}} {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/lambadalambda.json")}}
end end
def get("https://social.heldscal.la/user/23211", _, _, Accept: "application/activity+json") do def get("https://social.heldscal.la/user/23211", _, _, [{"accept", "application/activity+json"}]) do
{:ok, Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)} {:ok, Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)}
end end
@ -889,7 +893,7 @@ defmodule HttpRequestMock do
"https://zetsubou.xn--q9jyb4c/.well-known/webfinger?resource=lain@zetsubou.xn--q9jyb4c", "https://zetsubou.xn--q9jyb4c/.well-known/webfinger?resource=lain@zetsubou.xn--q9jyb4c",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -902,7 +906,7 @@ defmodule HttpRequestMock do
"https://zetsubou.xn--q9jyb4c/.well-known/webfinger?resource=https://zetsubou.xn--q9jyb4c/users/lain", "https://zetsubou.xn--q9jyb4c/.well-known/webfinger?resource=https://zetsubou.xn--q9jyb4c/users/lain",
_, _,
_, _,
Accept: "application/xrd+xml,application/jrd+json" [{"accept", "application/xrd+xml,application/jrd+json"}]
) do ) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
@ -924,7 +928,9 @@ defmodule HttpRequestMock do
}} }}
end end
def get("https://info.pleroma.site/activity.json", _, _, Accept: "application/activity+json") do def get("https://info.pleroma.site/activity.json", _, _, [
{"accept", "application/activity+json"}
]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -936,7 +942,9 @@ defmodule HttpRequestMock do
{:ok, %Tesla.Env{status: 404, body: ""}} {:ok, %Tesla.Env{status: 404, body: ""}}
end end
def get("https://info.pleroma.site/activity2.json", _, _, Accept: "application/activity+json") do def get("https://info.pleroma.site/activity2.json", _, _, [
{"accept", "application/activity+json"}
]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
@ -948,7 +956,9 @@ defmodule HttpRequestMock do
{:ok, %Tesla.Env{status: 404, body: ""}} {:ok, %Tesla.Env{status: 404, body: ""}}
end end
def get("https://info.pleroma.site/activity3.json", _, _, Accept: "application/activity+json") do def get("https://info.pleroma.site/activity3.json", _, _, [
{"accept", "application/activity+json"}
]) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,

View File

@ -11,6 +11,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
import Pleroma.Factory import Pleroma.Factory
import Mock import Mock
import ExUnit.CaptureLog
setup do setup do
Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end) Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
@ -367,15 +368,18 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
assert html_response(response, 200) =~ "Remote follow" assert html_response(response, 200) =~ "Remote follow"
end end
test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do test "show follow page with error when user cannot fetch by `acct` link", %{conn: conn} do
user = insert(:user) user = insert(:user)
response = assert capture_log(fn ->
conn response =
|> assign(:user, user) conn
|> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found") |> assign(:user, user)
|> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found")
assert html_response(response, 200) =~ "Error fetching user" assert html_response(response, 200) =~ "Error fetching user"
end) =~
"Could not decode user at fetch https://mastodon.social/users/not_found, {:error, \"Object has been deleted\"}"
end end
end end

View File

@ -7,6 +7,7 @@ defmodule Pleroma.Web.WebFinger.WebFingerControllerTest do
import Pleroma.Factory import Pleroma.Factory
import Tesla.Mock import Tesla.Mock
import ExUnit.CaptureLog
setup do setup do
mock(fn env -> apply(HttpRequestMock, :request, [env]) end) mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
@ -75,11 +76,13 @@ defmodule Pleroma.Web.WebFinger.WebFingerControllerTest do
test "Sends a 404 when invalid format" do test "Sends a 404 when invalid format" do
user = insert(:user) user = insert(:user)
assert_raise Phoenix.NotAcceptableError, fn -> assert capture_log(fn ->
build_conn() assert_raise Phoenix.NotAcceptableError, fn ->
|> put_req_header("accept", "text/html") build_conn()
|> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost") |> put_req_header("accept", "text/html")
end |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
end
end) =~ "Internal server error:"
end end
test "Sends a 400 when resource param is missing" do test "Sends a 400 when resource param is missing" do