Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1373 lines
36KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule HttpRequestMock do
  5. require Logger
  6. def activitypub_object_headers, do: [{"content-type", "application/activity+json"}]
  7. def request(
  8. %Tesla.Env{
  9. url: url,
  10. method: method,
  11. headers: headers,
  12. query: query,
  13. body: body
  14. } = _env
  15. ) do
  16. with {:ok, res} <- apply(__MODULE__, method, [url, query, body, headers]) do
  17. res
  18. else
  19. error ->
  20. with {:error, message} <- error do
  21. Logger.warn(to_string(message))
  22. end
  23. {_, _r} = error
  24. end
  25. end
  26. # GET Requests
  27. #
  28. def get(url, query \\ [], body \\ [], headers \\ [])
  29. def get("https://osada.macgirvin.com/channel/mike", _, _, _) do
  30. {:ok,
  31. %Tesla.Env{
  32. status: 200,
  33. body: File.read!("test/fixtures/tesla_mock/https___osada.macgirvin.com_channel_mike.json"),
  34. headers: activitypub_object_headers()
  35. }}
  36. end
  37. def get("https://shitposter.club/users/moonman", _, _, _) do
  38. {:ok,
  39. %Tesla.Env{
  40. status: 200,
  41. body: File.read!("test/fixtures/tesla_mock/moonman@shitposter.club.json"),
  42. headers: activitypub_object_headers()
  43. }}
  44. end
  45. def get("https://mastodon.social/users/emelie/statuses/101849165031453009", _, _, _) do
  46. {:ok,
  47. %Tesla.Env{
  48. status: 200,
  49. body: File.read!("test/fixtures/tesla_mock/status.emelie.json"),
  50. headers: activitypub_object_headers()
  51. }}
  52. end
  53. def get("https://mastodon.social/users/emelie/statuses/101849165031453404", _, _, _) do
  54. {:ok,
  55. %Tesla.Env{
  56. status: 404,
  57. body: ""
  58. }}
  59. end
  60. def get("https://mastodon.social/users/emelie", _, _, _) do
  61. {:ok,
  62. %Tesla.Env{
  63. status: 200,
  64. body: File.read!("test/fixtures/tesla_mock/emelie.json"),
  65. headers: activitypub_object_headers()
  66. }}
  67. end
  68. def get("https://mastodon.social/users/not_found", _, _, _) do
  69. {:ok, %Tesla.Env{status: 404}}
  70. end
  71. def get("https://mastodon.sdf.org/users/rinpatch", _, _, _) do
  72. {:ok,
  73. %Tesla.Env{
  74. status: 200,
  75. body: File.read!("test/fixtures/tesla_mock/rinpatch.json"),
  76. headers: activitypub_object_headers()
  77. }}
  78. end
  79. def get("https://patch.cx/objects/tesla_mock/poll_attachment", _, _, _) do
  80. {:ok,
  81. %Tesla.Env{
  82. status: 200,
  83. body: File.read!("test/fixtures/tesla_mock/poll_attachment.json"),
  84. headers: activitypub_object_headers()
  85. }}
  86. end
  87. def get(
  88. "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/emelie",
  89. _,
  90. _,
  91. _
  92. ) do
  93. {:ok,
  94. %Tesla.Env{
  95. status: 200,
  96. body: File.read!("test/fixtures/tesla_mock/webfinger_emelie.json"),
  97. headers: activitypub_object_headers()
  98. }}
  99. end
  100. def get(
  101. "https://osada.macgirvin.com/.well-known/webfinger?resource=acct:mike@osada.macgirvin.com",
  102. _,
  103. _,
  104. [{"accept", "application/xrd+xml,application/jrd+json"}]
  105. ) do
  106. {:ok,
  107. %Tesla.Env{
  108. status: 200,
  109. body: File.read!("test/fixtures/tesla_mock/mike@osada.macgirvin.com.json"),
  110. headers: [{"content-type", "application/jrd+json"}]
  111. }}
  112. end
  113. def get(
  114. "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/29191",
  115. _,
  116. _,
  117. [{"accept", "application/xrd+xml,application/jrd+json"}]
  118. ) do
  119. {:ok,
  120. %Tesla.Env{
  121. status: 200,
  122. body: File.read!("test/fixtures/tesla_mock/https___social.heldscal.la_user_29191.xml")
  123. }}
  124. end
  125. def get(
  126. "https://pawoo.net/.well-known/webfinger?resource=acct:https://pawoo.net/users/pekorino",
  127. _,
  128. _,
  129. [{"accept", "application/xrd+xml,application/jrd+json"}]
  130. ) do
  131. {:ok,
  132. %Tesla.Env{
  133. status: 200,
  134. body: File.read!("test/fixtures/tesla_mock/https___pawoo.net_users_pekorino.xml")
  135. }}
  136. end
  137. def get(
  138. "https://social.stopwatchingus-heidelberg.de/.well-known/webfinger?resource=acct:https://social.stopwatchingus-heidelberg.de/user/18330",
  139. _,
  140. _,
  141. [{"accept", "application/xrd+xml,application/jrd+json"}]
  142. ) do
  143. {:ok,
  144. %Tesla.Env{
  145. status: 200,
  146. body: File.read!("test/fixtures/tesla_mock/atarifrosch_webfinger.xml")
  147. }}
  148. end
  149. def get(
  150. "https://social.heldscal.la/.well-known/webfinger?resource=nonexistant@social.heldscal.la",
  151. _,
  152. _,
  153. [{"accept", "application/xrd+xml,application/jrd+json"}]
  154. ) do
  155. {:ok,
  156. %Tesla.Env{
  157. status: 200,
  158. body: File.read!("test/fixtures/tesla_mock/nonexistant@social.heldscal.la.xml")
  159. }}
  160. end
  161. def get(
  162. "https://squeet.me/xrd/?uri=acct:lain@squeet.me",
  163. _,
  164. _,
  165. [{"accept", "application/xrd+xml,application/jrd+json"}]
  166. ) do
  167. {:ok,
  168. %Tesla.Env{
  169. status: 200,
  170. body: File.read!("test/fixtures/tesla_mock/lain_squeet.me_webfinger.xml"),
  171. headers: [{"content-type", "application/xrd+xml"}]
  172. }}
  173. end
  174. def get(
  175. "https://mst3k.interlinked.me/users/luciferMysticus",
  176. _,
  177. _,
  178. [{"accept", "application/activity+json"}]
  179. ) do
  180. {:ok,
  181. %Tesla.Env{
  182. status: 200,
  183. body: File.read!("test/fixtures/tesla_mock/lucifermysticus.json"),
  184. headers: activitypub_object_headers()
  185. }}
  186. end
  187. def get("https://prismo.news/@mxb", _, _, _) do
  188. {:ok,
  189. %Tesla.Env{
  190. status: 200,
  191. body: File.read!("test/fixtures/tesla_mock/https___prismo.news__mxb.json"),
  192. headers: activitypub_object_headers()
  193. }}
  194. end
  195. def get(
  196. "https://hubzilla.example.org/channel/kaniini",
  197. _,
  198. _,
  199. [{"accept", "application/activity+json"}]
  200. ) do
  201. {:ok,
  202. %Tesla.Env{
  203. status: 200,
  204. body: File.read!("test/fixtures/tesla_mock/kaniini@hubzilla.example.org.json"),
  205. headers: activitypub_object_headers()
  206. }}
  207. end
  208. def get("https://niu.moe/users/rye", _, _, [{"accept", "application/activity+json"}]) do
  209. {:ok,
  210. %Tesla.Env{
  211. status: 200,
  212. body: File.read!("test/fixtures/tesla_mock/rye.json"),
  213. headers: activitypub_object_headers()
  214. }}
  215. end
  216. def get("https://n1u.moe/users/rye", _, _, [{"accept", "application/activity+json"}]) do
  217. {:ok,
  218. %Tesla.Env{
  219. status: 200,
  220. body: File.read!("test/fixtures/tesla_mock/rye.json"),
  221. headers: activitypub_object_headers()
  222. }}
  223. end
  224. def get("http://mastodon.example.org/users/admin/statuses/100787282858396771", _, _, _) do
  225. {:ok,
  226. %Tesla.Env{
  227. status: 200,
  228. body:
  229. File.read!(
  230. "test/fixtures/tesla_mock/http___mastodon.example.org_users_admin_status_1234.json"
  231. )
  232. }}
  233. end
  234. def get("https://puckipedia.com/", _, _, [{"accept", "application/activity+json"}]) do
  235. {:ok,
  236. %Tesla.Env{
  237. status: 200,
  238. body: File.read!("test/fixtures/tesla_mock/puckipedia.com.json"),
  239. headers: activitypub_object_headers()
  240. }}
  241. end
  242. def get("https://peertube.moe/accounts/7even", _, _, _) do
  243. {:ok,
  244. %Tesla.Env{
  245. status: 200,
  246. body: File.read!("test/fixtures/tesla_mock/7even.json"),
  247. headers: activitypub_object_headers()
  248. }}
  249. end
  250. def get("https://peertube.stream/accounts/createurs", _, _, _) do
  251. {:ok,
  252. %Tesla.Env{
  253. status: 200,
  254. body: File.read!("test/fixtures/peertube/actor-person.json"),
  255. headers: activitypub_object_headers()
  256. }}
  257. end
  258. def get("https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3", _, _, _) do
  259. {:ok,
  260. %Tesla.Env{
  261. status: 200,
  262. body: File.read!("test/fixtures/tesla_mock/peertube.moe-vid.json"),
  263. headers: activitypub_object_headers()
  264. }}
  265. end
  266. def get("https://framatube.org/accounts/framasoft", _, _, _) do
  267. {:ok,
  268. %Tesla.Env{
  269. status: 200,
  270. body: File.read!("test/fixtures/tesla_mock/https___framatube.org_accounts_framasoft.json"),
  271. headers: activitypub_object_headers()
  272. }}
  273. end
  274. def get("https://framatube.org/videos/watch/6050732a-8a7a-43d4-a6cd-809525a1d206", _, _, _) do
  275. {:ok,
  276. %Tesla.Env{
  277. status: 200,
  278. body: File.read!("test/fixtures/tesla_mock/framatube.org-video.json"),
  279. headers: activitypub_object_headers()
  280. }}
  281. end
  282. def get("https://peertube.social/accounts/craigmaloney", _, _, _) do
  283. {:ok,
  284. %Tesla.Env{
  285. status: 200,
  286. body: File.read!("test/fixtures/tesla_mock/craigmaloney.json"),
  287. headers: activitypub_object_headers()
  288. }}
  289. end
  290. def get("https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe", _, _, _) do
  291. {:ok,
  292. %Tesla.Env{
  293. status: 200,
  294. body: File.read!("test/fixtures/tesla_mock/peertube-social.json"),
  295. headers: activitypub_object_headers()
  296. }}
  297. end
  298. def get("https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39", _, _, [
  299. {"accept", "application/activity+json"}
  300. ]) do
  301. {:ok,
  302. %Tesla.Env{
  303. status: 200,
  304. body: File.read!("test/fixtures/tesla_mock/mobilizon.org-event.json"),
  305. headers: activitypub_object_headers()
  306. }}
  307. end
  308. def get("https://mobilizon.org/@tcit", _, _, [{"accept", "application/activity+json"}]) do
  309. {:ok,
  310. %Tesla.Env{
  311. status: 200,
  312. body: File.read!("test/fixtures/tesla_mock/mobilizon.org-user.json"),
  313. headers: activitypub_object_headers()
  314. }}
  315. end
  316. def get("https://baptiste.gelez.xyz/@/BaptisteGelez", _, _, _) do
  317. {:ok,
  318. %Tesla.Env{
  319. status: 200,
  320. body: File.read!("test/fixtures/tesla_mock/baptiste.gelex.xyz-user.json"),
  321. headers: activitypub_object_headers()
  322. }}
  323. end
  324. def get("https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/", _, _, _) do
  325. {:ok,
  326. %Tesla.Env{
  327. status: 200,
  328. body: File.read!("test/fixtures/tesla_mock/baptiste.gelex.xyz-article.json"),
  329. headers: activitypub_object_headers()
  330. }}
  331. end
  332. def get("https://wedistribute.org/wp-json/pterotype/v1/object/85810", _, _, _) do
  333. {:ok,
  334. %Tesla.Env{
  335. status: 200,
  336. body: File.read!("test/fixtures/tesla_mock/wedistribute-article.json"),
  337. headers: activitypub_object_headers()
  338. }}
  339. end
  340. def get("https://wedistribute.org/wp-json/pterotype/v1/actor/-blog", _, _, _) do
  341. {:ok,
  342. %Tesla.Env{
  343. status: 200,
  344. body: File.read!("test/fixtures/tesla_mock/wedistribute-user.json"),
  345. headers: activitypub_object_headers()
  346. }}
  347. end
  348. def get("http://mastodon.example.org/users/admin", _, _, _) do
  349. {:ok,
  350. %Tesla.Env{
  351. status: 200,
  352. body: File.read!("test/fixtures/tesla_mock/admin@mastdon.example.org.json"),
  353. headers: activitypub_object_headers()
  354. }}
  355. end
  356. def get("http://mastodon.example.org/users/relay", _, _, [
  357. {"accept", "application/activity+json"}
  358. ]) do
  359. {:ok,
  360. %Tesla.Env{
  361. status: 200,
  362. body: File.read!("test/fixtures/tesla_mock/relay@mastdon.example.org.json"),
  363. headers: activitypub_object_headers()
  364. }}
  365. end
  366. def get("http://mastodon.example.org/users/gargron", _, _, [
  367. {"accept", "application/activity+json"}
  368. ]) do
  369. {:error, :nxdomain}
  370. end
  371. def get("http://osada.macgirvin.com/.well-known/host-meta", _, _, _) do
  372. {:ok,
  373. %Tesla.Env{
  374. status: 404,
  375. body: ""
  376. }}
  377. end
  378. def get("https://osada.macgirvin.com/.well-known/host-meta", _, _, _) do
  379. {:ok,
  380. %Tesla.Env{
  381. status: 404,
  382. body: ""
  383. }}
  384. end
  385. def get("http://mastodon.sdf.org/.well-known/host-meta", _, _, _) do
  386. {:ok,
  387. %Tesla.Env{
  388. status: 200,
  389. body: File.read!("test/fixtures/tesla_mock/sdf.org_host_meta")
  390. }}
  391. end
  392. def get("https://mastodon.sdf.org/.well-known/host-meta", _, _, _) do
  393. {:ok,
  394. %Tesla.Env{
  395. status: 200,
  396. body: File.read!("test/fixtures/tesla_mock/sdf.org_host_meta")
  397. }}
  398. end
  399. def get(
  400. "https://mastodon.sdf.org/.well-known/webfinger?resource=https://mastodon.sdf.org/users/snowdusk",
  401. _,
  402. _,
  403. _
  404. ) do
  405. {:ok,
  406. %Tesla.Env{
  407. status: 200,
  408. body: File.read!("test/fixtures/tesla_mock/snowdusk@sdf.org_host_meta.json")
  409. }}
  410. end
  411. def get("http://mstdn.jp/.well-known/host-meta", _, _, _) do
  412. {:ok,
  413. %Tesla.Env{
  414. status: 200,
  415. body: File.read!("test/fixtures/tesla_mock/mstdn.jp_host_meta")
  416. }}
  417. end
  418. def get("https://mstdn.jp/.well-known/host-meta", _, _, _) do
  419. {:ok,
  420. %Tesla.Env{
  421. status: 200,
  422. body: File.read!("test/fixtures/tesla_mock/mstdn.jp_host_meta")
  423. }}
  424. end
  425. def get("https://mstdn.jp/.well-known/webfinger?resource=kpherox@mstdn.jp", _, _, _) do
  426. {:ok,
  427. %Tesla.Env{
  428. status: 200,
  429. body: File.read!("test/fixtures/tesla_mock/kpherox@mstdn.jp.xml")
  430. }}
  431. end
  432. def get("http://mamot.fr/.well-known/host-meta", _, _, _) do
  433. {:ok,
  434. %Tesla.Env{
  435. status: 200,
  436. body: File.read!("test/fixtures/tesla_mock/mamot.fr_host_meta")
  437. }}
  438. end
  439. def get("https://mamot.fr/.well-known/host-meta", _, _, _) do
  440. {:ok,
  441. %Tesla.Env{
  442. status: 200,
  443. body: File.read!("test/fixtures/tesla_mock/mamot.fr_host_meta")
  444. }}
  445. end
  446. def get("http://pawoo.net/.well-known/host-meta", _, _, _) do
  447. {:ok,
  448. %Tesla.Env{
  449. status: 200,
  450. body: File.read!("test/fixtures/tesla_mock/pawoo.net_host_meta")
  451. }}
  452. end
  453. def get("https://pawoo.net/.well-known/host-meta", _, _, _) do
  454. {:ok,
  455. %Tesla.Env{
  456. status: 200,
  457. body: File.read!("test/fixtures/tesla_mock/pawoo.net_host_meta")
  458. }}
  459. end
  460. def get(
  461. "https://pawoo.net/.well-known/webfinger?resource=https://pawoo.net/users/pekorino",
  462. _,
  463. _,
  464. _
  465. ) do
  466. {:ok,
  467. %Tesla.Env{
  468. status: 200,
  469. body: File.read!("test/fixtures/tesla_mock/pekorino@pawoo.net_host_meta.json"),
  470. headers: activitypub_object_headers()
  471. }}
  472. end
  473. def get("http://pleroma.soykaf.com/.well-known/host-meta", _, _, _) do
  474. {:ok,
  475. %Tesla.Env{
  476. status: 200,
  477. body: File.read!("test/fixtures/tesla_mock/soykaf.com_host_meta")
  478. }}
  479. end
  480. def get("https://pleroma.soykaf.com/.well-known/host-meta", _, _, _) do
  481. {:ok,
  482. %Tesla.Env{
  483. status: 200,
  484. body: File.read!("test/fixtures/tesla_mock/soykaf.com_host_meta")
  485. }}
  486. end
  487. def get("http://social.stopwatchingus-heidelberg.de/.well-known/host-meta", _, _, _) do
  488. {:ok,
  489. %Tesla.Env{
  490. status: 200,
  491. body: File.read!("test/fixtures/tesla_mock/stopwatchingus-heidelberg.de_host_meta")
  492. }}
  493. end
  494. def get("https://social.stopwatchingus-heidelberg.de/.well-known/host-meta", _, _, _) do
  495. {:ok,
  496. %Tesla.Env{
  497. status: 200,
  498. body: File.read!("test/fixtures/tesla_mock/stopwatchingus-heidelberg.de_host_meta")
  499. }}
  500. end
  501. def get(
  502. "http://mastodon.example.org/@admin/99541947525187367",
  503. _,
  504. _,
  505. _
  506. ) do
  507. {:ok,
  508. %Tesla.Env{
  509. status: 200,
  510. body: File.read!("test/fixtures/mastodon-note-object.json"),
  511. headers: activitypub_object_headers()
  512. }}
  513. end
  514. def get("http://mastodon.example.org/@admin/99541947525187368", _, _, _) do
  515. {:ok,
  516. %Tesla.Env{
  517. status: 404,
  518. body: ""
  519. }}
  520. end
  521. def get("https://shitposter.club/notice/7369654", _, _, _) do
  522. {:ok,
  523. %Tesla.Env{
  524. status: 200,
  525. body: File.read!("test/fixtures/tesla_mock/7369654.html")
  526. }}
  527. end
  528. def get("https://mstdn.io/users/mayuutann", _, _, [{"accept", "application/activity+json"}]) do
  529. {:ok,
  530. %Tesla.Env{
  531. status: 200,
  532. body: File.read!("test/fixtures/tesla_mock/mayumayu.json"),
  533. headers: activitypub_object_headers()
  534. }}
  535. end
  536. def get(
  537. "https://mstdn.io/users/mayuutann/statuses/99568293732299394",
  538. _,
  539. _,
  540. [{"accept", "application/activity+json"}]
  541. ) do
  542. {:ok,
  543. %Tesla.Env{
  544. status: 200,
  545. body: File.read!("test/fixtures/tesla_mock/mayumayupost.json"),
  546. headers: activitypub_object_headers()
  547. }}
  548. end
  549. def get(url, _, _, [{"accept", "application/xrd+xml,application/jrd+json"}])
  550. when url in [
  551. "https://pleroma.soykaf.com/.well-known/webfinger?resource=acct:https://pleroma.soykaf.com/users/lain",
  552. "https://pleroma.soykaf.com/.well-known/webfinger?resource=https://pleroma.soykaf.com/users/lain"
  553. ] do
  554. {:ok,
  555. %Tesla.Env{
  556. status: 200,
  557. body: File.read!("test/fixtures/tesla_mock/https___pleroma.soykaf.com_users_lain.xml")
  558. }}
  559. end
  560. def get(
  561. "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/1",
  562. _,
  563. _,
  564. [{"accept", "application/xrd+xml,application/jrd+json"}]
  565. ) do
  566. {:ok,
  567. %Tesla.Env{
  568. status: 200,
  569. body: File.read!("test/fixtures/tesla_mock/https___shitposter.club_user_1.xml")
  570. }}
  571. end
  572. def get("https://testing.pleroma.lol/objects/b319022a-4946-44c5-9de9-34801f95507b", _, _, _) do
  573. {:ok, %Tesla.Env{status: 200}}
  574. end
  575. def get(
  576. "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/5381",
  577. _,
  578. _,
  579. [{"accept", "application/xrd+xml,application/jrd+json"}]
  580. ) do
  581. {:ok,
  582. %Tesla.Env{
  583. status: 200,
  584. body: File.read!("test/fixtures/tesla_mock/spc_5381_xrd.xml")
  585. }}
  586. end
  587. def get("http://shitposter.club/.well-known/host-meta", _, _, _) do
  588. {:ok,
  589. %Tesla.Env{
  590. status: 200,
  591. body: File.read!("test/fixtures/tesla_mock/shitposter.club_host_meta")
  592. }}
  593. end
  594. def get("https://shitposter.club/notice/4027863", _, _, _) do
  595. {:ok,
  596. %Tesla.Env{
  597. status: 200,
  598. body: File.read!("test/fixtures/tesla_mock/7369654.html")
  599. }}
  600. end
  601. def get("http://social.sakamoto.gq/.well-known/host-meta", _, _, _) do
  602. {:ok,
  603. %Tesla.Env{
  604. status: 200,
  605. body: File.read!("test/fixtures/tesla_mock/social.sakamoto.gq_host_meta")
  606. }}
  607. end
  608. def get(
  609. "https://social.sakamoto.gq/.well-known/webfinger?resource=https://social.sakamoto.gq/users/eal",
  610. _,
  611. _,
  612. [{"accept", "application/xrd+xml,application/jrd+json"}]
  613. ) do
  614. {:ok,
  615. %Tesla.Env{
  616. status: 200,
  617. body: File.read!("test/fixtures/tesla_mock/eal_sakamoto.xml")
  618. }}
  619. end
  620. def get("http://mastodon.social/.well-known/host-meta", _, _, _) do
  621. {:ok,
  622. %Tesla.Env{
  623. status: 200,
  624. body: File.read!("test/fixtures/tesla_mock/mastodon.social_host_meta")
  625. }}
  626. end
  627. def get(
  628. "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/lambadalambda",
  629. _,
  630. _,
  631. [{"accept", "application/xrd+xml,application/jrd+json"}]
  632. ) do
  633. {:ok,
  634. %Tesla.Env{
  635. status: 200,
  636. body:
  637. File.read!("test/fixtures/tesla_mock/https___mastodon.social_users_lambadalambda.xml")
  638. }}
  639. end
  640. def get("http://gs.example.org/.well-known/host-meta", _, _, _) do
  641. {:ok,
  642. %Tesla.Env{
  643. status: 200,
  644. body: File.read!("test/fixtures/tesla_mock/gs.example.org_host_meta")
  645. }}
  646. end
  647. def get(
  648. "http://gs.example.org/.well-known/webfinger?resource=http://gs.example.org:4040/index.php/user/1",
  649. _,
  650. _,
  651. [{"accept", "application/xrd+xml,application/jrd+json"}]
  652. ) do
  653. {:ok,
  654. %Tesla.Env{
  655. status: 200,
  656. body:
  657. File.read!("test/fixtures/tesla_mock/http___gs.example.org_4040_index.php_user_1.xml")
  658. }}
  659. end
  660. def get(
  661. "http://gs.example.org:4040/index.php/user/1",
  662. _,
  663. _,
  664. [{"accept", "application/activity+json"}]
  665. ) do
  666. {:ok, %Tesla.Env{status: 406, body: ""}}
  667. end
  668. def get("http://squeet.me/.well-known/host-meta", _, _, _) do
  669. {:ok,
  670. %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/squeet.me_host_meta")}}
  671. end
  672. def get(
  673. "https://squeet.me/xrd?uri=lain@squeet.me",
  674. _,
  675. _,
  676. [{"accept", "application/xrd+xml,application/jrd+json"}]
  677. ) do
  678. {:ok,
  679. %Tesla.Env{
  680. status: 200,
  681. body: File.read!("test/fixtures/tesla_mock/lain_squeet.me_webfinger.xml")
  682. }}
  683. end
  684. def get(
  685. "https://social.heldscal.la/.well-known/webfinger?resource=acct:shp@social.heldscal.la",
  686. _,
  687. _,
  688. [{"accept", "application/xrd+xml,application/jrd+json"}]
  689. ) do
  690. {:ok,
  691. %Tesla.Env{
  692. status: 200,
  693. body: File.read!("test/fixtures/tesla_mock/shp@social.heldscal.la.xml"),
  694. headers: [{"content-type", "application/xrd+xml"}]
  695. }}
  696. end
  697. def get(
  698. "https://social.heldscal.la/.well-known/webfinger?resource=acct:invalid_content@social.heldscal.la",
  699. _,
  700. _,
  701. [{"accept", "application/xrd+xml,application/jrd+json"}]
  702. ) do
  703. {:ok, %Tesla.Env{status: 200, body: "", headers: [{"content-type", "application/jrd+json"}]}}
  704. end
  705. def get("http://framatube.org/.well-known/host-meta", _, _, _) do
  706. {:ok,
  707. %Tesla.Env{
  708. status: 200,
  709. body: File.read!("test/fixtures/tesla_mock/framatube.org_host_meta")
  710. }}
  711. end
  712. def get(
  713. "http://framatube.org/main/xrd?uri=acct:framasoft@framatube.org",
  714. _,
  715. _,
  716. [{"accept", "application/xrd+xml,application/jrd+json"}]
  717. ) do
  718. {:ok,
  719. %Tesla.Env{
  720. status: 200,
  721. headers: [{"content-type", "application/jrd+json"}],
  722. body: File.read!("test/fixtures/tesla_mock/framasoft@framatube.org.json")
  723. }}
  724. end
  725. def get("http://gnusocial.de/.well-known/host-meta", _, _, _) do
  726. {:ok,
  727. %Tesla.Env{
  728. status: 200,
  729. body: File.read!("test/fixtures/tesla_mock/gnusocial.de_host_meta")
  730. }}
  731. end
  732. def get(
  733. "http://gnusocial.de/main/xrd?uri=winterdienst@gnusocial.de",
  734. _,
  735. _,
  736. [{"accept", "application/xrd+xml,application/jrd+json"}]
  737. ) do
  738. {:ok,
  739. %Tesla.Env{
  740. status: 200,
  741. body: File.read!("test/fixtures/tesla_mock/winterdienst_webfinger.json"),
  742. headers: activitypub_object_headers()
  743. }}
  744. end
  745. def get("http://status.alpicola.com/.well-known/host-meta", _, _, _) do
  746. {:ok,
  747. %Tesla.Env{
  748. status: 200,
  749. body: File.read!("test/fixtures/tesla_mock/status.alpicola.com_host_meta")
  750. }}
  751. end
  752. def get("http://macgirvin.com/.well-known/host-meta", _, _, _) do
  753. {:ok,
  754. %Tesla.Env{
  755. status: 200,
  756. body: File.read!("test/fixtures/tesla_mock/macgirvin.com_host_meta")
  757. }}
  758. end
  759. def get("http://gerzilla.de/.well-known/host-meta", _, _, _) do
  760. {:ok,
  761. %Tesla.Env{
  762. status: 200,
  763. body: File.read!("test/fixtures/tesla_mock/gerzilla.de_host_meta")
  764. }}
  765. end
  766. def get(
  767. "https://gerzilla.de/xrd/?uri=acct:kaniini@gerzilla.de",
  768. _,
  769. _,
  770. [{"accept", "application/xrd+xml,application/jrd+json"}]
  771. ) do
  772. {:ok,
  773. %Tesla.Env{
  774. status: 200,
  775. headers: [{"content-type", "application/jrd+json"}],
  776. body: File.read!("test/fixtures/tesla_mock/kaniini@gerzilla.de.json")
  777. }}
  778. end
  779. def get(
  780. "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/23211",
  781. _,
  782. _,
  783. _
  784. ) do
  785. {:ok,
  786. %Tesla.Env{
  787. status: 200,
  788. body: File.read!("test/fixtures/tesla_mock/https___social.heldscal.la_user_23211.xml")
  789. }}
  790. end
  791. def get("http://social.heldscal.la/.well-known/host-meta", _, _, _) do
  792. {:ok,
  793. %Tesla.Env{
  794. status: 200,
  795. body: File.read!("test/fixtures/tesla_mock/social.heldscal.la_host_meta")
  796. }}
  797. end
  798. def get("https://social.heldscal.la/.well-known/host-meta", _, _, _) do
  799. {:ok,
  800. %Tesla.Env{
  801. status: 200,
  802. body: File.read!("test/fixtures/tesla_mock/social.heldscal.la_host_meta")
  803. }}
  804. end
  805. def get("https://mastodon.social/users/lambadalambda", _, _, _) do
  806. {:ok,
  807. %Tesla.Env{
  808. status: 200,
  809. body: File.read!("test/fixtures/lambadalambda.json"),
  810. headers: activitypub_object_headers()
  811. }}
  812. end
  813. def get("https://apfed.club/channel/indio", _, _, _) do
  814. {:ok,
  815. %Tesla.Env{
  816. status: 200,
  817. body: File.read!("test/fixtures/tesla_mock/osada-user-indio.json"),
  818. headers: activitypub_object_headers()
  819. }}
  820. end
  821. def get("https://social.heldscal.la/user/23211", _, _, [{"accept", "application/activity+json"}]) do
  822. {:ok, Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)}
  823. end
  824. def get("http://example.com/ogp", _, _, _) do
  825. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
  826. end
  827. def get("https://example.com/ogp", _, _, _) do
  828. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
  829. end
  830. def get("https://pleroma.local/notice/9kCP7V", _, _, _) do
  831. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
  832. end
  833. def get("http://localhost:4001/users/masto_closed/followers", _, _, _) do
  834. {:ok,
  835. %Tesla.Env{
  836. status: 200,
  837. body: File.read!("test/fixtures/users_mock/masto_closed_followers.json"),
  838. headers: activitypub_object_headers()
  839. }}
  840. end
  841. def get("http://localhost:4001/users/masto_closed/followers?page=1", _, _, _) do
  842. {:ok,
  843. %Tesla.Env{
  844. status: 200,
  845. body: File.read!("test/fixtures/users_mock/masto_closed_followers_page.json"),
  846. headers: activitypub_object_headers()
  847. }}
  848. end
  849. def get("http://localhost:4001/users/masto_closed/following", _, _, _) do
  850. {:ok,
  851. %Tesla.Env{
  852. status: 200,
  853. body: File.read!("test/fixtures/users_mock/masto_closed_following.json"),
  854. headers: activitypub_object_headers()
  855. }}
  856. end
  857. def get("http://localhost:4001/users/masto_closed/following?page=1", _, _, _) do
  858. {:ok,
  859. %Tesla.Env{
  860. status: 200,
  861. body: File.read!("test/fixtures/users_mock/masto_closed_following_page.json"),
  862. headers: activitypub_object_headers()
  863. }}
  864. end
  865. def get("http://localhost:8080/followers/fuser3", _, _, _) do
  866. {:ok,
  867. %Tesla.Env{
  868. status: 200,
  869. body: File.read!("test/fixtures/users_mock/friendica_followers.json"),
  870. headers: activitypub_object_headers()
  871. }}
  872. end
  873. def get("http://localhost:8080/following/fuser3", _, _, _) do
  874. {:ok,
  875. %Tesla.Env{
  876. status: 200,
  877. body: File.read!("test/fixtures/users_mock/friendica_following.json"),
  878. headers: activitypub_object_headers()
  879. }}
  880. end
  881. def get("http://localhost:4001/users/fuser2/followers", _, _, _) do
  882. {:ok,
  883. %Tesla.Env{
  884. status: 200,
  885. body: File.read!("test/fixtures/users_mock/pleroma_followers.json"),
  886. headers: activitypub_object_headers()
  887. }}
  888. end
  889. def get("http://localhost:4001/users/fuser2/following", _, _, _) do
  890. {:ok,
  891. %Tesla.Env{
  892. status: 200,
  893. body: File.read!("test/fixtures/users_mock/pleroma_following.json"),
  894. headers: activitypub_object_headers()
  895. }}
  896. end
  897. def get("http://domain-with-errors:4001/users/fuser1/followers", _, _, _) do
  898. {:ok,
  899. %Tesla.Env{
  900. status: 504,
  901. body: ""
  902. }}
  903. end
  904. def get("http://domain-with-errors:4001/users/fuser1/following", _, _, _) do
  905. {:ok,
  906. %Tesla.Env{
  907. status: 504,
  908. body: ""
  909. }}
  910. end
  911. def get("http://example.com/ogp-missing-data", _, _, _) do
  912. {:ok,
  913. %Tesla.Env{
  914. status: 200,
  915. body: File.read!("test/fixtures/rich_media/ogp-missing-data.html")
  916. }}
  917. end
  918. def get("https://example.com/ogp-missing-data", _, _, _) do
  919. {:ok,
  920. %Tesla.Env{
  921. status: 200,
  922. body: File.read!("test/fixtures/rich_media/ogp-missing-data.html")
  923. }}
  924. end
  925. def get("http://example.com/malformed", _, _, _) do
  926. {:ok,
  927. %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/malformed-data.html")}}
  928. end
  929. def get("http://example.com/empty", _, _, _) do
  930. {:ok, %Tesla.Env{status: 200, body: "hello"}}
  931. end
  932. def get("http://404.site" <> _, _, _, _) do
  933. {:ok,
  934. %Tesla.Env{
  935. status: 404,
  936. body: ""
  937. }}
  938. end
  939. def get(
  940. "https://zetsubou.xn--q9jyb4c/.well-known/webfinger?resource=acct:lain@zetsubou.xn--q9jyb4c",
  941. _,
  942. _,
  943. [{"accept", "application/xrd+xml,application/jrd+json"}]
  944. ) do
  945. {:ok,
  946. %Tesla.Env{
  947. status: 200,
  948. body: File.read!("test/fixtures/lain.xml"),
  949. headers: [{"content-type", "application/xrd+xml"}]
  950. }}
  951. end
  952. def get(
  953. "https://zetsubou.xn--q9jyb4c/.well-known/webfinger?resource=acct:https://zetsubou.xn--q9jyb4c/users/lain",
  954. _,
  955. _,
  956. [{"accept", "application/xrd+xml,application/jrd+json"}]
  957. ) do
  958. {:ok,
  959. %Tesla.Env{
  960. status: 200,
  961. body: File.read!("test/fixtures/lain.xml"),
  962. headers: [{"content-type", "application/xrd+xml"}]
  963. }}
  964. end
  965. def get("http://zetsubou.xn--q9jyb4c/.well-known/host-meta", _, _, _) do
  966. {:ok,
  967. %Tesla.Env{
  968. status: 200,
  969. body: File.read!("test/fixtures/host-meta-zetsubou.xn--q9jyb4c.xml")
  970. }}
  971. end
  972. def get(
  973. "https://zetsubou.xn--q9jyb4c/.well-known/host-meta",
  974. _,
  975. _,
  976. _
  977. ) do
  978. {:ok,
  979. %Tesla.Env{
  980. status: 200,
  981. body: File.read!("test/fixtures/host-meta-zetsubou.xn--q9jyb4c.xml")
  982. }}
  983. end
  984. def get("https://info.pleroma.site/activity.json", _, _, [
  985. {"accept", "application/activity+json"}
  986. ]) do
  987. {:ok,
  988. %Tesla.Env{
  989. status: 200,
  990. body: File.read!("test/fixtures/tesla_mock/https__info.pleroma.site_activity.json"),
  991. headers: activitypub_object_headers()
  992. }}
  993. end
  994. def get("https://info.pleroma.site/activity.json", _, _, _) do
  995. {:ok, %Tesla.Env{status: 404, body: ""}}
  996. end
  997. def get("https://info.pleroma.site/activity2.json", _, _, [
  998. {"accept", "application/activity+json"}
  999. ]) do
  1000. {:ok,
  1001. %Tesla.Env{
  1002. status: 200,
  1003. body: File.read!("test/fixtures/tesla_mock/https__info.pleroma.site_activity2.json"),
  1004. headers: activitypub_object_headers()
  1005. }}
  1006. end
  1007. def get("https://info.pleroma.site/activity2.json", _, _, _) do
  1008. {:ok, %Tesla.Env{status: 404, body: ""}}
  1009. end
  1010. def get("https://info.pleroma.site/activity3.json", _, _, [
  1011. {"accept", "application/activity+json"}
  1012. ]) do
  1013. {:ok,
  1014. %Tesla.Env{
  1015. status: 200,
  1016. body: File.read!("test/fixtures/tesla_mock/https__info.pleroma.site_activity3.json"),
  1017. headers: activitypub_object_headers()
  1018. }}
  1019. end
  1020. def get("https://info.pleroma.site/activity3.json", _, _, _) do
  1021. {:ok, %Tesla.Env{status: 404, body: ""}}
  1022. end
  1023. def get("https://mstdn.jp/.well-known/webfinger?resource=acct:kpherox@mstdn.jp", _, _, _) do
  1024. {:ok,
  1025. %Tesla.Env{
  1026. status: 200,
  1027. body: File.read!("test/fixtures/tesla_mock/kpherox@mstdn.jp.xml"),
  1028. headers: [{"content-type", "application/xrd+xml"}]
  1029. }}
  1030. end
  1031. def get("https://10.111.10.1/notice/9kCP7V", _, _, _) do
  1032. {:ok, %Tesla.Env{status: 200, body: ""}}
  1033. end
  1034. def get("https://172.16.32.40/notice/9kCP7V", _, _, _) do
  1035. {:ok, %Tesla.Env{status: 200, body: ""}}
  1036. end
  1037. def get("https://192.168.10.40/notice/9kCP7V", _, _, _) do
  1038. {:ok, %Tesla.Env{status: 200, body: ""}}
  1039. end
  1040. def get("https://www.patreon.com/posts/mastodon-2-9-and-28121681", _, _, _) do
  1041. {:ok, %Tesla.Env{status: 200, body: ""}}
  1042. end
  1043. def get("http://mastodon.example.org/@admin/99541947525187367", _, _, _) do
  1044. {:ok,
  1045. %Tesla.Env{
  1046. status: 200,
  1047. body: File.read!("test/fixtures/mastodon-post-activity.json"),
  1048. headers: activitypub_object_headers()
  1049. }}
  1050. end
  1051. def get("https://info.pleroma.site/activity4.json", _, _, _) do
  1052. {:ok, %Tesla.Env{status: 500, body: "Error occurred"}}
  1053. end
  1054. def get("http://example.com/rel_me/anchor", _, _, _) do
  1055. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_anchor.html")}}
  1056. end
  1057. def get("http://example.com/rel_me/anchor_nofollow", _, _, _) do
  1058. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_anchor_nofollow.html")}}
  1059. end
  1060. def get("http://example.com/rel_me/link", _, _, _) do
  1061. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_link.html")}}
  1062. end
  1063. def get("http://example.com/rel_me/null", _, _, _) do
  1064. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_null.html")}}
  1065. end
  1066. def get("https://skippers-bin.com/notes/7x9tmrp97i", _, _, _) do
  1067. {:ok,
  1068. %Tesla.Env{
  1069. status: 200,
  1070. body: File.read!("test/fixtures/tesla_mock/misskey_poll_no_end_date.json"),
  1071. headers: activitypub_object_headers()
  1072. }}
  1073. end
  1074. def get("https://example.org/emoji/firedfox.png", _, _, _) do
  1075. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/image.jpg")}}
  1076. end
  1077. def get("https://skippers-bin.com/users/7v1w1r8ce6", _, _, _) do
  1078. {:ok,
  1079. %Tesla.Env{
  1080. status: 200,
  1081. body: File.read!("test/fixtures/tesla_mock/sjw.json"),
  1082. headers: activitypub_object_headers()
  1083. }}
  1084. end
  1085. def get("https://patch.cx/users/rin", _, _, _) do
  1086. {:ok,
  1087. %Tesla.Env{
  1088. status: 200,
  1089. body: File.read!("test/fixtures/tesla_mock/rin.json"),
  1090. headers: activitypub_object_headers()
  1091. }}
  1092. end
  1093. def get(
  1094. "https://channels.tests.funkwhale.audio/federation/music/uploads/42342395-0208-4fee-a38d-259a6dae0871",
  1095. _,
  1096. _,
  1097. _
  1098. ) do
  1099. {:ok,
  1100. %Tesla.Env{
  1101. status: 200,
  1102. body: File.read!("test/fixtures/tesla_mock/funkwhale_audio.json"),
  1103. headers: activitypub_object_headers()
  1104. }}
  1105. end
  1106. def get("https://channels.tests.funkwhale.audio/federation/actors/compositions", _, _, _) do
  1107. {:ok,
  1108. %Tesla.Env{
  1109. status: 200,
  1110. body: File.read!("test/fixtures/tesla_mock/funkwhale_channel.json"),
  1111. headers: activitypub_object_headers()
  1112. }}
  1113. end
  1114. def get("http://example.com/rel_me/error", _, _, _) do
  1115. {:ok, %Tesla.Env{status: 404, body: ""}}
  1116. end
  1117. def get("https://relay.mastodon.host/actor", _, _, _) do
  1118. {:ok,
  1119. %Tesla.Env{
  1120. status: 200,
  1121. body: File.read!("test/fixtures/relay/relay.json"),
  1122. headers: activitypub_object_headers()
  1123. }}
  1124. end
  1125. def get("http://localhost:4001/", _, "", [{"accept", "text/html"}]) do
  1126. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/7369654.html")}}
  1127. end
  1128. def get("https://osada.macgirvin.com/", _, "", [{"accept", "text/html"}]) do
  1129. {:ok,
  1130. %Tesla.Env{
  1131. status: 200,
  1132. body: File.read!("test/fixtures/tesla_mock/https___osada.macgirvin.com.html")
  1133. }}
  1134. end
  1135. def get(url, query, body, headers) do
  1136. {:error,
  1137. "Mock response not implemented for GET #{inspect(url)}, #{query}, #{inspect(body)}, #{
  1138. inspect(headers)
  1139. }"}
  1140. end
  1141. # POST Requests
  1142. #
  1143. def post(url, query \\ [], body \\ [], headers \\ [])
  1144. def post("https://relay.mastodon.host/inbox", _, _, _) do
  1145. {:ok, %Tesla.Env{status: 200, body: ""}}
  1146. end
  1147. def post("http://example.org/needs_refresh", _, _, _) do
  1148. {:ok,
  1149. %Tesla.Env{
  1150. status: 200,
  1151. body: ""
  1152. }}
  1153. end
  1154. def post("http://mastodon.example.org/inbox", _, _, _) do
  1155. {:ok,
  1156. %Tesla.Env{
  1157. status: 200,
  1158. body: ""
  1159. }}
  1160. end
  1161. def post("https://hubzilla.example.org/inbox", _, _, _) do
  1162. {:ok,
  1163. %Tesla.Env{
  1164. status: 200,
  1165. body: ""
  1166. }}
  1167. end
  1168. def post("http://gs.example.org/index.php/main/salmon/user/1", _, _, _) do
  1169. {:ok,
  1170. %Tesla.Env{
  1171. status: 200,
  1172. body: ""
  1173. }}
  1174. end
  1175. def post("http://200.site" <> _, _, _, _) do
  1176. {:ok,
  1177. %Tesla.Env{
  1178. status: 200,
  1179. body: ""
  1180. }}
  1181. end
  1182. def post("http://connrefused.site" <> _, _, _, _) do
  1183. {:error, :connrefused}
  1184. end
  1185. def post("http://404.site" <> _, _, _, _) do
  1186. {:ok,
  1187. %Tesla.Env{
  1188. status: 404,
  1189. body: ""
  1190. }}
  1191. end
  1192. def post(url, query, body, headers) do
  1193. {:error,
  1194. "Mock response not implemented for POST #{inspect(url)}, #{query}, #{inspect(body)}, #{
  1195. inspect(headers)
  1196. }"}
  1197. end
  1198. # Most of the rich media mocks are missing HEAD requests, so we just return 404.
  1199. @rich_media_mocks [
  1200. "https://example.com/ogp",
  1201. "https://example.com/ogp-missing-data",
  1202. "https://example.com/twitter-card"
  1203. ]
  1204. def head(url, _query, _body, _headers) when url in @rich_media_mocks do
  1205. {:ok, %Tesla.Env{status: 404, body: ""}}
  1206. end
  1207. def head(url, query, body, headers) do
  1208. {:error,
  1209. "Mock response not implemented for HEAD #{inspect(url)}, #{query}, #{inspect(body)}, #{
  1210. inspect(headers)
  1211. }"}
  1212. end
  1213. end