64c3d11b37cd59473608afa54f1b08cf066c0807
[akkoma] / test / support / http_request_mock.ex
1 defmodule HttpRequestMock do
2 require Logger
3
4 def request(
5 %Tesla.Env{
6 url: url,
7 method: method,
8 headers: headers,
9 query: query,
10 body: body
11 } = _env
12 ) do
13 with {:ok, res} <- apply(__MODULE__, method, [url, query, body, headers]) do
14 res
15 else
16 {_, r} = error ->
17 Logger.warn(r)
18 error
19 end
20 end
21
22 # GET Requests
23 #
24 def get(url, query \\ [], body \\ [], headers \\ [])
25
26 def get("https://pleroma.soykaf.com/users/lain/feed.atom", _, _, _) do
27 {:ok,
28 %Tesla.Env{
29 status: 200,
30 body:
31 File.read!(
32 "test/fixtures/httpoison_mock/https___pleroma.soykaf.com_users_lain_feed.atom.xml"
33 )
34 }}
35 end
36
37 def get(url, _, _, Accept: "application/xrd+xml,application/jrd+json")
38 when url in [
39 "https://pleroma.soykaf.com/.well-known/webfinger?resource=acct:https://pleroma.soykaf.com/users/lain",
40 "https://pleroma.soykaf.com/.well-known/webfinger?resource=https://pleroma.soykaf.com/users/lain"
41 ] do
42 {:ok,
43 %Tesla.Env{
44 status: 200,
45 body: File.read!("test/fixtures/httpoison_mock/https___pleroma.soykaf.com_users_lain.xml")
46 }}
47 end
48
49 def get("https://shitposter.club/api/statuses/user_timeline/1.atom", _, _, _) do
50 {:ok,
51 %Tesla.Env{
52 status: 200,
53 body:
54 File.read!(
55 "test/fixtures/httpoison_mock/https___shitposter.club_api_statuses_user_timeline_1.atom.xml"
56 )
57 }}
58 end
59
60 def get(
61 "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/1",
62 _,
63 _,
64 Accept: "application/xrd+xml,application/jrd+json"
65 ) do
66 {:ok,
67 %Tesla.Env{
68 status: 200,
69 body: File.read!("test/fixtures/httpoison_mock/https___shitposter.club_user_1.xml")
70 }}
71 end
72
73 def get("https://shitposter.club/notice/2827873", _, _, _) do
74 {:ok,
75 %Tesla.Env{
76 status: 200,
77 body:
78 File.read!("test/fixtures/httpoison_mock/https___shitposter.club_notice_2827873.html")
79 }}
80 end
81
82 def get("https://shitposter.club/api/statuses/show/2827873.atom", _, _, _) do
83 {:ok,
84 %Tesla.Env{
85 status: 200,
86 body:
87 File.read!(
88 "test/fixtures/httpoison_mock/https___shitposter.club_api_statuses_show_2827873.atom.xml"
89 )
90 }}
91 end
92
93 def get("https://testing.pleroma.lol/objects/b319022a-4946-44c5-9de9-34801f95507b", _, _, _) do
94 {:ok, %Tesla.Env{status: 200}}
95 end
96
97 def get("https://shitposter.club/api/statuses/user_timeline/5381.atom", _, _, _) do
98 {:ok,
99 %Tesla.Env{
100 status: 200,
101 body: File.read!("test/fixtures/httpoison_mock/spc_5381.atom")
102 }}
103 end
104
105 def get(
106 "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/5381",
107 _,
108 _,
109 Accept: "application/xrd+xml,application/jrd+json"
110 ) do
111 {:ok,
112 %Tesla.Env{
113 status: 200,
114 body: File.read!("test/fixtures/httpoison_mock/spc_5381_xrd.xml")
115 }}
116 end
117
118 def get("http://shitposter.club/.well-known/host-meta", _, _, _) do
119 {:ok,
120 %Tesla.Env{
121 status: 200,
122 body: File.read!("test/fixtures/httpoison_mock/shitposter.club_host_meta")
123 }}
124 end
125
126 def get("https://shitposter.club/api/statuses/show/7369654.atom", _, _, _) do
127 {:ok,
128 %Tesla.Env{
129 status: 200,
130 body: File.read!("test/fixtures/httpoison_mock/7369654.atom")
131 }}
132 end
133
134 def get("https://shitposter.club/notice/4027863", _, _, _) do
135 {:ok,
136 %Tesla.Env{
137 status: 200,
138 body: File.read!("test/fixtures/httpoison_mock/7369654.html")
139 }}
140 end
141
142 def get("https://social.sakamoto.gq/users/eal/feed.atom", _, _, _) do
143 {:ok,
144 %Tesla.Env{
145 status: 200,
146 body: File.read!("test/fixtures/httpoison_mock/sakamoto_eal_feed.atom")
147 }}
148 end
149
150 def get("http://social.sakamoto.gq/.well-known/host-meta", _, _, _) do
151 {:ok,
152 %Tesla.Env{
153 status: 200,
154 body: File.read!("test/fixtures/httpoison_mock/social.sakamoto.gq_host_meta")
155 }}
156 end
157
158 def get(
159 "https://social.sakamoto.gq/.well-known/webfinger?resource=https://social.sakamoto.gq/users/eal",
160 _,
161 _,
162 Accept: "application/xrd+xml,application/jrd+json"
163 ) do
164 {:ok,
165 %Tesla.Env{
166 status: 200,
167 body: File.read!("test/fixtures/httpoison_mock/eal_sakamoto.xml")
168 }}
169 end
170
171 def get("https://social.sakamoto.gq/objects/0ccc1a2c-66b0-4305-b23a-7f7f2b040056", _, _,
172 Accept: "application/atom+xml"
173 ) do
174 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/sakamoto.atom")}}
175 end
176
177 def get("http://mastodon.social/.well-known/host-meta", _, _, _) do
178 {:ok,
179 %Tesla.Env{
180 status: 200,
181 body: File.read!("test/fixtures/httpoison_mock/mastodon.social_host_meta")
182 }}
183 end
184
185 def get(
186 "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/lambadalambda",
187 _,
188 _,
189 Accept: "application/xrd+xml,application/jrd+json"
190 ) do
191 {:ok,
192 %Tesla.Env{
193 status: 200,
194 body:
195 File.read!(
196 "test/fixtures/httpoison_mock/https___mastodon.social_users_lambadalambda.xml"
197 )
198 }}
199 end
200
201 def get("http://gs.example.org/.well-known/host-meta", _, _, _) do
202 {:ok,
203 %Tesla.Env{
204 status: 200,
205 body: File.read!("test/fixtures/httpoison_mock/gs.example.org_host_meta")
206 }}
207 end
208
209 def get(
210 "http://gs.example.org/.well-known/webfinger?resource=http://gs.example.org:4040/index.php/user/1",
211 _,
212 _,
213 Accept: "application/xrd+xml,application/jrd+json"
214 ) do
215 {:ok,
216 %Tesla.Env{
217 status: 200,
218 body:
219 File.read!(
220 "test/fixtures/httpoison_mock/http___gs.example.org_4040_index.php_user_1.xml"
221 )
222 }}
223 end
224
225 def get("http://gs.example.org/index.php/api/statuses/user_timeline/1.atom", _, _, _) do
226 {:ok,
227 %Tesla.Env{
228 status: 200,
229 body:
230 File.read!(
231 "test/fixtures/httpoison_mock/http__gs.example.org_index.php_api_statuses_user_timeline_1.atom.xml"
232 )
233 }}
234 end
235
236 def get("https://social.heldscal.la/api/statuses/user_timeline/29191.atom", _, _, _) do
237 {:ok,
238 %Tesla.Env{
239 status: 200,
240 body:
241 File.read!(
242 "test/fixtures/httpoison_mock/https___social.heldscal.la_api_statuses_user_timeline_29191.atom.xml"
243 )
244 }}
245 end
246
247 def get("http://squeet.me/.well-known/host-meta", _, _, _) do
248 {:ok,
249 %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/squeet.me_host_meta")}}
250 end
251
252 def get("https://squeet.me/xrd?uri=lain@squeet.me", _, _,
253 Accept: "application/xrd+xml,application/jrd+json"
254 ) do
255 {:ok,
256 %Tesla.Env{
257 status: 200,
258 body: File.read!("test/fixtures/httpoison_mock/lain_squeet.me_webfinger.xml")
259 }}
260 end
261
262 def get(
263 "https://social.heldscal.la/.well-known/webfinger?resource=shp@social.heldscal.la",
264 _,
265 _,
266 Accept: "application/xrd+xml,application/jrd+json"
267 ) do
268 {:ok,
269 %Tesla.Env{
270 status: 200,
271 body: File.read!("test/fixtures/httpoison_mock/shp@social.heldscal.la.xml")
272 }}
273 end
274
275 def get("http://framatube.org/.well-known/host-meta", _, _, _) do
276 {:ok,
277 %Tesla.Env{
278 status: 200,
279 body: File.read!("test/fixtures/httpoison_mock/framatube.org_host_meta")
280 }}
281 end
282
283 def get("http://framatube.org/main/xrd?uri=framasoft@framatube.org", _, _,
284 Accept: "application/xrd+xml,application/jrd+json"
285 ) do
286 {:ok,
287 %Tesla.Env{
288 status: 200,
289 headers: [{"content-type", "application/json"}],
290 body: File.read!("test/fixtures/httpoison_mock/framasoft@framatube.org.json")
291 }}
292 end
293
294 def get("http://gnusocial.de/.well-known/host-meta", _, _, _) do
295 {:ok,
296 %Tesla.Env{
297 status: 200,
298 body: File.read!("test/fixtures/httpoison_mock/gnusocial.de_host_meta")
299 }}
300 end
301
302 def get("http://gnusocial.de/main/xrd?uri=winterdienst@gnusocial.de", _, _,
303 Accept: "application/xrd+xml,application/jrd+json"
304 ) do
305 {:ok,
306 %Tesla.Env{
307 status: 200,
308 body: File.read!("test/fixtures/httpoison_mock/winterdienst_webfinger.json")
309 }}
310 end
311
312 def get("http://status.alpicola.com/.well-known/host-meta", _, _, _) do
313 {:ok,
314 %Tesla.Env{
315 status: 200,
316 body: File.read!("test/fixtures/httpoison_mock/status.alpicola.com_host_meta")
317 }}
318 end
319
320 def get("http://macgirvin.com/.well-known/host-meta", _, _, _) do
321 {:ok,
322 %Tesla.Env{
323 status: 200,
324 body: File.read!("test/fixtures/httpoison_mock/macgirvin.com_host_meta")
325 }}
326 end
327
328 def get("http://gerzilla.de/.well-known/host-meta", _, _, _) do
329 {:ok,
330 %Tesla.Env{
331 status: 200,
332 body: File.read!("test/fixtures/httpoison_mock/gerzilla.de_host_meta")
333 }}
334 end
335
336 def get("https://gerzilla.de/xrd/?uri=kaniini@gerzilla.de", _, _,
337 Accept: "application/xrd+xml,application/jrd+json"
338 ) do
339 {:ok,
340 %Tesla.Env{
341 status: 200,
342 headers: [{"content-type", "application/json"}],
343 body: File.read!("test/fixtures/httpoison_mock/kaniini@gerzilla.de.json")
344 }}
345 end
346
347 def get("https://social.heldscal.la/api/statuses/user_timeline/23211.atom", _, _, _) do
348 {:ok,
349 %Tesla.Env{
350 status: 200,
351 body:
352 File.read!(
353 "test/fixtures/httpoison_mock/https___social.heldscal.la_api_statuses_user_timeline_23211.atom.xml"
354 )
355 }}
356 end
357
358 def get(
359 "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/23211",
360 _,
361 _,
362 _
363 ) do
364 {:ok,
365 %Tesla.Env{
366 status: 200,
367 body: File.read!("test/fixtures/httpoison_mock/https___social.heldscal.la_user_23211.xml")
368 }}
369 end
370
371 def get("http://social.heldscal.la/.well-known/host-meta", _, _, _) do
372 {:ok,
373 %Tesla.Env{
374 status: 200,
375 body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")
376 }}
377 end
378
379 def get("https://social.heldscal.la/.well-known/host-meta", _, _, _) do
380 {:ok,
381 %Tesla.Env{
382 status: 200,
383 body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")
384 }}
385 end
386
387 def get("https://mastodon.social/users/lambadalambda.atom", _, _, _) do
388 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/lambadalambda.atom")}}
389 end
390
391 def get("https://social.heldscal.la/user/23211", _, _, Accept: "application/activity+json") do
392 {:ok, Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)}
393 end
394
395 def get(url, query, body, headers) do
396 {:error,
397 "Not implemented the mock response for get #{inspect(url)}, #{query}, #{inspect(body)}, #{
398 inspect(headers)
399 }"}
400 end
401
402 # POST Requests
403 #
404
405 def post(url, query \\ [], body \\ [], headers \\ [])
406
407 def post("http://example.org/needs_refresh", _, _, _) do
408 {:ok,
409 %Tesla.Env{
410 status: 200,
411 body: ""
412 }}
413 end
414
415 def post(url, _query, _body, _headers) do
416 {:error, "Not implemented the mock response for post #{inspect(url)}"}
417 end
418 end