From 916aeb934f4a961948ba41f9ac9c0f52d986600f Mon Sep 17 00:00:00 2001 From: Alex S Date: Wed, 14 Aug 2019 20:08:15 +0300 Subject: [PATCH] adding host header for gun adapter --- lib/pleroma/http/connection.ex | 6 ++---- lib/pleroma/http/http.ex | 2 +- lib/pleroma/http/request_builder.ex | 9 +++++++++ test/http/request_builder_test.exs | 13 ++++++++++++- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/http/connection.ex b/lib/pleroma/http/connection.ex index ef1c1cabd..6cb26c0fe 100644 --- a/lib/pleroma/http/connection.ex +++ b/lib/pleroma/http/connection.ex @@ -9,8 +9,7 @@ defmodule Pleroma.HTTP.Connection do @options [ connect_timeout: 10_000, - timeout: 20_000, - protocols: [:http] + timeout: 20_000 ] @doc """ @@ -24,8 +23,7 @@ defmodule Pleroma.HTTP.Connection do def new(opts \\ []) do middleware = [Tesla.Middleware.FollowRedirects] adapter = Application.get_env(:tesla, :adapter) - options = options(opts) - Tesla.client(middleware, {adapter, options}) + Tesla.client(middleware, {adapter, options(opts)}) end # fetch http options diff --git a/lib/pleroma/http/http.ex b/lib/pleroma/http/http.ex index d493a8b4a..21c057f4b 100644 --- a/lib/pleroma/http/http.ex +++ b/lib/pleroma/http/http.ex @@ -45,9 +45,9 @@ defmodule Pleroma.HTTP do %{} |> Builder.method(method) + |> Builder.url(url) |> Builder.headers(headers) |> Builder.opts(options) - |> Builder.url(url) |> Builder.add_param(:body, :body, body) |> Builder.add_param(:query, :query, params) |> Enum.into([]) diff --git a/lib/pleroma/http/request_builder.ex b/lib/pleroma/http/request_builder.ex index 4e77870bd..6edf15bbe 100644 --- a/lib/pleroma/http/request_builder.ex +++ b/lib/pleroma/http/request_builder.ex @@ -53,6 +53,15 @@ defmodule Pleroma.HTTP.RequestBuilder do header_list end + # TODO: maybe we need this header for all adapters, so we won't fail on adapter change + header_list = + if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do + %{host: host} = URI.parse(request.url) + header_list ++ [{"host", host}] + else + header_list + end + Map.put_new(request, :headers, header_list) end diff --git a/test/http/request_builder_test.exs b/test/http/request_builder_test.exs index ea4dd087e..631730f65 100644 --- a/test/http/request_builder_test.exs +++ b/test/http/request_builder_test.exs @@ -3,7 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.HTTP.RequestBuilderTest do - use ExUnit.Case, async: true + use ExUnit.Case use Pleroma.Tests.Helpers alias Pleroma.HTTP.RequestBuilder @@ -21,6 +21,17 @@ defmodule Pleroma.HTTP.RequestBuilderTest do headers: [{"user-agent", Pleroma.Application.user_agent()}] } end + + test "it adds host header for gun adapter" do + adapter = Application.get_env(:tesla, :adapter) + Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun) + on_exit(fn -> Application.put_env(:tesla, :adapter, adapter) end) + + assert RequestBuilder.headers(%{url: "https://example.com"}, []) == %{ + headers: [{"host", "example.com"}], + url: "https://example.com" + } + end end describe "add_optional_params/3" do