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