2148bd4e7c9cfb9cb66ee50bdf2ed54a8ef667d8
[akkoma] / test / support / http_request_mock.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule HttpRequestMock do
6 require Logger
7
8 def request(
9 %Tesla.Env{
10 url: url,
11 method: method,
12 headers: headers,
13 query: query,
14 body: body
15 } = _env
16 ) do
17 with {:ok, res} <- apply(__MODULE__, method, [url, query, body, headers]) do
18 res
19 else
20 {_, _r} = error ->
21 # Logger.warn(r)
22 error
23 end
24 end
25
26 # GET Requests
27 #
28 def get(url, query \\ [], body \\ [], headers \\ [])
29
30 def get("https://osada.macgirvin.com/channel/mike", _, _, _) do
31 {:ok,
32 %Tesla.Env{
33 status: 200,
34 body:
35 File.read!("test/fixtures/httpoison_mock/https___osada.macgirvin.com_channel_mike.json")
36 }}
37 end
38
39 def get("https://mastodon.social/users/emelie/statuses/101849165031453009", _, _, _) do
40 {:ok,
41 %Tesla.Env{
42 status: 200,
43 body: File.read!("test/fixtures/httpoison_mock/status.emelie.json")
44 }}
45 end
46
47 def get("https://mastodon.social/users/emelie", _, _, _) do
48 {:ok,
49 %Tesla.Env{
50 status: 200,
51 body: File.read!("test/fixtures/httpoison_mock/emelie.json")
52 }}
53 end
54
55 def get(
56 "https://osada.macgirvin.com/.well-known/webfinger?resource=acct:mike@osada.macgirvin.com",
57 _,
58 _,
59 Accept: "application/xrd+xml,application/jrd+json"
60 ) do
61 {:ok,
62 %Tesla.Env{
63 status: 200,
64 body: File.read!("test/fixtures/httpoison_mock/mike@osada.macgirvin.com.json")
65 }}
66 end
67
68 def get(
69 "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/29191",
70 _,
71 _,
72 Accept: "application/xrd+xml,application/jrd+json"
73 ) do
74 {:ok,
75 %Tesla.Env{
76 status: 200,
77 body: File.read!("test/fixtures/httpoison_mock/https___social.heldscal.la_user_29191.xml")
78 }}
79 end
80
81 def get("https://pawoo.net/users/pekorino.atom", _, _, _) do
82 {:ok,
83 %Tesla.Env{
84 status: 200,
85 body: File.read!("test/fixtures/httpoison_mock/https___pawoo.net_users_pekorino.atom")
86 }}
87 end
88
89 def get(
90 "https://pawoo.net/.well-known/webfinger?resource=acct:https://pawoo.net/users/pekorino",
91 _,
92 _,
93 Accept: "application/xrd+xml,application/jrd+json"
94 ) do
95 {:ok,
96 %Tesla.Env{
97 status: 200,
98 body: File.read!("test/fixtures/httpoison_mock/https___pawoo.net_users_pekorino.xml")
99 }}
100 end
101
102 def get(
103 "https://social.stopwatchingus-heidelberg.de/api/statuses/user_timeline/18330.atom",
104 _,
105 _,
106 _
107 ) do
108 {:ok,
109 %Tesla.Env{
110 status: 200,
111 body: File.read!("test/fixtures/httpoison_mock/atarifrosch_feed.xml")
112 }}
113 end
114
115 def get(
116 "https://social.stopwatchingus-heidelberg.de/.well-known/webfinger?resource=acct:https://social.stopwatchingus-heidelberg.de/user/18330",
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/httpoison_mock/atarifrosch_webfinger.xml")
125 }}
126 end
127
128 def get("https://mamot.fr/users/Skruyb.atom", _, _, _) do
129 {:ok,
130 %Tesla.Env{
131 status: 200,
132 body: File.read!("test/fixtures/httpoison_mock/https___mamot.fr_users_Skruyb.atom")
133 }}
134 end
135
136 def get(
137 "https://mamot.fr/.well-known/webfinger?resource=acct:https://mamot.fr/users/Skruyb",
138 _,
139 _,
140 Accept: "application/xrd+xml,application/jrd+json"
141 ) do
142 {:ok,
143 %Tesla.Env{
144 status: 200,
145 body: File.read!("test/fixtures/httpoison_mock/skruyb@mamot.fr.atom")
146 }}
147 end
148
149 def get(
150 "https://social.heldscal.la/.well-known/webfinger?resource=nonexistant@social.heldscal.la",
151 _,
152 _,
153 Accept: "application/xrd+xml,application/jrd+json"
154 ) do
155 {:ok,
156 %Tesla.Env{
157 status: 200,
158 body: File.read!("test/fixtures/httpoison_mock/nonexistant@social.heldscal.la.xml")
159 }}
160 end
161
162 def get(
163 "https://squeet.me/xrd/?uri=lain@squeet.me",
164 _,
165 _,
166 Accept: "application/xrd+xml,application/jrd+json"
167 ) do
168 {:ok,
169 %Tesla.Env{
170 status: 200,
171 body: File.read!("test/fixtures/httpoison_mock/lain_squeet.me_webfinger.xml")
172 }}
173 end
174
175 def get(
176 "https://mst3k.interlinked.me/users/luciferMysticus",
177 _,
178 _,
179 Accept: "application/activity+json"
180 ) do
181 {:ok,
182 %Tesla.Env{
183 status: 200,
184 body: File.read!("test/fixtures/httpoison_mock/lucifermysticus.json")
185 }}
186 end
187
188 def get("https://prismo.news/@mxb", _, _, _) do
189 {:ok,
190 %Tesla.Env{
191 status: 200,
192 body: File.read!("test/fixtures/httpoison_mock/https___prismo.news__mxb.json")
193 }}
194 end
195
196 def get(
197 "https://hubzilla.example.org/channel/kaniini",
198 _,
199 _,
200 Accept: "application/activity+json"
201 ) do
202 {:ok,
203 %Tesla.Env{
204 status: 200,
205 body: File.read!("test/fixtures/httpoison_mock/kaniini@hubzilla.example.org.json")
206 }}
207 end
208
209 def get("https://niu.moe/users/rye", _, _, Accept: "application/activity+json") do
210 {:ok,
211 %Tesla.Env{
212 status: 200,
213 body: File.read!("test/fixtures/httpoison_mock/rye.json")
214 }}
215 end
216
217 def get("http://mastodon.example.org/users/admin/statuses/100787282858396771", _, _, _) do
218 {:ok,
219 %Tesla.Env{
220 status: 200,
221 body:
222 File.read!(
223 "test/fixtures/httpoison_mock/http___mastodon.example.org_users_admin_status_1234.json"
224 )
225 }}
226 end
227
228 def get("https://puckipedia.com/", _, _, Accept: "application/activity+json") do
229 {:ok,
230 %Tesla.Env{
231 status: 200,
232 body: File.read!("test/fixtures/httpoison_mock/puckipedia.com.json")
233 }}
234 end
235
236 def get("https://peertube.moe/accounts/7even", _, _, _) do
237 {:ok,
238 %Tesla.Env{
239 status: 200,
240 body: File.read!("test/fixtures/httpoison_mock/7even.json")
241 }}
242 end
243
244 def get("https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3", _, _, _) do
245 {:ok,
246 %Tesla.Env{
247 status: 200,
248 body: File.read!("test/fixtures/httpoison_mock/peertube.moe-vid.json")
249 }}
250 end
251
252 def get("https://baptiste.gelez.xyz/@/BaptisteGelez", _, _, _) do
253 {:ok,
254 %Tesla.Env{
255 status: 200,
256 body: File.read!("test/fixtures/httpoison_mock/baptiste.gelex.xyz-user.json")
257 }}
258 end
259
260 def get("https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/", _, _, _) do
261 {:ok,
262 %Tesla.Env{
263 status: 200,
264 body: File.read!("test/fixtures/httpoison_mock/baptiste.gelex.xyz-article.json")
265 }}
266 end
267
268 def get("http://mastodon.example.org/users/admin", _, _, Accept: "application/activity+json") do
269 {:ok,
270 %Tesla.Env{
271 status: 200,
272 body: File.read!("test/fixtures/httpoison_mock/admin@mastdon.example.org.json")
273 }}
274 end
275
276 def get(
277 "http://mastodon.example.org/@admin/99541947525187367",
278 _,
279 _,
280 Accept: "application/activity+json"
281 ) do
282 {:ok,
283 %Tesla.Env{
284 status: 200,
285 body: File.read!("test/fixtures/mastodon-note-object.json")
286 }}
287 end
288
289 def get("https://shitposter.club/notice/7369654", _, _, _) do
290 {:ok,
291 %Tesla.Env{
292 status: 200,
293 body: File.read!("test/fixtures/httpoison_mock/7369654.html")
294 }}
295 end
296
297 def get("https://mstdn.io/users/mayuutann", _, _, Accept: "application/activity+json") do
298 {:ok,
299 %Tesla.Env{
300 status: 200,
301 body: File.read!("test/fixtures/httpoison_mock/mayumayu.json")
302 }}
303 end
304
305 def get(
306 "https://mstdn.io/users/mayuutann/statuses/99568293732299394",
307 _,
308 _,
309 Accept: "application/activity+json"
310 ) do
311 {:ok,
312 %Tesla.Env{
313 status: 200,
314 body: File.read!("test/fixtures/httpoison_mock/mayumayupost.json")
315 }}
316 end
317
318 def get("https://pleroma.soykaf.com/users/lain/feed.atom", _, _, _) do
319 {:ok,
320 %Tesla.Env{
321 status: 200,
322 body:
323 File.read!(
324 "test/fixtures/httpoison_mock/https___pleroma.soykaf.com_users_lain_feed.atom.xml"
325 )
326 }}
327 end
328
329 def get(url, _, _, Accept: "application/xrd+xml,application/jrd+json")
330 when url in [
331 "https://pleroma.soykaf.com/.well-known/webfinger?resource=acct:https://pleroma.soykaf.com/users/lain",
332 "https://pleroma.soykaf.com/.well-known/webfinger?resource=https://pleroma.soykaf.com/users/lain"
333 ] do
334 {:ok,
335 %Tesla.Env{
336 status: 200,
337 body: File.read!("test/fixtures/httpoison_mock/https___pleroma.soykaf.com_users_lain.xml")
338 }}
339 end
340
341 def get("https://shitposter.club/api/statuses/user_timeline/1.atom", _, _, _) do
342 {:ok,
343 %Tesla.Env{
344 status: 200,
345 body:
346 File.read!(
347 "test/fixtures/httpoison_mock/https___shitposter.club_api_statuses_user_timeline_1.atom.xml"
348 )
349 }}
350 end
351
352 def get(
353 "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/1",
354 _,
355 _,
356 Accept: "application/xrd+xml,application/jrd+json"
357 ) do
358 {:ok,
359 %Tesla.Env{
360 status: 200,
361 body: File.read!("test/fixtures/httpoison_mock/https___shitposter.club_user_1.xml")
362 }}
363 end
364
365 def get("https://shitposter.club/notice/2827873", _, _, _) do
366 {:ok,
367 %Tesla.Env{
368 status: 200,
369 body:
370 File.read!("test/fixtures/httpoison_mock/https___shitposter.club_notice_2827873.html")
371 }}
372 end
373
374 def get("https://shitposter.club/api/statuses/show/2827873.atom", _, _, _) do
375 {:ok,
376 %Tesla.Env{
377 status: 200,
378 body:
379 File.read!(
380 "test/fixtures/httpoison_mock/https___shitposter.club_api_statuses_show_2827873.atom.xml"
381 )
382 }}
383 end
384
385 def get("https://testing.pleroma.lol/objects/b319022a-4946-44c5-9de9-34801f95507b", _, _, _) do
386 {:ok, %Tesla.Env{status: 200}}
387 end
388
389 def get("https://shitposter.club/api/statuses/user_timeline/5381.atom", _, _, _) do
390 {:ok,
391 %Tesla.Env{
392 status: 200,
393 body: File.read!("test/fixtures/httpoison_mock/spc_5381.atom")
394 }}
395 end
396
397 def get(
398 "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/5381",
399 _,
400 _,
401 Accept: "application/xrd+xml,application/jrd+json"
402 ) do
403 {:ok,
404 %Tesla.Env{
405 status: 200,
406 body: File.read!("test/fixtures/httpoison_mock/spc_5381_xrd.xml")
407 }}
408 end
409
410 def get("http://shitposter.club/.well-known/host-meta", _, _, _) do
411 {:ok,
412 %Tesla.Env{
413 status: 200,
414 body: File.read!("test/fixtures/httpoison_mock/shitposter.club_host_meta")
415 }}
416 end
417
418 def get("https://shitposter.club/api/statuses/show/7369654.atom", _, _, _) do
419 {:ok,
420 %Tesla.Env{
421 status: 200,
422 body: File.read!("test/fixtures/httpoison_mock/7369654.atom")
423 }}
424 end
425
426 def get("https://shitposter.club/notice/4027863", _, _, _) do
427 {:ok,
428 %Tesla.Env{
429 status: 200,
430 body: File.read!("test/fixtures/httpoison_mock/7369654.html")
431 }}
432 end
433
434 def get("https://social.sakamoto.gq/users/eal/feed.atom", _, _, _) do
435 {:ok,
436 %Tesla.Env{
437 status: 200,
438 body: File.read!("test/fixtures/httpoison_mock/sakamoto_eal_feed.atom")
439 }}
440 end
441
442 def get("http://social.sakamoto.gq/.well-known/host-meta", _, _, _) do
443 {:ok,
444 %Tesla.Env{
445 status: 200,
446 body: File.read!("test/fixtures/httpoison_mock/social.sakamoto.gq_host_meta")
447 }}
448 end
449
450 def get(
451 "https://social.sakamoto.gq/.well-known/webfinger?resource=https://social.sakamoto.gq/users/eal",
452 _,
453 _,
454 Accept: "application/xrd+xml,application/jrd+json"
455 ) do
456 {:ok,
457 %Tesla.Env{
458 status: 200,
459 body: File.read!("test/fixtures/httpoison_mock/eal_sakamoto.xml")
460 }}
461 end
462
463 def get(
464 "https://social.sakamoto.gq/objects/0ccc1a2c-66b0-4305-b23a-7f7f2b040056",
465 _,
466 _,
467 Accept: "application/atom+xml"
468 ) do
469 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/sakamoto.atom")}}
470 end
471
472 def get("http://mastodon.social/.well-known/host-meta", _, _, _) do
473 {:ok,
474 %Tesla.Env{
475 status: 200,
476 body: File.read!("test/fixtures/httpoison_mock/mastodon.social_host_meta")
477 }}
478 end
479
480 def get(
481 "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/lambadalambda",
482 _,
483 _,
484 Accept: "application/xrd+xml,application/jrd+json"
485 ) do
486 {:ok,
487 %Tesla.Env{
488 status: 200,
489 body:
490 File.read!(
491 "test/fixtures/httpoison_mock/https___mastodon.social_users_lambadalambda.xml"
492 )
493 }}
494 end
495
496 def get("http://gs.example.org/.well-known/host-meta", _, _, _) do
497 {:ok,
498 %Tesla.Env{
499 status: 200,
500 body: File.read!("test/fixtures/httpoison_mock/gs.example.org_host_meta")
501 }}
502 end
503
504 def get(
505 "http://gs.example.org/.well-known/webfinger?resource=http://gs.example.org:4040/index.php/user/1",
506 _,
507 _,
508 Accept: "application/xrd+xml,application/jrd+json"
509 ) do
510 {:ok,
511 %Tesla.Env{
512 status: 200,
513 body:
514 File.read!(
515 "test/fixtures/httpoison_mock/http___gs.example.org_4040_index.php_user_1.xml"
516 )
517 }}
518 end
519
520 def get("http://gs.example.org/index.php/api/statuses/user_timeline/1.atom", _, _, _) do
521 {:ok,
522 %Tesla.Env{
523 status: 200,
524 body:
525 File.read!(
526 "test/fixtures/httpoison_mock/http__gs.example.org_index.php_api_statuses_user_timeline_1.atom.xml"
527 )
528 }}
529 end
530
531 def get("https://social.heldscal.la/api/statuses/user_timeline/29191.atom", _, _, _) do
532 {:ok,
533 %Tesla.Env{
534 status: 200,
535 body:
536 File.read!(
537 "test/fixtures/httpoison_mock/https___social.heldscal.la_api_statuses_user_timeline_29191.atom.xml"
538 )
539 }}
540 end
541
542 def get("http://squeet.me/.well-known/host-meta", _, _, _) do
543 {:ok,
544 %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/squeet.me_host_meta")}}
545 end
546
547 def get(
548 "https://squeet.me/xrd?uri=lain@squeet.me",
549 _,
550 _,
551 Accept: "application/xrd+xml,application/jrd+json"
552 ) do
553 {:ok,
554 %Tesla.Env{
555 status: 200,
556 body: File.read!("test/fixtures/httpoison_mock/lain_squeet.me_webfinger.xml")
557 }}
558 end
559
560 def get(
561 "https://social.heldscal.la/.well-known/webfinger?resource=shp@social.heldscal.la",
562 _,
563 _,
564 Accept: "application/xrd+xml,application/jrd+json"
565 ) do
566 {:ok,
567 %Tesla.Env{
568 status: 200,
569 body: File.read!("test/fixtures/httpoison_mock/shp@social.heldscal.la.xml")
570 }}
571 end
572
573 def get("http://framatube.org/.well-known/host-meta", _, _, _) do
574 {:ok,
575 %Tesla.Env{
576 status: 200,
577 body: File.read!("test/fixtures/httpoison_mock/framatube.org_host_meta")
578 }}
579 end
580
581 def get(
582 "http://framatube.org/main/xrd?uri=framasoft@framatube.org",
583 _,
584 _,
585 Accept: "application/xrd+xml,application/jrd+json"
586 ) do
587 {:ok,
588 %Tesla.Env{
589 status: 200,
590 headers: [{"content-type", "application/json"}],
591 body: File.read!("test/fixtures/httpoison_mock/framasoft@framatube.org.json")
592 }}
593 end
594
595 def get("http://gnusocial.de/.well-known/host-meta", _, _, _) do
596 {:ok,
597 %Tesla.Env{
598 status: 200,
599 body: File.read!("test/fixtures/httpoison_mock/gnusocial.de_host_meta")
600 }}
601 end
602
603 def get(
604 "http://gnusocial.de/main/xrd?uri=winterdienst@gnusocial.de",
605 _,
606 _,
607 Accept: "application/xrd+xml,application/jrd+json"
608 ) do
609 {:ok,
610 %Tesla.Env{
611 status: 200,
612 body: File.read!("test/fixtures/httpoison_mock/winterdienst_webfinger.json")
613 }}
614 end
615
616 def get("http://status.alpicola.com/.well-known/host-meta", _, _, _) do
617 {:ok,
618 %Tesla.Env{
619 status: 200,
620 body: File.read!("test/fixtures/httpoison_mock/status.alpicola.com_host_meta")
621 }}
622 end
623
624 def get("http://macgirvin.com/.well-known/host-meta", _, _, _) do
625 {:ok,
626 %Tesla.Env{
627 status: 200,
628 body: File.read!("test/fixtures/httpoison_mock/macgirvin.com_host_meta")
629 }}
630 end
631
632 def get("http://gerzilla.de/.well-known/host-meta", _, _, _) do
633 {:ok,
634 %Tesla.Env{
635 status: 200,
636 body: File.read!("test/fixtures/httpoison_mock/gerzilla.de_host_meta")
637 }}
638 end
639
640 def get(
641 "https://gerzilla.de/xrd/?uri=kaniini@gerzilla.de",
642 _,
643 _,
644 Accept: "application/xrd+xml,application/jrd+json"
645 ) do
646 {:ok,
647 %Tesla.Env{
648 status: 200,
649 headers: [{"content-type", "application/json"}],
650 body: File.read!("test/fixtures/httpoison_mock/kaniini@gerzilla.de.json")
651 }}
652 end
653
654 def get("https://social.heldscal.la/api/statuses/user_timeline/23211.atom", _, _, _) do
655 {:ok,
656 %Tesla.Env{
657 status: 200,
658 body:
659 File.read!(
660 "test/fixtures/httpoison_mock/https___social.heldscal.la_api_statuses_user_timeline_23211.atom.xml"
661 )
662 }}
663 end
664
665 def get(
666 "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/23211",
667 _,
668 _,
669 _
670 ) do
671 {:ok,
672 %Tesla.Env{
673 status: 200,
674 body: File.read!("test/fixtures/httpoison_mock/https___social.heldscal.la_user_23211.xml")
675 }}
676 end
677
678 def get("http://social.heldscal.la/.well-known/host-meta", _, _, _) do
679 {:ok,
680 %Tesla.Env{
681 status: 200,
682 body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")
683 }}
684 end
685
686 def get("https://social.heldscal.la/.well-known/host-meta", _, _, _) do
687 {:ok,
688 %Tesla.Env{
689 status: 200,
690 body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")
691 }}
692 end
693
694 def get("https://mastodon.social/users/lambadalambda.atom", _, _, _) do
695 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/lambadalambda.atom")}}
696 end
697
698 def get("https://social.heldscal.la/user/23211", _, _, Accept: "application/activity+json") do
699 {:ok, Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)}
700 end
701
702 def get("http://example.com/ogp", _, _, _) do
703 {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
704 end
705
706 def get("http://example.com/malformed", _, _, _) do
707 {:ok,
708 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/malformed-data.html")}}
709 end
710
711 def get("http://example.com/empty", _, _, _) do
712 {:ok, %Tesla.Env{status: 200, body: "hello"}}
713 end
714
715 def get("http://404.site" <> _, _, _, _) do
716 {:ok,
717 %Tesla.Env{
718 status: 404,
719 body: ""
720 }}
721 end
722
723 def get(url, query, body, headers) do
724 {:error,
725 "Not implemented the mock response for get #{inspect(url)}, #{query}, #{inspect(body)}, #{
726 inspect(headers)
727 }"}
728 end
729
730 # POST Requests
731 #
732
733 def post(url, query \\ [], body \\ [], headers \\ [])
734
735 def post("http://example.org/needs_refresh", _, _, _) do
736 {:ok,
737 %Tesla.Env{
738 status: 200,
739 body: ""
740 }}
741 end
742
743 def post("http://200.site" <> _, _, _, _) do
744 {:ok,
745 %Tesla.Env{
746 status: 200,
747 body: ""
748 }}
749 end
750
751 def post("http://connrefused.site" <> _, _, _, _) do
752 {:error, :connrefused}
753 end
754
755 def post("http://404.site" <> _, _, _, _) do
756 {:ok,
757 %Tesla.Env{
758 status: 404,
759 body: ""
760 }}
761 end
762
763 def post(url, _query, _body, _headers) do
764 {:error, "Not implemented the mock response for post #{inspect(url)}"}
765 end
766 end