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