c3501c6cf68cf49a50f32750b24cfa9f93c48b14
[akkoma] / test / support / http_request_mock.ex
1 defmodule HttpRequestMock do
2 def request(
3 %Tesla.Env{
4 url: url,
5 method: method,
6 headers: headers,
7 query: query,
8 body: body
9 } = _env
10 ) do
11 with {:ok, res} <- apply(__MODULE__, method, [url, query, body, headers]) do
12 res
13 else
14 {_, r} = error ->
15 IO.warn(r)
16 error
17 end
18 end
19
20 # GET Requests
21 #
22 def get(url, query \\ [], body \\ [], headers \\ [])
23
24 def get("https://social.heldscal.la/api/statuses/user_timeline/23211.atom", _, _, _) do
25 {:ok, %Tesla.Env{
26 status: 200,
27 body: File.read!(
28 "test/fixtures/httpoison_mock/https___social.heldscal.la_api_statuses_user_timeline_23211.atom.xml"
29 )}}
30 end
31
32 def get("https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/23211", _, _, _) do
33 {:ok, %Tesla.Env{
34 status: 200,
35 body: File.read!("test/fixtures/httpoison_mock/https___social.heldscal.la_user_23211.xml")}}
36 end
37
38 def get("http://social.heldscal.la/.well-known/host-meta", _, _, _) do
39 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")}}
40 end
41
42 def get("https://social.heldscal.la/.well-known/host-meta", _, _, _) do
43 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")}}
44 end
45
46 def get("https://mastodon.social/users/lambadalambda.atom", _, _, _) do
47 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/lambadalambda.atom")}}
48 end
49
50 def get("https://social.heldscal.la/user/23211", _, _, [Accept: "application/activity+json"]) do
51 {:ok,
52 Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)
53 }
54 end
55
56 def get(url, query, body, headers) do
57 {:error,
58 "Not implemented the mock response for get #{inspect(url)}, #{query}, #{inspect(body)}, #{
59 inspect(headers)
60 }"}
61 end
62
63
64 # POST Requests
65 #
66
67 def post(url, query \\ [], body \\ [], headers \\ [])
68
69 def post("http://example.org/needs_refresh", _, _, _) do
70 {:ok,
71 %Tesla.Env{
72 status: 200,
73 body: ""
74 }}
75 end
76
77 def post(url, _query, _body, _headers) do
78 {:error, "Not implemented the mock response for post #{inspect(url)}"}
79 end
80 end