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