Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / support / http_request_mock.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule HttpRequestMock do
6 require Logger
7
8 def activitypub_object_headers, do: [{"content-type", "application/activity+json"}]
9
10 def request(
11 %Tesla.Env{
12 url: url,
13 method: method,
14 headers: headers,
15 query: query,
16 body: body
17 } = _env
18 ) do
19 with {:ok, res} <- apply(__MODULE__, method, [url, query, body, headers]) do
20 res
21 else
22 error ->
23 with {:error, message} <- error do
24 Logger.warn(to_string(message))
25 end
26
27 {_, _r} = error
28 end
29 end
30
31 # GET Requests
32 #
33 def get(url, query \\ [], body \\ [], headers \\ [])
34
35 def get("https://osada.macgirvin.com/channel/mike", _, _, _) do
36 {:ok,
37 %Tesla.Env{
38 status: 200,
39 body: File.read!("test/fixtures/tesla_mock/https___osada.macgirvin.com_channel_mike.json"),
40 headers: activitypub_object_headers()
41 }}
42 end
43
44 def get("https://shitposter.club/users/moonman", _, _, _) do
45 {:ok,
46 %Tesla.Env{
47 status: 200,
48 body: File.read!("test/fixtures/tesla_mock/moonman@shitposter.club.json"),
49 headers: activitypub_object_headers()
50 }}
51 end
52
53 def get("https://mastodon.social/users/emelie/statuses/101849165031453009", _, _, _) do
54 {:ok,
55 %Tesla.Env{
56 status: 200,
57 body: File.read!("test/fixtures/tesla_mock/status.emelie.json"),
58 headers: activitypub_object_headers()
59 }}
60 end
61
62 def get("https://mastodon.social/users/emelie/statuses/101849165031453404", _, _, _) do
63 {:ok,
64 %Tesla.Env{
65 status: 404,
66 body: ""
67 }}
68 end
69
70 def get("https://mastodon.social/users/emelie", _, _, _) do
71 {:ok,
72 %Tesla.Env{
73 status: 200,
74 body: File.read!("test/fixtures/tesla_mock/emelie.json"),
75 headers: activitypub_object_headers()
76 }}
77 end
78
79 def get("https://mastodon.social/users/not_found", _, _, _) do
80 {:ok, %Tesla.Env{status: 404}}
81 end
82
83 def get("https://mastodon.sdf.org/users/rinpatch", _, _, _) do
84 {:ok,
85 %Tesla.Env{
86 status: 200,
87 body: File.read!("test/fixtures/tesla_mock/rinpatch.json"),
88 headers: activitypub_object_headers()
89 }}
90 end
91
92 def get("https://patch.cx/objects/tesla_mock/poll_attachment", _, _, _) do
93 {:ok,
94 %Tesla.Env{
95 status: 200,
96 body: File.read!("test/fixtures/tesla_mock/poll_attachment.json"),
97 headers: activitypub_object_headers()
98 }}
99 end
100
101 def get(
102 "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/emelie",
103 _,
104 _,
105 _
106 ) do
107 {:ok,
108 %Tesla.Env{
109 status: 200,
110 body: File.read!("test/fixtures/tesla_mock/webfinger_emelie.json"),
111 headers: activitypub_object_headers()
112 }}
113 end
114
115 def get(
116 "https://osada.macgirvin.com/.well-known/webfinger?resource=acct:mike@osada.macgirvin.com",
117 _,
118 _,
119 [{"accept", "application/xrd+xml,application/jrd+json"}]
120 ) do
121 {:ok,
122 %Tesla.Env{
123 status: 200,
124 body: File.read!("test/fixtures/tesla_mock/mike@osada.macgirvin.com.json"),
125 headers: [{"content-type", "application/jrd+json"}]
126 }}
127 end
128
129 def get(
130 "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/29191",
131 _,
132 _,
133 [{"accept", "application/xrd+xml,application/jrd+json"}]
134 ) do
135 {:ok,
136 %Tesla.Env{
137 status: 200,
138 body: File.read!("test/fixtures/tesla_mock/https___social.heldscal.la_user_29191.xml")
139 }}
140 end
141
142 def get(
143 "https://pawoo.net/.well-known/webfinger?resource=acct:https://pawoo.net/users/pekorino",
144 _,
145 _,
146 [{"accept", "application/xrd+xml,application/jrd+json"}]
147 ) do
148 {:ok,
149 %Tesla.Env{
150 status: 200,
151 body: File.read!("test/fixtures/tesla_mock/https___pawoo.net_users_pekorino.xml")
152 }}
153 end
154
155 def get(
156 "https://social.stopwatchingus-heidelberg.de/.well-known/webfinger?resource=acct:https://social.stopwatchingus-heidelberg.de/user/18330",
157 _,
158 _,
159 [{"accept", "application/xrd+xml,application/jrd+json"}]
160 ) do
161 {:ok,
162 %Tesla.Env{
163 status: 200,
164 body: File.read!("test/fixtures/tesla_mock/atarifrosch_webfinger.xml")
165 }}
166 end
167
168 def get(
169 "https://social.heldscal.la/.well-known/webfinger?resource=nonexistant@social.heldscal.la",
170 _,
171 _,
172 [{"accept", "application/xrd+xml,application/jrd+json"}]
173 ) do
174 {:ok,
175 %Tesla.Env{
176 status: 200,
177 body: File.read!("test/fixtures/tesla_mock/nonexistant@social.heldscal.la.xml")
178 }}
179 end
180
181 def get(
182 "https://squeet.me/xrd/?uri=acct:lain@squeet.me",
183 _,
184 _,
185 [{"accept", "application/xrd+xml,application/jrd+json"}]
186 ) do
187 {:ok,
188 %Tesla.Env{
189 status: 200,
190 body: File.read!("test/fixtures/tesla_mock/lain_squeet.me_webfinger.xml"),
191 headers: [{"content-type", "application/xrd+xml"}]
192 }}
193 end
194
195 def get(
196 "https://mst3k.interlinked.me/users/luciferMysticus",
197 _,
198 _,
199 [{"accept", "application/activity+json"}]
200 ) do
201 {:ok,
202 %Tesla.Env{
203 status: 200,
204 body: File.read!("test/fixtures/tesla_mock/lucifermysticus.json"),
205 headers: activitypub_object_headers()
206 }}
207 end
208
209 def get("https://prismo.news/@mxb", _, _, _) do
210 {:ok,
211 %Tesla.Env{
212 status: 200,
213 body: File.read!("test/fixtures/tesla_mock/https___prismo.news__mxb.json"),
214 headers: activitypub_object_headers()
215 }}
216 end
217
218 def get(
219 "https://hubzilla.example.org/channel/kaniini",
220 _,
221 _,
222 [{"accept", "application/activity+json"}]
223 ) do
224 {:ok,
225 %Tesla.Env{
226 status: 200,
227 body: File.read!("test/fixtures/tesla_mock/kaniini@hubzilla.example.org.json"),
228 headers: activitypub_object_headers()
229 }}
230 end
231
232 def get("https://niu.moe/users/rye", _, _, [{"accept", "application/activity+json"}]) do
233 {:ok,
234 %Tesla.Env{
235 status: 200,
236 body: File.read!("test/fixtures/tesla_mock/rye.json"),
237 headers: activitypub_object_headers()
238 }}
239 end
240
241 def get("https://n1u.moe/users/rye", _, _, [{"accept", "application/activity+json"}]) do
242 {:ok,
243 %Tesla.Env{
244 status: 200,
245 body: File.read!("test/fixtures/tesla_mock/rye.json"),
246 headers: activitypub_object_headers()
247 }}
248 end
249
250 def get("http://mastodon.example.org/users/admin/statuses/100787282858396771", _, _, _) do
251 {:ok,
252 %Tesla.Env{
253 status: 200,
254 body:
255 File.read!(
256 "test/fixtures/tesla_mock/http___mastodon.example.org_users_admin_status_1234.json"
257 )
258 }}
259 end
260
261 def get("https://puckipedia.com/", _, _, [{"accept", "application/activity+json"}]) do
262 {:ok,
263 %Tesla.Env{
264 status: 200,
265 body: File.read!("test/fixtures/tesla_mock/puckipedia.com.json"),
266 headers: activitypub_object_headers()
267 }}
268 end
269
270 def get("https://peertube.moe/accounts/7even", _, _, _) do
271 {:ok,
272 %Tesla.Env{
273 status: 200,
274 body: File.read!("test/fixtures/tesla_mock/7even.json"),
275 headers: activitypub_object_headers()
276 }}
277 end
278
279 def get("https://peertube.stream/accounts/createurs", _, _, _) do
280 {:ok,
281 %Tesla.Env{
282 status: 200,
283 body: File.read!("test/fixtures/peertube/actor-person.json"),
284 headers: activitypub_object_headers()
285 }}
286 end
287
288 def get("https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3", _, _, _) do
289 {:ok,
290 %Tesla.Env{
291 status: 200,
292 body: File.read!("test/fixtures/tesla_mock/peertube.moe-vid.json"),
293 headers: activitypub_object_headers()
294 }}
295 end
296
297 def get("https://framatube.org/accounts/framasoft", _, _, _) do
298 {:ok,
299 %Tesla.Env{
300 status: 200,
301 body: File.read!("test/fixtures/tesla_mock/https___framatube.org_accounts_framasoft.json"),
302 headers: activitypub_object_headers()
303 }}
304 end
305
306 def get("https://framatube.org/videos/watch/6050732a-8a7a-43d4-a6cd-809525a1d206", _, _, _) do
307 {:ok,
308 %Tesla.Env{
309 status: 200,
310 body: File.read!("test/fixtures/tesla_mock/framatube.org-video.json"),
311 headers: activitypub_object_headers()
312 }}
313 end
314
315 def get("https://peertube.social/accounts/craigmaloney", _, _, _) do
316 {:ok,
317 %Tesla.Env{
318 status: 200,
319 body: File.read!("test/fixtures/tesla_mock/craigmaloney.json"),
320 headers: activitypub_object_headers()
321 }}
322 end
323
324 def get("https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe", _, _, _) do
325 {:ok,
326 %Tesla.Env{
327 status: 200,
328 body: File.read!("test/fixtures/tesla_mock/peertube-social.json"),
329 headers: activitypub_object_headers()
330 }}
331 end
332
333 def get("https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39", _, _, [
334 {"accept", "application/activity+json"}
335 ]) do
336 {:ok,
337 %Tesla.Env{
338 status: 200,
339 body: File.read!("test/fixtures/tesla_mock/mobilizon.org-event.json"),
340 headers: activitypub_object_headers()
341 }}
342 end
343
344 def get("https://mobilizon.org/@tcit", _, _, [{"accept", "application/activity+json"}]) do
345 {:ok,
346 %Tesla.Env{
347 status: 200,
348 body: File.read!("test/fixtures/tesla_mock/mobilizon.org-user.json"),
349 headers: activitypub_object_headers()
350 }}
351 end
352
353 def get("https://baptiste.gelez.xyz/@/BaptisteGelez", _, _, _) do
354 {:ok,
355 %Tesla.Env{
356 status: 200,
357 body: File.read!("test/fixtures/tesla_mock/baptiste.gelex.xyz-user.json"),
358 headers: activitypub_object_headers()
359 }}
360 end
361
362 def get("https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/", _, _, _) do
363 {:ok,
364 %Tesla.Env{
365 status: 200,
366 body: File.read!("test/fixtures/tesla_mock/baptiste.gelex.xyz-article.json"),
367 headers: activitypub_object_headers()
368 }}
369 end
370
371 def get("https://wedistribute.org/wp-json/pterotype/v1/object/85810", _, _, _) do
372 {:ok,
373 %Tesla.Env{
374 status: 200,
375 body: File.read!("test/fixtures/tesla_mock/wedistribute-article.json"),
376 headers: activitypub_object_headers()
377 }}
378 end
379
380 def get("https://wedistribute.org/wp-json/pterotype/v1/actor/-blog", _, _, _) do
381 {:ok,
382 %Tesla.Env{
383 status: 200,
384 body: File.read!("test/fixtures/tesla_mock/wedistribute-user.json"),
385 headers: activitypub_object_headers()
386 }}
387 end
388
389 def get("http://mastodon.example.org/users/admin", _, _, _) do
390 {:ok,
391 %Tesla.Env{
392 status: 200,
393 body: File.read!("test/fixtures/tesla_mock/admin@mastdon.example.org.json"),
394 headers: activitypub_object_headers()
395 }}
396 end
397
398 def get("http://mastodon.example.org/users/relay", _, _, [
399 {"accept", "application/activity+json"}
400 ]) do
401 {:ok,
402 %Tesla.Env{
403 status: 200,
404 body: File.read!("test/fixtures/tesla_mock/relay@mastdon.example.org.json"),
405 headers: activitypub_object_headers()
406 }}
407 end
408
409 def get("http://mastodon.example.org/users/gargron", _, _, [
410 {"accept", "application/activity+json"}
411 ]) do
412 {:error, :nxdomain}
413 end
414
415 def get("http://osada.macgirvin.com/.well-known/host-meta", _, _, _) do
416 {:ok,
417 %Tesla.Env{
418 status: 404,
419 body: ""
420 }}
421 end
422
423 def get("https://osada.macgirvin.com/.well-known/host-meta", _, _, _) do
424 {:ok,
425 %Tesla.Env{
426 status: 404,
427 body: ""
428 }}
429 end
430
431 def get("http://mastodon.sdf.org/.well-known/host-meta", _, _, _) do
432 {:ok,
433 %Tesla.Env{
434 status: 200,
435 body: File.read!("test/fixtures/tesla_mock/sdf.org_host_meta")
436 }}
437 end
438
439 def get("https://mastodon.sdf.org/.well-known/host-meta", _, _, _) do
440 {:ok,
441 %Tesla.Env{
442 status: 200,
443 body: File.read!("test/fixtures/tesla_mock/sdf.org_host_meta")
444 }}
445 end
446
447 def get(
448 "https://mastodon.sdf.org/.well-known/webfinger?resource=https://mastodon.sdf.org/users/snowdusk",
449 _,
450 _,
451 _
452 ) do
453 {:ok,
454 %Tesla.Env{
455 status: 200,
456 body: File.read!("test/fixtures/tesla_mock/snowdusk@sdf.org_host_meta.json")
457 }}
458 end
459
460 def get("http://mstdn.jp/.well-known/host-meta", _, _, _) do
461 {:ok,
462 %Tesla.Env{
463 status: 200,
464 body: File.read!("test/fixtures/tesla_mock/mstdn.jp_host_meta")
465 }}
466 end
467
468 def get("https://mstdn.jp/.well-known/host-meta", _, _, _) do
469 {:ok,
470 %Tesla.Env{
471 status: 200,
472 body: File.read!("test/fixtures/tesla_mock/mstdn.jp_host_meta")
473 }}
474 end
475
476 def get("https://mstdn.jp/.well-known/webfinger?resource=kpherox@mstdn.jp", _, _, _) do
477 {:ok,
478 %Tesla.Env{
479 status: 200,
480 body: File.read!("test/fixtures/tesla_mock/kpherox@mstdn.jp.xml")
481 }}
482 end
483
484 def get("http://mamot.fr/.well-known/host-meta", _, _, _) do
485 {:ok,
486 %Tesla.Env{
487 status: 200,
488 body: File.read!("test/fixtures/tesla_mock/mamot.fr_host_meta")
489 }}
490 end
491
492 def get("https://mamot.fr/.well-known/host-meta", _, _, _) do
493 {:ok,
494 %Tesla.Env{
495 status: 200,
496 body: File.read!("test/fixtures/tesla_mock/mamot.fr_host_meta")
497 }}
498 end
499
500 def get("http://pawoo.net/.well-known/host-meta", _, _, _) do
501 {:ok,
502 %Tesla.Env{
503 status: 200,
504 body: File.read!("test/fixtures/tesla_mock/pawoo.net_host_meta")
505 }}
506 end
507
508 def get("https://pawoo.net/.well-known/host-meta", _, _, _) do
509 {:ok,
510 %Tesla.Env{
511 status: 200,
512 body: File.read!("test/fixtures/tesla_mock/pawoo.net_host_meta")
513 }}
514 end
515
516 def get(
517 "https://pawoo.net/.well-known/webfinger?resource=https://pawoo.net/users/pekorino",
518 _,
519 _,
520 _
521 ) do
522 {:ok,
523 %Tesla.Env{
524 status: 200,
525 body: File.read!("test/fixtures/tesla_mock/pekorino@pawoo.net_host_meta.json"),
526 headers: activitypub_object_headers()
527 }}
528 end
529
530 def get("http://pleroma.soykaf.com/.well-known/host-meta", _, _, _) do
531 {:ok,
532 %Tesla.Env{
533 status: 200,
534 body: File.read!("test/fixtures/tesla_mock/soykaf.com_host_meta")
535 }}
536 end
537
538 def get("https://pleroma.soykaf.com/.well-known/host-meta", _, _, _) do
539 {:ok,
540 %Tesla.Env{
541 status: 200,
542 body: File.read!("test/fixtures/tesla_mock/soykaf.com_host_meta")
543 }}
544 end
545
546 def get("http://social.stopwatchingus-heidelberg.de/.well-known/host-meta", _, _, _) do
547 {:ok,
548 %Tesla.Env{
549 status: 200,
550 body: File.read!("test/fixtures/tesla_mock/stopwatchingus-heidelberg.de_host_meta")
551 }}
552 end
553
554 def get("https://social.stopwatchingus-heidelberg.de/.well-known/host-meta", _, _, _) do
555 {:ok,
556 %Tesla.Env{
557 status: 200,
558 body: File.read!("test/fixtures/tesla_mock/stopwatchingus-heidelberg.de_host_meta")
559 }}
560 end
561
562 def get(
563 "http://mastodon.example.org/@admin/99541947525187367",
564 _,
565 _,
566 _
567 ) do
568 {:ok,
569 %Tesla.Env{
570 status: 200,
571 body: File.read!("test/fixtures/mastodon-note-object.json"),
572 headers: activitypub_object_headers()
573 }}
574 end
575
576 def get("http://mastodon.example.org/@admin/99541947525187368", _, _, _) do
577 {:ok,
578 %Tesla.Env{
579 status: 404,
580 body: ""
581 }}
582 end
583
584 def get("https://shitposter.club/notice/7369654", _, _, _) do
585 {:ok,
586 %Tesla.Env{
587 status: 200,
588 body: File.read!("test/fixtures/tesla_mock/7369654.html")
589 }}
590 end
591
592 def get("https://mstdn.io/users/mayuutann", _, _, [{"accept", "application/activity+json"}]) do
593 {:ok,
594 %Tesla.Env{
595 status: 200,
596 body: File.read!("test/fixtures/tesla_mock/mayumayu.json"),
597 headers: activitypub_object_headers()
598 }}
599 end
600
601 def get(
602 "https://mstdn.io/users/mayuutann/statuses/99568293732299394",
603 _,
604 _,
605 [{"accept", "application/activity+json"}]
606 ) do
607 {:ok,
608 %Tesla.Env{
609 status: 200,
610 body: File.read!("test/fixtures/tesla_mock/mayumayupost.json"),
611 headers: activitypub_object_headers()
612 }}
613 end
614
615 def get(url, _, _, [{"accept", "application/xrd+xml,application/jrd+json"}])
616 when url in [
617 "https://pleroma.soykaf.com/.well-known/webfinger?resource=acct:https://pleroma.soykaf.com/users/lain",
618 "https://pleroma.soykaf.com/.well-known/webfinger?resource=https://pleroma.soykaf.com/users/lain"
619 ] do
620 {:ok,
621 %Tesla.Env{
622 status: 200,
623 body: File.read!("test/fixtures/tesla_mock/https___pleroma.soykaf.com_users_lain.xml")
624 }}
625 end
626
627 def get(
628 "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/1",
629 _,
630 _,
631 [{"accept", "application/xrd+xml,application/jrd+json"}]
632 ) do
633 {:ok,
634 %Tesla.Env{
635 status: 200,
636 body: File.read!("test/fixtures/tesla_mock/https___shitposter.club_user_1.xml")
637 }}
638 end
639
640 def get("https://testing.pleroma.lol/objects/b319022a-4946-44c5-9de9-34801f95507b", _, _, _) do
641 {:ok, %Tesla.Env{status: 200}}
642 end
643
644 def get(
645 "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/5381",
646 _,
647 _,
648 [{"accept", "application/xrd+xml,application/jrd+json"}]
649 ) do
650 {:ok,
651 %Tesla.Env{
652 status: 200,
653 body: File.read!("test/fixtures/tesla_mock/spc_5381_xrd.xml")
654 }}
655 end
656
657 def get("http://shitposter.club/.well-known/host-meta", _, _, _) do
658 {:ok,
659 %Tesla.Env{
660 status: 200,
661 body: File.read!("test/fixtures/tesla_mock/shitposter.club_host_meta")
662 }}
663 end
664
665 def get("https://shitposter.club/notice/4027863", _, _, _) do
666 {:ok,
667 %Tesla.Env{
668 status: 200,
669 body: File.read!("test/fixtures/tesla_mock/7369654.html")
670 }}
671 end
672
673 def get("http://social.sakamoto.gq/.well-known/host-meta", _, _, _) do
674 {:ok,
675 %Tesla.Env{
676 status: 200,
677 body: File.read!("test/fixtures/tesla_mock/social.sakamoto.gq_host_meta")
678 }}
679 end
680
681 def get(
682 "https://social.sakamoto.gq/.well-known/webfinger?resource=https://social.sakamoto.gq/users/eal",
683 _,
684 _,
685 [{"accept", "application/xrd+xml,application/jrd+json"}]
686 ) do
687 {:ok,
688 %Tesla.Env{
689 status: 200,
690 body: File.read!("test/fixtures/tesla_mock/eal_sakamoto.xml")
691 }}
692 end
693
694 def get("http://mastodon.social/.well-known/host-meta", _, _, _) do
695 {:ok,
696 %Tesla.Env{
697 status: 200,
698 body: File.read!("test/fixtures/tesla_mock/mastodon.social_host_meta")
699 }}
700 end
701
702 def get(
703 "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/lambadalambda",
704 _,
705 _,
706 [{"accept", "application/xrd+xml,application/jrd+json"}]
707 ) do
708 {:ok,
709 %Tesla.Env{
710 status: 200,
711 body:
712 File.read!("test/fixtures/tesla_mock/https___mastodon.social_users_lambadalambda.xml")
713 }}
714 end
715
716 def get("http://gs.example.org/.well-known/host-meta", _, _, _) do
717 {:ok,
718 %Tesla.Env{
719 status: 200,
720 body: File.read!("test/fixtures/tesla_mock/gs.example.org_host_meta")
721 }}
722 end
723
724 def get(
725 "http://gs.example.org/.well-known/webfinger?resource=http://gs.example.org:4040/index.php/user/1",
726 _,
727 _,
728 [{"accept", "application/xrd+xml,application/jrd+json"}]
729 ) do
730 {:ok,
731 %Tesla.Env{
732 status: 200,
733 body:
734 File.read!("test/fixtures/tesla_mock/http___gs.example.org_4040_index.php_user_1.xml")
735 }}
736 end
737
738 def get(
739 "http://gs.example.org:4040/index.php/user/1",
740 _,
741 _,
742 [{"accept", "application/activity+json"}]
743 ) do
744 {:ok, %Tesla.Env{status: 406, body: ""}}
745 end
746
747 def get("http://squeet.me/.well-known/host-meta", _, _, _) do
748 {:ok,
749 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/squeet.me_host_meta")}}
750 end
751
752 def get(
753 "https://squeet.me/xrd?uri=lain@squeet.me",
754 _,
755 _,
756 [{"accept", "application/xrd+xml,application/jrd+json"}]
757 ) do
758 {:ok,
759 %Tesla.Env{
760 status: 200,
761 body: File.read!("test/fixtures/tesla_mock/lain_squeet.me_webfinger.xml")
762 }}
763 end
764
765 def get(
766 "https://social.heldscal.la/.well-known/webfinger?resource=acct:shp@social.heldscal.la",
767 _,
768 _,
769 [{"accept", "application/xrd+xml,application/jrd+json"}]
770 ) do
771 {:ok,
772 %Tesla.Env{
773 status: 200,
774 body: File.read!("test/fixtures/tesla_mock/shp@social.heldscal.la.xml"),
775 headers: [{"content-type", "application/xrd+xml"}]
776 }}
777 end
778
779 def get(
780 "https://social.heldscal.la/.well-known/webfinger?resource=acct:invalid_content@social.heldscal.la",
781 _,
782 _,
783 [{"accept", "application/xrd+xml,application/jrd+json"}]
784 ) do
785 {:ok, %Tesla.Env{status: 200, body: "", headers: [{"content-type", "application/jrd+json"}]}}
786 end
787
788 def get("http://framatube.org/.well-known/host-meta", _, _, _) do
789 {:ok,
790 %Tesla.Env{
791 status: 200,
792 body: File.read!("test/fixtures/tesla_mock/framatube.org_host_meta")
793 }}
794 end
795
796 def get(
797 "http://framatube.org/main/xrd?uri=acct:framasoft@framatube.org",
798 _,
799 _,
800 [{"accept", "application/xrd+xml,application/jrd+json"}]
801 ) do
802 {:ok,
803 %Tesla.Env{
804 status: 200,
805 headers: [{"content-type", "application/jrd+json"}],
806 body: File.read!("test/fixtures/tesla_mock/framasoft@framatube.org.json")
807 }}
808 end
809
810 def get("http://gnusocial.de/.well-known/host-meta", _, _, _) do
811 {:ok,
812 %Tesla.Env{
813 status: 200,
814 body: File.read!("test/fixtures/tesla_mock/gnusocial.de_host_meta")
815 }}
816 end
817
818 def get(
819 "http://gnusocial.de/main/xrd?uri=winterdienst@gnusocial.de",
820 _,
821 _,
822 [{"accept", "application/xrd+xml,application/jrd+json"}]
823 ) do
824 {:ok,
825 %Tesla.Env{
826 status: 200,
827 body: File.read!("test/fixtures/tesla_mock/winterdienst_webfinger.json"),
828 headers: activitypub_object_headers()
829 }}
830 end
831
832 def get("http://status.alpicola.com/.well-known/host-meta", _, _, _) do
833 {:ok,
834 %Tesla.Env{
835 status: 200,
836 body: File.read!("test/fixtures/tesla_mock/status.alpicola.com_host_meta")
837 }}
838 end
839
840 def get("http://macgirvin.com/.well-known/host-meta", _, _, _) do
841 {:ok,
842 %Tesla.Env{
843 status: 200,
844 body: File.read!("test/fixtures/tesla_mock/macgirvin.com_host_meta")
845 }}
846 end
847
848 def get("http://gerzilla.de/.well-known/host-meta", _, _, _) do
849 {:ok,
850 %Tesla.Env{
851 status: 200,
852 body: File.read!("test/fixtures/tesla_mock/gerzilla.de_host_meta")
853 }}
854 end
855
856 def get(
857 "https://gerzilla.de/xrd/?uri=acct:kaniini@gerzilla.de",
858 _,
859 _,
860 [{"accept", "application/xrd+xml,application/jrd+json"}]
861 ) do
862 {:ok,
863 %Tesla.Env{
864 status: 200,
865 headers: [{"content-type", "application/jrd+json"}],
866 body: File.read!("test/fixtures/tesla_mock/kaniini@gerzilla.de.json")
867 }}
868 end
869
870 def get(
871 "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/23211",
872 _,
873 _,
874 _
875 ) do
876 {:ok,
877 %Tesla.Env{
878 status: 200,
879 body: File.read!("test/fixtures/tesla_mock/https___social.heldscal.la_user_23211.xml")
880 }}
881 end
882
883 def get("http://social.heldscal.la/.well-known/host-meta", _, _, _) do
884 {:ok,
885 %Tesla.Env{
886 status: 200,
887 body: File.read!("test/fixtures/tesla_mock/social.heldscal.la_host_meta")
888 }}
889 end
890
891 def get("https://social.heldscal.la/.well-known/host-meta", _, _, _) do
892 {:ok,
893 %Tesla.Env{
894 status: 200,
895 body: File.read!("test/fixtures/tesla_mock/social.heldscal.la_host_meta")
896 }}
897 end
898
899 def get("https://mastodon.social/users/lambadalambda", _, _, _) do
900 {:ok,
901 %Tesla.Env{
902 status: 200,
903 body: File.read!("test/fixtures/lambadalambda.json"),
904 headers: activitypub_object_headers()
905 }}
906 end
907
908 def get("https://apfed.club/channel/indio", _, _, _) do
909 {:ok,
910 %Tesla.Env{
911 status: 200,
912 body: File.read!("test/fixtures/tesla_mock/osada-user-indio.json"),
913 headers: activitypub_object_headers()
914 }}
915 end
916
917 def get("https://social.heldscal.la/user/23211", _, _, [{"accept", "application/activity+json"}]) do
918 {:ok, Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)}
919 end
920
921 def get("http://example.com/ogp", _, _, _) do
922 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
923 end
924
925 def get("https://example.com/ogp", _, _, _) do
926 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
927 end
928
929 def get("https://pleroma.local/notice/9kCP7V", _, _, _) do
930 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
931 end
932
933 def get("http://localhost:4001/users/masto_closed/followers", _, _, _) do
934 {:ok,
935 %Tesla.Env{
936 status: 200,
937 body: File.read!("test/fixtures/users_mock/masto_closed_followers.json"),
938 headers: activitypub_object_headers()
939 }}
940 end
941
942 def get("http://localhost:4001/users/masto_closed/followers?page=1", _, _, _) do
943 {:ok,
944 %Tesla.Env{
945 status: 200,
946 body: File.read!("test/fixtures/users_mock/masto_closed_followers_page.json"),
947 headers: activitypub_object_headers()
948 }}
949 end
950
951 def get("http://localhost:4001/users/masto_closed/following", _, _, _) do
952 {:ok,
953 %Tesla.Env{
954 status: 200,
955 body: File.read!("test/fixtures/users_mock/masto_closed_following.json"),
956 headers: activitypub_object_headers()
957 }}
958 end
959
960 def get("http://localhost:4001/users/masto_closed/following?page=1", _, _, _) do
961 {:ok,
962 %Tesla.Env{
963 status: 200,
964 body: File.read!("test/fixtures/users_mock/masto_closed_following_page.json"),
965 headers: activitypub_object_headers()
966 }}
967 end
968
969 def get("http://localhost:8080/followers/fuser3", _, _, _) do
970 {:ok,
971 %Tesla.Env{
972 status: 200,
973 body: File.read!("test/fixtures/users_mock/friendica_followers.json"),
974 headers: activitypub_object_headers()
975 }}
976 end
977
978 def get("http://localhost:8080/following/fuser3", _, _, _) do
979 {:ok,
980 %Tesla.Env{
981 status: 200,
982 body: File.read!("test/fixtures/users_mock/friendica_following.json"),
983 headers: activitypub_object_headers()
984 }}
985 end
986
987 def get("http://localhost:4001/users/fuser2/followers", _, _, _) do
988 {:ok,
989 %Tesla.Env{
990 status: 200,
991 body: File.read!("test/fixtures/users_mock/pleroma_followers.json"),
992 headers: activitypub_object_headers()
993 }}
994 end
995
996 def get("http://localhost:4001/users/fuser2/following", _, _, _) do
997 {:ok,
998 %Tesla.Env{
999 status: 200,
1000 body: File.read!("test/fixtures/users_mock/pleroma_following.json"),
1001 headers: activitypub_object_headers()
1002 }}
1003 end
1004
1005 def get("http://domain-with-errors:4001/users/fuser1/followers", _, _, _) do
1006 {:ok,
1007 %Tesla.Env{
1008 status: 504,
1009 body: ""
1010 }}
1011 end
1012
1013 def get("http://domain-with-errors:4001/users/fuser1/following", _, _, _) do
1014 {:ok,
1015 %Tesla.Env{
1016 status: 504,
1017 body: ""
1018 }}
1019 end
1020
1021 def get("http://example.com/ogp-missing-data", _, _, _) do
1022 {:ok,
1023 %Tesla.Env{
1024 status: 200,
1025 body: File.read!("test/fixtures/rich_media/ogp-missing-data.html")
1026 }}
1027 end
1028
1029 def get("https://example.com/ogp-missing-data", _, _, _) do
1030 {:ok,
1031 %Tesla.Env{
1032 status: 200,
1033 body: File.read!("test/fixtures/rich_media/ogp-missing-data.html")
1034 }}
1035 end
1036
1037 def get("http://example.com/malformed", _, _, _) do
1038 {:ok,
1039 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/malformed-data.html")}}
1040 end
1041
1042 def get("http://example.com/empty", _, _, _) do
1043 {:ok, %Tesla.Env{status: 200, body: "hello"}}
1044 end
1045
1046 def get("http://404.site" <> _, _, _, _) do
1047 {:ok,
1048 %Tesla.Env{
1049 status: 404,
1050 body: ""
1051 }}
1052 end
1053
1054 def get(
1055 "https://zetsubou.xn--q9jyb4c/.well-known/webfinger?resource=acct:lain@zetsubou.xn--q9jyb4c",
1056 _,
1057 _,
1058 [{"accept", "application/xrd+xml,application/jrd+json"}]
1059 ) do
1060 {:ok,
1061 %Tesla.Env{
1062 status: 200,
1063 body: File.read!("test/fixtures/lain.xml"),
1064 headers: [{"content-type", "application/xrd+xml"}]
1065 }}
1066 end
1067
1068 def get(
1069 "https://zetsubou.xn--q9jyb4c/.well-known/webfinger?resource=acct:https://zetsubou.xn--q9jyb4c/users/lain",
1070 _,
1071 _,
1072 [{"accept", "application/xrd+xml,application/jrd+json"}]
1073 ) do
1074 {:ok,
1075 %Tesla.Env{
1076 status: 200,
1077 body: File.read!("test/fixtures/lain.xml"),
1078 headers: [{"content-type", "application/xrd+xml"}]
1079 }}
1080 end
1081
1082 def get("http://zetsubou.xn--q9jyb4c/.well-known/host-meta", _, _, _) do
1083 {:ok,
1084 %Tesla.Env{
1085 status: 200,
1086 body: File.read!("test/fixtures/host-meta-zetsubou.xn--q9jyb4c.xml")
1087 }}
1088 end
1089
1090 def get(
1091 "https://zetsubou.xn--q9jyb4c/.well-known/host-meta",
1092 _,
1093 _,
1094 _
1095 ) do
1096 {:ok,
1097 %Tesla.Env{
1098 status: 200,
1099 body: File.read!("test/fixtures/host-meta-zetsubou.xn--q9jyb4c.xml")
1100 }}
1101 end
1102
1103 def get("https://info.pleroma.site/activity.json", _, _, [
1104 {"accept", "application/activity+json"}
1105 ]) do
1106 {:ok,
1107 %Tesla.Env{
1108 status: 200,
1109 body: File.read!("test/fixtures/tesla_mock/https__info.pleroma.site_activity.json"),
1110 headers: activitypub_object_headers()
1111 }}
1112 end
1113
1114 def get("https://info.pleroma.site/activity.json", _, _, _) do
1115 {:ok, %Tesla.Env{status: 404, body: ""}}
1116 end
1117
1118 def get("https://info.pleroma.site/activity2.json", _, _, [
1119 {"accept", "application/activity+json"}
1120 ]) do
1121 {:ok,
1122 %Tesla.Env{
1123 status: 200,
1124 body: File.read!("test/fixtures/tesla_mock/https__info.pleroma.site_activity2.json"),
1125 headers: activitypub_object_headers()
1126 }}
1127 end
1128
1129 def get("https://info.pleroma.site/activity2.json", _, _, _) do
1130 {:ok, %Tesla.Env{status: 404, body: ""}}
1131 end
1132
1133 def get("https://info.pleroma.site/activity3.json", _, _, [
1134 {"accept", "application/activity+json"}
1135 ]) do
1136 {:ok,
1137 %Tesla.Env{
1138 status: 200,
1139 body: File.read!("test/fixtures/tesla_mock/https__info.pleroma.site_activity3.json"),
1140 headers: activitypub_object_headers()
1141 }}
1142 end
1143
1144 def get("https://info.pleroma.site/activity3.json", _, _, _) do
1145 {:ok, %Tesla.Env{status: 404, body: ""}}
1146 end
1147
1148 def get("https://mstdn.jp/.well-known/webfinger?resource=acct:kpherox@mstdn.jp", _, _, _) do
1149 {:ok,
1150 %Tesla.Env{
1151 status: 200,
1152 body: File.read!("test/fixtures/tesla_mock/kpherox@mstdn.jp.xml"),
1153 headers: [{"content-type", "application/xrd+xml"}]
1154 }}
1155 end
1156
1157 def get("https://10.111.10.1/notice/9kCP7V", _, _, _) do
1158 {:ok, %Tesla.Env{status: 200, body: ""}}
1159 end
1160
1161 def get("https://172.16.32.40/notice/9kCP7V", _, _, _) do
1162 {:ok, %Tesla.Env{status: 200, body: ""}}
1163 end
1164
1165 def get("https://192.168.10.40/notice/9kCP7V", _, _, _) do
1166 {:ok, %Tesla.Env{status: 200, body: ""}}
1167 end
1168
1169 def get("https://www.patreon.com/posts/mastodon-2-9-and-28121681", _, _, _) do
1170 {:ok, %Tesla.Env{status: 200, body: ""}}
1171 end
1172
1173 def get("http://mastodon.example.org/@admin/99541947525187367", _, _, _) do
1174 {:ok,
1175 %Tesla.Env{
1176 status: 200,
1177 body: File.read!("test/fixtures/mastodon-post-activity.json"),
1178 headers: activitypub_object_headers()
1179 }}
1180 end
1181
1182 def get("https://info.pleroma.site/activity4.json", _, _, _) do
1183 {:ok, %Tesla.Env{status: 500, body: "Error occurred"}}
1184 end
1185
1186 def get("http://example.com/rel_me/anchor", _, _, _) do
1187 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_anchor.html")}}
1188 end
1189
1190 def get("http://example.com/rel_me/anchor_nofollow", _, _, _) do
1191 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_anchor_nofollow.html")}}
1192 end
1193
1194 def get("http://example.com/rel_me/link", _, _, _) do
1195 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_link.html")}}
1196 end
1197
1198 def get("http://example.com/rel_me/null", _, _, _) do
1199 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_null.html")}}
1200 end
1201
1202 def get("https://skippers-bin.com/notes/7x9tmrp97i", _, _, _) do
1203 {:ok,
1204 %Tesla.Env{
1205 status: 200,
1206 body: File.read!("test/fixtures/tesla_mock/misskey_poll_no_end_date.json"),
1207 headers: activitypub_object_headers()
1208 }}
1209 end
1210
1211 def get("https://example.org/emoji/firedfox.png", _, _, _) do
1212 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/image.jpg")}}
1213 end
1214
1215 def get("https://skippers-bin.com/users/7v1w1r8ce6", _, _, _) do
1216 {:ok,
1217 %Tesla.Env{
1218 status: 200,
1219 body: File.read!("test/fixtures/tesla_mock/sjw.json"),
1220 headers: activitypub_object_headers()
1221 }}
1222 end
1223
1224 def get("https://patch.cx/users/rin", _, _, _) do
1225 {:ok,
1226 %Tesla.Env{
1227 status: 200,
1228 body: File.read!("test/fixtures/tesla_mock/rin.json"),
1229 headers: activitypub_object_headers()
1230 }}
1231 end
1232
1233 def get(
1234 "https://channels.tests.funkwhale.audio/federation/music/uploads/42342395-0208-4fee-a38d-259a6dae0871",
1235 _,
1236 _,
1237 _
1238 ) do
1239 {:ok,
1240 %Tesla.Env{
1241 status: 200,
1242 body: File.read!("test/fixtures/tesla_mock/funkwhale_audio.json"),
1243 headers: activitypub_object_headers()
1244 }}
1245 end
1246
1247 def get("https://channels.tests.funkwhale.audio/federation/actors/compositions", _, _, _) do
1248 {:ok,
1249 %Tesla.Env{
1250 status: 200,
1251 body: File.read!("test/fixtures/tesla_mock/funkwhale_channel.json"),
1252 headers: activitypub_object_headers()
1253 }}
1254 end
1255
1256 def get("http://example.com/rel_me/error", _, _, _) do
1257 {:ok, %Tesla.Env{status: 404, body: ""}}
1258 end
1259
1260 def get("https://relay.mastodon.host/actor", _, _, _) do
1261 {:ok,
1262 %Tesla.Env{
1263 status: 200,
1264 body: File.read!("test/fixtures/relay/relay.json"),
1265 headers: activitypub_object_headers()
1266 }}
1267 end
1268
1269 def get("http://localhost:4001/", _, "", [{"accept", "text/html"}]) do
1270 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/7369654.html")}}
1271 end
1272
1273 def get("https://osada.macgirvin.com/", _, "", [{"accept", "text/html"}]) do
1274 {:ok,
1275 %Tesla.Env{
1276 status: 200,
1277 body: File.read!("test/fixtures/tesla_mock/https___osada.macgirvin.com.html")
1278 }}
1279 end
1280
1281 def get("https://patch.cx/objects/a399c28e-c821-4820-bc3e-4afeb044c16f", _, _, _) do
1282 {:ok,
1283 %Tesla.Env{
1284 status: 200,
1285 body: File.read!("test/fixtures/tesla_mock/emoji-in-summary.json"),
1286 headers: activitypub_object_headers()
1287 }}
1288 end
1289
1290 def get(url, query, body, headers) do
1291 {:error,
1292 "Mock response not implemented for GET #{inspect(url)}, #{query}, #{inspect(body)}, #{
1293 inspect(headers)
1294 }"}
1295 end
1296
1297 # POST Requests
1298 #
1299
1300 def post(url, query \\ [], body \\ [], headers \\ [])
1301
1302 def post("https://relay.mastodon.host/inbox", _, _, _) do
1303 {:ok, %Tesla.Env{status: 200, body: ""}}
1304 end
1305
1306 def post("http://example.org/needs_refresh", _, _, _) do
1307 {:ok,
1308 %Tesla.Env{
1309 status: 200,
1310 body: ""
1311 }}
1312 end
1313
1314 def post("http://mastodon.example.org/inbox", _, _, _) do
1315 {:ok,
1316 %Tesla.Env{
1317 status: 200,
1318 body: ""
1319 }}
1320 end
1321
1322 def post("https://hubzilla.example.org/inbox", _, _, _) do
1323 {:ok,
1324 %Tesla.Env{
1325 status: 200,
1326 body: ""
1327 }}
1328 end
1329
1330 def post("http://gs.example.org/index.php/main/salmon/user/1", _, _, _) do
1331 {:ok,
1332 %Tesla.Env{
1333 status: 200,
1334 body: ""
1335 }}
1336 end
1337
1338 def post("http://200.site" <> _, _, _, _) do
1339 {:ok,
1340 %Tesla.Env{
1341 status: 200,
1342 body: ""
1343 }}
1344 end
1345
1346 def post("http://connrefused.site" <> _, _, _, _) do
1347 {:error, :connrefused}
1348 end
1349
1350 def post("http://404.site" <> _, _, _, _) do
1351 {:ok,
1352 %Tesla.Env{
1353 status: 404,
1354 body: ""
1355 }}
1356 end
1357
1358 def post(url, query, body, headers) do
1359 {:error,
1360 "Mock response not implemented for POST #{inspect(url)}, #{query}, #{inspect(body)}, #{
1361 inspect(headers)
1362 }"}
1363 end
1364
1365 # Most of the rich media mocks are missing HEAD requests, so we just return 404.
1366 @rich_media_mocks [
1367 "https://example.com/ogp",
1368 "https://example.com/ogp-missing-data",
1369 "https://example.com/twitter-card"
1370 ]
1371 def head(url, _query, _body, _headers) when url in @rich_media_mocks do
1372 {:ok, %Tesla.Env{status: 404, body: ""}}
1373 end
1374
1375 def head(url, query, body, headers) do
1376 {:error,
1377 "Mock response not implemented for HEAD #{inspect(url)}, #{query}, #{inspect(body)}, #{
1378 inspect(headers)
1379 }"}
1380 end
1381 end