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