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