add http requests mock
authorMaksim Pechnikov <parallel588@gmail.com>
Sun, 2 Dec 2018 15:17:26 +0000 (18:17 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Tue, 4 Dec 2018 11:04:06 +0000 (14:04 +0300)
test/support/http_request_mock.ex [new file with mode: 0644]
test/web/salmon/salmon_test.exs
test/web/websub/websub_test.exs

diff --git a/test/support/http_request_mock.ex b/test/support/http_request_mock.ex
new file mode 100644 (file)
index 0000000..c3501c6
--- /dev/null
@@ -0,0 +1,80 @@
+defmodule HttpRequestMock do
+  def request(
+        %Tesla.Env{
+          url: url,
+          method: method,
+          headers: headers,
+          query: query,
+          body: body
+        } = _env
+      ) do
+    with {:ok, res} <- apply(__MODULE__, method, [url, query, body, headers]) do
+      res
+    else
+      {_, r} = error ->
+        IO.warn(r)
+        error
+    end
+  end
+
+  # GET Requests
+  #
+  def get(url, query \\ [], body \\ [], headers \\ [])
+
+  def get("https://social.heldscal.la/api/statuses/user_timeline/23211.atom", _, _, _) do
+    {:ok, %Tesla.Env{
+        status: 200,
+        body: File.read!(
+          "test/fixtures/httpoison_mock/https___social.heldscal.la_api_statuses_user_timeline_23211.atom.xml"
+        )}}
+  end
+
+  def get("https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/23211", _, _, _) do
+    {:ok, %Tesla.Env{
+        status: 200,
+        body: File.read!("test/fixtures/httpoison_mock/https___social.heldscal.la_user_23211.xml")}}
+  end
+
+  def get("http://social.heldscal.la/.well-known/host-meta", _, _, _) do
+    {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")}}
+  end
+
+  def get("https://social.heldscal.la/.well-known/host-meta", _, _, _) do
+    {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")}}
+  end
+
+  def get("https://mastodon.social/users/lambadalambda.atom", _, _, _) do
+    {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/lambadalambda.atom")}}
+  end
+
+  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)
+    }
+  end
+
+  def get(url, query, body, headers) do
+    {:error,
+     "Not implemented the mock response for get #{inspect(url)}, #{query}, #{inspect(body)}, #{
+     inspect(headers)
+     }"}
+  end
+
+
+  # POST Requests
+  #
+
+  def post(url, query \\ [], body \\ [], headers \\ [])
+
+  def post("http://example.org/needs_refresh", _, _, _) do
+    {:ok,
+     %Tesla.Env{
+       status: 200,
+       body: ""
+     }}
+  end
+
+  def post(url, _query, _body, _headers) do
+    {:error, "Not implemented the mock response for post #{inspect(url)}"}
+  end
+end
index 1b39b4b2d70d4c15dfcf8a821a6491e5a1f6a304..3285c11f0e8cfd0c6d65e3dedd8871ef6938781c 100644 (file)
@@ -3,6 +3,7 @@ defmodule Pleroma.Web.Salmon.SalmonTest do
   alias Pleroma.Web.Salmon
   alias Pleroma.{Repo, Activity, User}
   import Pleroma.Factory
+  import Tesla.Mock
 
   @magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
 
@@ -10,6 +11,10 @@ defmodule Pleroma.Web.Salmon.SalmonTest do
 
   @magickey_friendica "RSA.AMwa8FUs2fWEjX0xN7yRQgegQffhBpuKNC6fa5VNSVorFjGZhRrlPMn7TQOeihlc9lBz2OsHlIedbYn2uJ7yCs0.AQAB"
 
+  setup do
+    mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
+    :ok
+  end
   test "decodes a salmon" do
     {:ok, salmon} = File.read("test/fixtures/salmon.xml")
     {:ok, doc} = Salmon.decode_and_validate(@magickey, salmon)
index f3d2da81ab2672daac16c34e075023c54b293006..47d1a88e183feb04bb16f1b810810cd05083b198 100644 (file)
@@ -13,13 +13,7 @@ defmodule Pleroma.Web.WebsubTest do
   import Tesla.Mock
 
   setup do
-     mock fn
-      %{method: :get, url: "https://mastodon.social/users/lambadalambda.atom"} ->
-        %Tesla.Env{status: 200, body: File.read!("test/fixtures/lambadalambda.atom")}
-      %{method: :post, url: "http://example.org/needs_refresh"} ->
-        %Tesla.Env{status: 200, body: ""}
-     end
-
+    mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
     :ok
   end