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