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