client_test.go 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package client
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "io"
  9. "net/http"
  10. "reflect"
  11. "testing"
  12. "time"
  13. )
  14. type roundTripperFunc func(req *http.Request) (*http.Response, error)
  15. func (fn roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
  16. return fn(req)
  17. }
  18. func newFakeHTTPClient(
  19. t *testing.T,
  20. fn func(t *testing.T, req *http.Request) *http.Response,
  21. ) *http.Client {
  22. return &http.Client{
  23. Transport: roundTripperFunc(
  24. func(req *http.Request) (*http.Response, error) {
  25. return fn(t, req), nil
  26. }),
  27. }
  28. }
  29. func jsonResponseFrom(
  30. t *testing.T,
  31. status int,
  32. headers http.Header,
  33. body any,
  34. ) *http.Response {
  35. data, err := json.Marshal(body)
  36. if err != nil {
  37. t.Fatalf("Unable to marshal body: %v", err)
  38. }
  39. return &http.Response{
  40. StatusCode: status,
  41. Body: io.NopCloser(bytes.NewBuffer(data)),
  42. Header: headers,
  43. }
  44. }
  45. func asJSON(data any) string {
  46. json, err := json.MarshalIndent(data, "", " ")
  47. if err != nil {
  48. panic(err)
  49. }
  50. return string(json)
  51. }
  52. func expectRequest(
  53. t *testing.T,
  54. method string,
  55. url string,
  56. checkBody func(r io.Reader),
  57. req *http.Request,
  58. ) {
  59. if req.Method != method {
  60. t.Fatalf("Expected method to be %s, got %s", method, req.Method)
  61. }
  62. if req.URL.String() != url {
  63. t.Fatalf("Expected URL path to be %s, got %s", url, req.URL)
  64. }
  65. if checkBody != nil {
  66. checkBody(req.Body)
  67. }
  68. }
  69. func expectFromJSON[T any](
  70. t *testing.T,
  71. r io.Reader,
  72. expected *T,
  73. ) {
  74. var got T
  75. if err := json.NewDecoder(r).Decode(&got); err != nil {
  76. t.Fatalf("Expected no error, got %v", err)
  77. }
  78. if !reflect.DeepEqual(&got, expected) {
  79. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(got))
  80. }
  81. }
  82. func TestIncorrectEndpoint(t *testing.T) {
  83. client := NewClient("incorrect url")
  84. if _, err := client.Users(); err == nil {
  85. t.Fatal(`Using an incorrect URL should raise an error`)
  86. }
  87. client = NewClient("")
  88. if _, err := client.Users(); err == nil {
  89. t.Fatal(`Using an empty URL should raise an error`)
  90. }
  91. }
  92. func TestHealthcheck(t *testing.T) {
  93. client := NewClientWithOptions(
  94. "http://mf",
  95. WithHTTPClient(
  96. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  97. expectRequest(t, http.MethodGet, "http://mf/healthcheck", nil, req)
  98. return &http.Response{
  99. StatusCode: http.StatusOK,
  100. Body: io.NopCloser(bytes.NewBufferString("OK")),
  101. }
  102. })))
  103. if err := client.HealthcheckContext(t.Context()); err != nil {
  104. t.Fatalf("Expected no error, got %v", err)
  105. }
  106. }
  107. func TestVersion(t *testing.T) {
  108. expected := &VersionResponse{
  109. Version: "1.0.0",
  110. Commit: "1234567890",
  111. BuildDate: "2021-01-01T00:00:00Z",
  112. GoVersion: "go1.20",
  113. Compiler: "gc",
  114. Arch: "amd64",
  115. OS: "linux",
  116. }
  117. client := NewClientWithOptions(
  118. "http://mf",
  119. WithHTTPClient(
  120. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  121. expectRequest(t, http.MethodGet, "http://mf/v1/version", nil, req)
  122. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  123. })))
  124. res, err := client.VersionContext(t.Context())
  125. if err != nil {
  126. t.Fatalf("Expected no error, got %v", err)
  127. }
  128. if !reflect.DeepEqual(res, expected) {
  129. t.Fatalf("Expected %+v, got %+v", expected, res)
  130. }
  131. }
  132. func TestMe(t *testing.T) {
  133. expected := &User{
  134. ID: 1,
  135. Username: "test",
  136. Password: "password",
  137. IsAdmin: false,
  138. Theme: "light",
  139. Language: "en",
  140. Timezone: "UTC",
  141. EntryDirection: "asc",
  142. EntryOrder: "created_at",
  143. Stylesheet: "default",
  144. CustomJS: "custom.js",
  145. GoogleID: "google-id",
  146. OpenIDConnectID: "openid-connect-id",
  147. EntriesPerPage: 10,
  148. KeyboardShortcuts: true,
  149. ShowReadingTime: true,
  150. EntrySwipe: true,
  151. GestureNav: "horizontal",
  152. DisplayMode: "read",
  153. DefaultReadingSpeed: 1,
  154. CJKReadingSpeed: 1,
  155. DefaultHomePage: "home",
  156. CategoriesSortingOrder: "asc",
  157. MarkReadOnView: true,
  158. MediaPlaybackRate: 1.0,
  159. BlockFilterEntryRules: "block",
  160. KeepFilterEntryRules: "keep",
  161. ExternalFontHosts: "https://fonts.googleapis.com",
  162. AlwaysOpenExternalLinks: true,
  163. OpenExternalLinksInNewTab: true,
  164. }
  165. client := NewClientWithOptions(
  166. "http://mf",
  167. WithHTTPClient(
  168. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  169. expectRequest(t, http.MethodGet, "http://mf/v1/me", nil, req)
  170. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  171. })))
  172. res, err := client.MeContext(t.Context())
  173. if err != nil {
  174. t.Fatalf("Expected no error, got %v", err)
  175. }
  176. if !reflect.DeepEqual(res, expected) {
  177. t.Fatalf("Expected %+v, got %+v", expected, res)
  178. }
  179. }
  180. func TestUsers(t *testing.T) {
  181. expected := Users{
  182. {
  183. ID: 1,
  184. Username: "test1",
  185. },
  186. {
  187. ID: 2,
  188. Username: "test2",
  189. },
  190. }
  191. client := NewClientWithOptions(
  192. "http://mf",
  193. WithHTTPClient(
  194. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  195. expectRequest(t, http.MethodGet, "http://mf/v1/users", nil, req)
  196. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  197. })))
  198. res, err := client.UsersContext(t.Context())
  199. if err != nil {
  200. t.Fatalf("Expected no error, got %v", err)
  201. }
  202. if !reflect.DeepEqual(res, expected) {
  203. t.Fatalf("Expected %+v, got %+v", expected, res)
  204. }
  205. }
  206. func TestUserByID(t *testing.T) {
  207. expected := &User{
  208. ID: 1,
  209. Username: "test",
  210. }
  211. client := NewClientWithOptions(
  212. "http://mf",
  213. WithHTTPClient(
  214. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  215. expectRequest(t, http.MethodGet, "http://mf/v1/users/1", nil, req)
  216. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  217. })))
  218. res, err := client.UserByIDContext(t.Context(), 1)
  219. if err != nil {
  220. t.Fatalf("Expected no error, got %v", err)
  221. }
  222. if !reflect.DeepEqual(res, expected) {
  223. t.Fatalf("Expected %+v, got %+v", expected, res)
  224. }
  225. }
  226. func TestUserByUsername(t *testing.T) {
  227. expected := &User{
  228. ID: 1,
  229. Username: "test",
  230. }
  231. client := NewClientWithOptions(
  232. "http://mf",
  233. WithHTTPClient(
  234. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  235. expectRequest(t, http.MethodGet, "http://mf/v1/users/test", nil, req)
  236. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  237. })))
  238. res, err := client.UserByUsernameContext(t.Context(), "test")
  239. if err != nil {
  240. t.Fatalf("Expected no error, got %v", err)
  241. }
  242. if !reflect.DeepEqual(res, expected) {
  243. t.Fatalf("Expected %+v, got %+v", expected, res)
  244. }
  245. }
  246. func TestCreateUser(t *testing.T) {
  247. expected := &User{
  248. ID: 1,
  249. Username: "test",
  250. Password: "password",
  251. IsAdmin: true,
  252. }
  253. client := NewClientWithOptions(
  254. "http://mf",
  255. WithHTTPClient(
  256. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  257. exp := UserCreationRequest{
  258. Username: "test",
  259. Password: "password",
  260. IsAdmin: true,
  261. }
  262. expectRequest(
  263. t,
  264. http.MethodPost,
  265. "http://mf/v1/users",
  266. func(r io.Reader) {
  267. expectFromJSON(t, r, &exp)
  268. },
  269. req)
  270. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  271. })))
  272. res, err := client.CreateUserContext(t.Context(), "test", "password", true)
  273. if err != nil {
  274. t.Fatalf("Expected no error, got %v", err)
  275. }
  276. if !reflect.DeepEqual(res, expected) {
  277. t.Fatalf("Expected %+v, got %+v", expected, res)
  278. }
  279. }
  280. func TestUpdateUser(t *testing.T) {
  281. expected := &User{
  282. ID: 1,
  283. Username: "test",
  284. Password: "password",
  285. IsAdmin: true,
  286. }
  287. client := NewClientWithOptions(
  288. "http://mf",
  289. WithHTTPClient(
  290. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  291. expectRequest(t, http.MethodPut, "http://mf/v1/users/1", func(r io.Reader) {
  292. expectFromJSON(t, r, &UserModificationRequest{
  293. Username: &expected.Username,
  294. Password: &expected.Password,
  295. })
  296. }, req)
  297. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  298. })))
  299. res, err := client.UpdateUserContext(t.Context(), 1, &UserModificationRequest{
  300. Username: &expected.Username,
  301. Password: &expected.Password,
  302. })
  303. if err != nil {
  304. t.Fatalf("Expected no error, got %v", err)
  305. }
  306. if !reflect.DeepEqual(res, expected) {
  307. t.Fatalf("Expected %+v, got %+v", expected, res)
  308. }
  309. }
  310. func TestDeleteUser(t *testing.T) {
  311. client := NewClientWithOptions(
  312. "http://mf",
  313. WithHTTPClient(
  314. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  315. expectRequest(t, http.MethodDelete, "http://mf/v1/users/1", nil, req)
  316. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  317. })))
  318. if err := client.DeleteUserContext(t.Context(), 1); err != nil {
  319. t.Fatalf("Expected no error, got %v", err)
  320. }
  321. }
  322. func TestAPIKeys(t *testing.T) {
  323. expected := APIKeys{
  324. {
  325. ID: 1,
  326. Token: "token",
  327. Description: "test",
  328. },
  329. {
  330. ID: 2,
  331. Token: "token2",
  332. Description: "test2",
  333. },
  334. }
  335. client := NewClientWithOptions(
  336. "http://mf",
  337. WithHTTPClient(
  338. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  339. expectRequest(t, http.MethodGet, "http://mf/v1/api-keys", nil, req)
  340. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  341. })))
  342. res, err := client.APIKeysContext(t.Context())
  343. if err != nil {
  344. t.Fatalf("Expected no error, got %v", err)
  345. }
  346. if !reflect.DeepEqual(res, expected) {
  347. t.Fatalf("Expected %+v, got %+v", expected, res)
  348. }
  349. }
  350. func TestCreateAPIKey(t *testing.T) {
  351. expected := &APIKey{
  352. ID: 42,
  353. Token: "some-token",
  354. Description: "desc",
  355. }
  356. client := NewClientWithOptions(
  357. "http://mf",
  358. WithHTTPClient(
  359. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  360. expectRequest(t, http.MethodPost, "http://mf/v1/api-keys", func(r io.Reader) {
  361. expectFromJSON(t, r, &APIKeyCreationRequest{
  362. Description: "desc",
  363. })
  364. }, req)
  365. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  366. })))
  367. res, err := client.CreateAPIKeyContext(t.Context(), "desc")
  368. if err != nil {
  369. t.Fatalf("Expected no error, got %v", err)
  370. }
  371. if !reflect.DeepEqual(res, expected) {
  372. t.Fatalf("Expected %+v, got %+v", expected, res)
  373. }
  374. }
  375. func TestDeleteAPIKey(t *testing.T) {
  376. client := NewClientWithOptions(
  377. "http://mf",
  378. WithHTTPClient(
  379. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  380. expectRequest(t, http.MethodDelete, "http://mf/v1/api-keys/1", nil, req)
  381. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  382. })))
  383. if err := client.DeleteAPIKeyContext(t.Context(), 1); err != nil {
  384. t.Fatalf("Expected no error, got %v", err)
  385. }
  386. }
  387. func TestMarkAllAsRead(t *testing.T) {
  388. client := NewClientWithOptions(
  389. "http://mf",
  390. WithHTTPClient(
  391. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  392. expectRequest(t, http.MethodPut, "http://mf/v1/users/1/mark-all-as-read", nil, req)
  393. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  394. })))
  395. if err := client.MarkAllAsReadContext(t.Context(), 1); err != nil {
  396. t.Fatalf("Expected no error, got %v", err)
  397. }
  398. }
  399. func TestIntegrationsStatus(t *testing.T) {
  400. client := NewClientWithOptions(
  401. "http://mf",
  402. WithHTTPClient(
  403. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  404. expectRequest(t, http.MethodGet, "http://mf/v1/integrations/status", nil, req)
  405. return jsonResponseFrom(t, http.StatusOK, http.Header{}, struct {
  406. HasIntegrations bool `json:"has_integrations"`
  407. }{
  408. HasIntegrations: true,
  409. })
  410. })))
  411. status, err := client.IntegrationsStatusContext(t.Context())
  412. if err != nil {
  413. t.Fatalf("Expected no error, got %v", err)
  414. }
  415. if !status {
  416. t.Fatalf("Expected integrations status to be true, got false")
  417. }
  418. }
  419. func TestDiscover(t *testing.T) {
  420. expected := Subscriptions{
  421. {
  422. URL: "http://example.com",
  423. Title: "Example",
  424. Type: "rss",
  425. },
  426. }
  427. client := NewClientWithOptions(
  428. "http://mf",
  429. WithHTTPClient(
  430. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  431. expectRequest(t, http.MethodPost, "http://mf/v1/discover", func(r io.Reader) {
  432. expectFromJSON(t, r, &map[string]string{"url": "http://example.com"})
  433. }, req)
  434. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  435. })))
  436. res, err := client.DiscoverContext(t.Context(), "http://example.com")
  437. if err != nil {
  438. t.Fatalf("Expected no error, got %v", err)
  439. }
  440. if !reflect.DeepEqual(res, expected) {
  441. t.Fatalf("Expected %+v, got %+v", expected, res)
  442. }
  443. }
  444. func TestCategories(t *testing.T) {
  445. expected := Categories{
  446. {
  447. ID: 1,
  448. Title: "Example",
  449. },
  450. }
  451. client := NewClientWithOptions(
  452. "http://mf",
  453. WithHTTPClient(
  454. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  455. expectRequest(t, http.MethodGet, "http://mf/v1/categories", nil, req)
  456. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  457. })))
  458. res, err := client.CategoriesContext(t.Context())
  459. if err != nil {
  460. t.Fatalf("Expected no error, got %v", err)
  461. }
  462. if !reflect.DeepEqual(res, expected) {
  463. t.Fatalf("Expected %+v, got %+v", expected, res)
  464. }
  465. }
  466. func TestCategoriesWithCounters(t *testing.T) {
  467. feedCount := 1
  468. totalUnread := 2
  469. expected := Categories{
  470. {
  471. ID: 1,
  472. Title: "Example",
  473. FeedCount: &feedCount,
  474. TotalUnread: &totalUnread,
  475. },
  476. }
  477. client := NewClientWithOptions(
  478. "http://mf",
  479. WithHTTPClient(
  480. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  481. expectRequest(t, http.MethodGet, "http://mf/v1/categories?counts=true", nil, req)
  482. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  483. })))
  484. res, err := client.CategoriesWithCountersContext(t.Context())
  485. if err != nil {
  486. t.Fatalf("Expected no error, got %v", err)
  487. }
  488. if !reflect.DeepEqual(res, expected) {
  489. t.Fatalf("Expected %+v, got %+v", expected, res)
  490. }
  491. }
  492. func TestCreateCategory(t *testing.T) {
  493. expected := &Category{
  494. ID: 1,
  495. Title: "Example",
  496. }
  497. client := NewClientWithOptions(
  498. "http://mf",
  499. WithHTTPClient(
  500. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  501. expectRequest(t, http.MethodPost, "http://mf/v1/categories", func(r io.Reader) {
  502. expectFromJSON(t, r, &CategoryCreationRequest{
  503. Title: "Example",
  504. })
  505. }, req)
  506. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  507. })))
  508. res, err := client.CreateCategoryContext(t.Context(), "Example")
  509. if err != nil {
  510. t.Fatalf("Expected no error, got %v", err)
  511. }
  512. if !reflect.DeepEqual(res, expected) {
  513. t.Fatalf("Expected %+v, got %+v", expected, res)
  514. }
  515. }
  516. func TestCreateCategoryWithOptions(t *testing.T) {
  517. expected := &Category{
  518. ID: 1,
  519. Title: "Example",
  520. HideGlobally: true,
  521. }
  522. client := NewClientWithOptions(
  523. "http://mf",
  524. WithHTTPClient(
  525. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  526. expectRequest(t, http.MethodPost, "http://mf/v1/categories", func(r io.Reader) {
  527. expectFromJSON(t, r, &CategoryCreationRequest{
  528. Title: "Example",
  529. HideGlobally: true,
  530. })
  531. }, req)
  532. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  533. })))
  534. res, err := client.CreateCategoryWithOptionsContext(t.Context(), &CategoryCreationRequest{
  535. Title: "Example",
  536. HideGlobally: true,
  537. })
  538. if err != nil {
  539. t.Fatalf("Expected no error, got %v", err)
  540. }
  541. if !reflect.DeepEqual(res, expected) {
  542. t.Fatalf("Expected %+v, got %+v", expected, res)
  543. }
  544. }
  545. func TestUpdateCategory(t *testing.T) {
  546. expected := &Category{
  547. ID: 1,
  548. Title: "Example",
  549. }
  550. client := NewClientWithOptions(
  551. "http://mf",
  552. WithHTTPClient(
  553. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  554. expectRequest(t, http.MethodPut, "http://mf/v1/categories/1", func(r io.Reader) {
  555. expectFromJSON(t, r, &CategoryModificationRequest{
  556. Title: &expected.Title,
  557. })
  558. }, req)
  559. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  560. })))
  561. res, err := client.UpdateCategoryContext(t.Context(), 1, "Example")
  562. if err != nil {
  563. t.Fatalf("Expected no error, got %v", err)
  564. }
  565. if !reflect.DeepEqual(res, expected) {
  566. t.Fatalf("Expected %+v, got %+v", expected, res)
  567. }
  568. }
  569. func TestUpdateCategoryWithOptions(t *testing.T) {
  570. expected := &Category{
  571. ID: 1,
  572. Title: "Example",
  573. HideGlobally: true,
  574. }
  575. client := NewClientWithOptions(
  576. "http://mf",
  577. WithHTTPClient(
  578. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  579. expectRequest(t, http.MethodPut, "http://mf/v1/categories/1", func(r io.Reader) {
  580. expectFromJSON(t, r, &CategoryModificationRequest{
  581. Title: &expected.Title,
  582. HideGlobally: &expected.HideGlobally,
  583. })
  584. }, req)
  585. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  586. })))
  587. res, err := client.UpdateCategoryWithOptionsContext(t.Context(), 1, &CategoryModificationRequest{
  588. Title: &expected.Title,
  589. HideGlobally: &expected.HideGlobally,
  590. })
  591. if err != nil {
  592. t.Fatalf("Expected no error, got %v", err)
  593. }
  594. if !reflect.DeepEqual(res, expected) {
  595. t.Fatalf("Expected %+v, got %+v", expected, res)
  596. }
  597. }
  598. func TestMarkCategoryAsRead(t *testing.T) {
  599. client := NewClientWithOptions(
  600. "http://mf",
  601. WithHTTPClient(
  602. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  603. expectRequest(t, http.MethodPut, "http://mf/v1/categories/1/mark-all-as-read", nil, req)
  604. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  605. })))
  606. if err := client.MarkCategoryAsReadContext(t.Context(), 1); err != nil {
  607. t.Fatalf("Expected no error, got %v", err)
  608. }
  609. }
  610. func TestCategoryFeeds(t *testing.T) {
  611. expected := Feeds{
  612. {
  613. ID: 1,
  614. Title: "Example",
  615. },
  616. }
  617. client := NewClientWithOptions(
  618. "http://mf",
  619. WithHTTPClient(
  620. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  621. expectRequest(t, http.MethodGet, "http://mf/v1/categories/1/feeds", nil, req)
  622. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  623. })))
  624. res, err := client.CategoryFeedsContext(t.Context(), 1)
  625. if err != nil {
  626. t.Fatalf("Expected no error, got %v", err)
  627. }
  628. if !reflect.DeepEqual(res, expected) {
  629. t.Fatalf("Expected %+v, got %+v", expected, res)
  630. }
  631. }
  632. func TestDeleteCategory(t *testing.T) {
  633. client := NewClientWithOptions(
  634. "http://mf",
  635. WithHTTPClient(
  636. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  637. expectRequest(t, http.MethodDelete, "http://mf/v1/categories/1", nil, req)
  638. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  639. })))
  640. if err := client.DeleteCategoryContext(t.Context(), 1); err != nil {
  641. t.Fatalf("Expected no error, got %v", err)
  642. }
  643. }
  644. func TestRefreshCategory(t *testing.T) {
  645. client := NewClientWithOptions(
  646. "http://mf",
  647. WithHTTPClient(
  648. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  649. expectRequest(t, http.MethodPut, "http://mf/v1/categories/1/refresh", nil, req)
  650. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  651. })))
  652. if err := client.RefreshCategoryContext(t.Context(), 1); err != nil {
  653. t.Fatalf("Expected no error, got %v", err)
  654. }
  655. }
  656. func TestFeeds(t *testing.T) {
  657. expected := Feeds{
  658. {
  659. ID: 1,
  660. Title: "Example",
  661. FeedURL: "http://example.com",
  662. SiteURL: "http://example.com",
  663. CheckedAt: time.Date(1970, 1, 1, 0, 7, 0, 0, time.UTC),
  664. Disabled: false,
  665. IgnoreHTTPCache: false,
  666. AllowSelfSignedCertificates: false,
  667. FetchViaProxy: false,
  668. ScraperRules: "",
  669. RewriteRules: "",
  670. UrlRewriteRules: "",
  671. BlocklistRules: "",
  672. KeeplistRules: "",
  673. BlockFilterEntryRules: "",
  674. KeepFilterEntryRules: "",
  675. Crawler: false,
  676. UserAgent: "",
  677. Cookie: "",
  678. Username: "",
  679. Password: "",
  680. Category: &Category{
  681. ID: 1,
  682. Title: "Example",
  683. },
  684. HideGlobally: false,
  685. DisableHTTP2: false,
  686. ProxyURL: "",
  687. },
  688. {
  689. ID: 2,
  690. Title: "Example 2",
  691. },
  692. }
  693. client := NewClientWithOptions(
  694. "http://mf",
  695. WithHTTPClient(
  696. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  697. expectRequest(t, http.MethodGet, "http://mf/v1/feeds", nil, req)
  698. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  699. })))
  700. res, err := client.FeedsContext(t.Context())
  701. if err != nil {
  702. t.Fatalf("Expected no error, got %v", err)
  703. }
  704. if !reflect.DeepEqual(res, expected) {
  705. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  706. }
  707. }
  708. func TestExport(t *testing.T) {
  709. expected := []byte("hello")
  710. client := NewClientWithOptions(
  711. "http://mf",
  712. WithHTTPClient(
  713. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  714. expectRequest(t, http.MethodGet, "http://mf/v1/export", nil, req)
  715. return &http.Response{
  716. StatusCode: http.StatusOK,
  717. Body: io.NopCloser(bytes.NewBufferString(string(expected))),
  718. Header: http.Header{},
  719. }
  720. })))
  721. res, err := client.ExportContext(t.Context())
  722. if err != nil {
  723. t.Fatalf("Expected no error, got %v", err)
  724. }
  725. if !reflect.DeepEqual(res, expected) {
  726. t.Fatalf("Expected %+v, got %+v", expected, res)
  727. }
  728. }
  729. func TestImport(t *testing.T) {
  730. expected := []byte("hello")
  731. client := NewClientWithOptions(
  732. "http://mf",
  733. WithHTTPClient(
  734. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  735. expectRequest(
  736. t,
  737. http.MethodPost,
  738. "http://mf/v1/import",
  739. func(r io.Reader) {
  740. b, err := io.ReadAll(r)
  741. if err != nil {
  742. t.Fatalf("Expected no error, got %v", err)
  743. }
  744. if !bytes.Equal(b, expected) {
  745. t.Fatalf("expected %+v, got %+v", expected, b)
  746. }
  747. },
  748. req)
  749. return &http.Response{
  750. StatusCode: http.StatusOK,
  751. Header: http.Header{},
  752. }
  753. })))
  754. if err := client.ImportContext(t.Context(), io.NopCloser(bytes.NewBufferString(string(expected)))); err != nil {
  755. t.Fatalf("Expected no error, got %v", err)
  756. }
  757. }
  758. func TestFeed(t *testing.T) {
  759. expected := &Feed{
  760. ID: 1,
  761. Title: "Example",
  762. }
  763. client := NewClientWithOptions(
  764. "http://mf",
  765. WithHTTPClient(
  766. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  767. expectRequest(t, http.MethodGet, "http://mf/v1/feeds/1", nil, req)
  768. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  769. })))
  770. res, err := client.FeedContext(t.Context(), 1)
  771. if err != nil {
  772. t.Fatalf("Expected no error, got %v", err)
  773. }
  774. if !reflect.DeepEqual(res, expected) {
  775. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  776. }
  777. }
  778. func TestCreateFeed(t *testing.T) {
  779. client := NewClientWithOptions(
  780. "http://mf",
  781. WithHTTPClient(
  782. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  783. expectRequest(t, http.MethodPost, "http://mf/v1/feeds", nil, req)
  784. return jsonResponseFrom(t, http.StatusOK, http.Header{}, struct {
  785. FeedID int64 `json:"feed_id"`
  786. }{
  787. FeedID: 1,
  788. })
  789. })))
  790. id, err := client.CreateFeedContext(t.Context(), &FeedCreationRequest{
  791. FeedURL: "http://example.com",
  792. })
  793. if err != nil {
  794. t.Fatalf("Expected no error, got %v", err)
  795. }
  796. if id != 1 {
  797. t.Fatalf("Expected feed ID to be 1, got %d", id)
  798. }
  799. }
  800. func TestUpdateFeed(t *testing.T) {
  801. expected := &Feed{
  802. ID: 1,
  803. FeedURL: "http://example.com/",
  804. }
  805. client := NewClientWithOptions(
  806. "http://mf",
  807. WithHTTPClient(
  808. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  809. expectRequest(t, http.MethodPut, "http://mf/v1/feeds/1", nil, req)
  810. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  811. })))
  812. res, err := client.UpdateFeedContext(t.Context(), 1, &FeedModificationRequest{
  813. FeedURL: &expected.FeedURL,
  814. })
  815. if err != nil {
  816. t.Fatalf("Expected no error, got %v", err)
  817. }
  818. if !reflect.DeepEqual(res, expected) {
  819. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  820. }
  821. }
  822. func TestMarkFeedAsRead(t *testing.T) {
  823. client := NewClientWithOptions(
  824. "http://mf",
  825. WithHTTPClient(
  826. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  827. expectRequest(t, http.MethodPut, "http://mf/v1/feeds/1/mark-all-as-read", nil, req)
  828. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  829. })))
  830. if err := client.MarkFeedAsReadContext(t.Context(), 1); err != nil {
  831. t.Fatalf("Expected no error, got %v", err)
  832. }
  833. }
  834. func TestRefreshAllFeeds(t *testing.T) {
  835. client := NewClientWithOptions(
  836. "http://mf",
  837. WithHTTPClient(
  838. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  839. expectRequest(t, http.MethodPut, "http://mf/v1/feeds/refresh", nil, req)
  840. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  841. })))
  842. if err := client.RefreshAllFeedsContext(t.Context()); err != nil {
  843. t.Fatalf("Expected no error, got %v", err)
  844. }
  845. }
  846. func TestRefreshFeed(t *testing.T) {
  847. client := NewClientWithOptions(
  848. "http://mf",
  849. WithHTTPClient(
  850. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  851. expectRequest(t, http.MethodPut, "http://mf/v1/feeds/1/refresh", nil, req)
  852. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  853. })))
  854. if err := client.RefreshFeedContext(t.Context(), 1); err != nil {
  855. t.Fatalf("Expected no error, got %v", err)
  856. }
  857. }
  858. func TestForceRefreshFeed(t *testing.T) {
  859. client := NewClientWithOptions(
  860. "http://mf",
  861. WithHTTPClient(
  862. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  863. expectRequest(t, http.MethodPut, "http://mf/v1/feeds/1/refresh?force=true", nil, req)
  864. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  865. })))
  866. if err := client.ForceRefreshFeedContext(t.Context(), 1); err != nil {
  867. t.Fatalf("Expected no error, got %v", err)
  868. }
  869. }
  870. func TestDeleteFeed(t *testing.T) {
  871. client := NewClientWithOptions(
  872. "http://mf",
  873. WithHTTPClient(
  874. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  875. expectRequest(t, http.MethodDelete, "http://mf/v1/feeds/1", nil, req)
  876. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  877. })))
  878. if err := client.DeleteFeedContext(t.Context(), 1); err != nil {
  879. t.Fatalf("Expected no error, got %v", err)
  880. }
  881. }
  882. func TestFeedIcon(t *testing.T) {
  883. expected := &FeedIcon{
  884. ID: 1,
  885. MimeType: "text/plain",
  886. Data: "data",
  887. }
  888. client := NewClientWithOptions(
  889. "http://mf",
  890. WithHTTPClient(
  891. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  892. expectRequest(t, http.MethodGet, "http://mf/v1/feeds/1/icon", nil, req)
  893. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  894. })))
  895. res, err := client.FeedIconContext(t.Context(), 1)
  896. if err != nil {
  897. t.Fatalf("Expected no error, got %v", err)
  898. }
  899. if !reflect.DeepEqual(res, expected) {
  900. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  901. }
  902. }
  903. func TestFeedEntry(t *testing.T) {
  904. expected := &Entry{
  905. ID: 1,
  906. Title: "Example",
  907. }
  908. client := NewClientWithOptions(
  909. "http://mf",
  910. WithHTTPClient(
  911. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  912. expectRequest(t, http.MethodGet, "http://mf/v1/feeds/1/entries/1", nil, req)
  913. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  914. })))
  915. res, err := client.FeedEntryContext(t.Context(), 1, 1)
  916. if err != nil {
  917. t.Fatalf("Expected no error, got %v", err)
  918. }
  919. if !reflect.DeepEqual(res, expected) {
  920. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  921. }
  922. }
  923. func TestCategoryEntry(t *testing.T) {
  924. expected := &Entry{
  925. ID: 1,
  926. Title: "Example",
  927. }
  928. client := NewClientWithOptions(
  929. "http://mf",
  930. WithHTTPClient(
  931. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  932. expectRequest(t, http.MethodGet, "http://mf/v1/categories/1/entries/1", nil, req)
  933. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  934. })))
  935. res, err := client.CategoryEntryContext(t.Context(), 1, 1)
  936. if err != nil {
  937. t.Fatalf("Expected no error, got %v", err)
  938. }
  939. if !reflect.DeepEqual(res, expected) {
  940. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  941. }
  942. }
  943. func TestEntry(t *testing.T) {
  944. expected := &Entry{
  945. ID: 1,
  946. Title: "Example",
  947. }
  948. client := NewClientWithOptions(
  949. "http://mf",
  950. WithHTTPClient(
  951. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  952. expectRequest(t, http.MethodGet, "http://mf/v1/entries/1", nil, req)
  953. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  954. })))
  955. res, err := client.EntryContext(t.Context(), 1)
  956. if err != nil {
  957. t.Fatalf("Expected no error, got %v", err)
  958. }
  959. if !reflect.DeepEqual(res, expected) {
  960. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  961. }
  962. }
  963. func TestEntries(t *testing.T) {
  964. expected := &EntryResultSet{
  965. Total: 1,
  966. Entries: Entries{
  967. {
  968. ID: 1,
  969. Title: "Example",
  970. },
  971. },
  972. }
  973. client := NewClientWithOptions(
  974. "http://mf",
  975. WithHTTPClient(
  976. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  977. expectRequest(t, http.MethodGet, "http://mf/v1/entries", nil, req)
  978. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  979. })))
  980. res, err := client.EntriesContext(t.Context(), nil)
  981. if err != nil {
  982. t.Fatalf("Expected no error, got %v", err)
  983. }
  984. if !reflect.DeepEqual(res, expected) {
  985. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  986. }
  987. }
  988. func TestFeedEntries(t *testing.T) {
  989. expected := &EntryResultSet{
  990. Total: 1,
  991. Entries: Entries{
  992. {
  993. ID: 1,
  994. Title: "Example",
  995. },
  996. },
  997. }
  998. client := NewClientWithOptions(
  999. "http://mf",
  1000. WithHTTPClient(
  1001. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1002. expectRequest(t, http.MethodGet, "http://mf/v1/feeds/1/entries?limit=10&offset=0", nil, req)
  1003. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1004. })))
  1005. res, err := client.FeedEntriesContext(t.Context(), 1, &Filter{
  1006. Limit: 10,
  1007. })
  1008. if err != nil {
  1009. t.Fatalf("Expected no error, got %v", err)
  1010. }
  1011. if !reflect.DeepEqual(res, expected) {
  1012. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1013. }
  1014. }
  1015. func TestCategoryEntries(t *testing.T) {
  1016. expected := &EntryResultSet{
  1017. Total: 1,
  1018. Entries: Entries{
  1019. {
  1020. ID: 1,
  1021. Title: "Example",
  1022. },
  1023. },
  1024. }
  1025. client := NewClientWithOptions(
  1026. "http://mf",
  1027. WithHTTPClient(
  1028. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1029. expectRequest(t, http.MethodGet, "http://mf/v1/categories/1/entries?limit=10&offset=0", nil, req)
  1030. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1031. })))
  1032. res, err := client.CategoryEntriesContext(t.Context(), 1, &Filter{
  1033. Limit: 10,
  1034. })
  1035. if err != nil {
  1036. t.Fatalf("Expected no error, got %v", err)
  1037. }
  1038. if !reflect.DeepEqual(res, expected) {
  1039. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1040. }
  1041. }
  1042. func TestUpdateEntries(t *testing.T) {
  1043. client := NewClientWithOptions(
  1044. "http://mf",
  1045. WithHTTPClient(
  1046. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1047. expectRequest(t, http.MethodPut, "http://mf/v1/entries", nil, req)
  1048. expectFromJSON(t, req.Body, &struct {
  1049. EntryIDs []int64 `json:"entry_ids"`
  1050. Status string `json:"status"`
  1051. }{
  1052. EntryIDs: []int64{1, 2},
  1053. Status: "read",
  1054. })
  1055. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  1056. })))
  1057. if err := client.UpdateEntriesContext(t.Context(), []int64{1, 2}, "read"); err != nil {
  1058. t.Fatalf("Expected no error, got %v", err)
  1059. }
  1060. }
  1061. func TestUpdateEntriesStarred(t *testing.T) {
  1062. starred := true
  1063. client := NewClientWithOptions(
  1064. "http://mf",
  1065. WithHTTPClient(
  1066. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1067. expectRequest(t, http.MethodPut, "http://mf/v1/entries", nil, req)
  1068. expectFromJSON(t, req.Body, &struct {
  1069. EntryIDs []int64 `json:"entry_ids"`
  1070. Starred *bool `json:"starred"`
  1071. }{
  1072. EntryIDs: []int64{1, 2},
  1073. Starred: &starred,
  1074. })
  1075. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  1076. })))
  1077. if err := client.UpdateEntriesStarredContext(t.Context(), []int64{1, 2}, true); err != nil {
  1078. t.Fatalf("Expected no error, got %v", err)
  1079. }
  1080. }
  1081. func TestUpdateEntry(t *testing.T) {
  1082. expected := &Entry{
  1083. ID: 1,
  1084. Title: "Example",
  1085. }
  1086. client := NewClientWithOptions(
  1087. "http://mf",
  1088. WithHTTPClient(
  1089. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1090. expectRequest(t, http.MethodPut, "http://mf/v1/entries/1", nil, req)
  1091. expectFromJSON(t, req.Body, &EntryModificationRequest{
  1092. Title: &expected.Title,
  1093. })
  1094. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1095. })))
  1096. res, err := client.UpdateEntryContext(t.Context(), 1, &EntryModificationRequest{
  1097. Title: &expected.Title,
  1098. })
  1099. if err != nil {
  1100. t.Fatalf("Expected no error, got %v", err)
  1101. }
  1102. if !reflect.DeepEqual(res, expected) {
  1103. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1104. }
  1105. }
  1106. func TestToggleStarred(t *testing.T) {
  1107. client := NewClientWithOptions(
  1108. "http://mf",
  1109. WithHTTPClient(
  1110. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1111. expectRequest(t, http.MethodPut, "http://mf/v1/entries/1/star", nil, req)
  1112. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  1113. })))
  1114. if err := client.ToggleStarredContext(t.Context(), 1); err != nil {
  1115. t.Fatalf("Expected no error, got %v", err)
  1116. }
  1117. }
  1118. func TestShareEntry(t *testing.T) {
  1119. expected := &ShareCodeResponse{
  1120. ShareCode: "ExampleShareCode",
  1121. }
  1122. // The server answers 201 when it creates a share code and 200 when the entry was already shared.
  1123. for _, status := range []int{http.StatusCreated, http.StatusOK} {
  1124. client := NewClientWithOptions(
  1125. "http://mf",
  1126. WithHTTPClient(
  1127. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1128. expectRequest(t, http.MethodPost, "http://mf/v1/entries/1/share", nil, req)
  1129. return jsonResponseFrom(t, status, http.Header{}, expected)
  1130. })))
  1131. shareCode, err := client.ShareEntryContext(t.Context(), 1)
  1132. if err != nil {
  1133. t.Fatalf("Expected no error for status %d, got %v", status, err)
  1134. }
  1135. if shareCode != expected.ShareCode {
  1136. t.Fatalf("Expected '%s' for status %d, got '%s'", expected.ShareCode, status, shareCode)
  1137. }
  1138. }
  1139. }
  1140. func TestUnshareEntry(t *testing.T) {
  1141. client := NewClientWithOptions(
  1142. "http://mf",
  1143. WithHTTPClient(
  1144. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1145. expectRequest(t, http.MethodDelete, "http://mf/v1/entries/1/share", nil, req)
  1146. return jsonResponseFrom(t, http.StatusNoContent, http.Header{}, nil)
  1147. })))
  1148. if err := client.UnshareEntryContext(t.Context(), 1); err != nil {
  1149. t.Fatalf("Expected no error, got %v", err)
  1150. }
  1151. }
  1152. func TestSaveEntry(t *testing.T) {
  1153. client := NewClientWithOptions(
  1154. "http://mf",
  1155. WithHTTPClient(
  1156. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1157. expectRequest(t, http.MethodPost, "http://mf/v1/entries/1/save", nil, req)
  1158. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  1159. })))
  1160. if err := client.SaveEntryContext(t.Context(), 1); err != nil {
  1161. t.Fatalf("Expected no error, got %v", err)
  1162. }
  1163. }
  1164. func TestFetchEntryOriginalContent(t *testing.T) {
  1165. expected := "Example"
  1166. client := NewClientWithOptions(
  1167. "http://mf",
  1168. WithHTTPClient(
  1169. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1170. expectRequest(t, http.MethodGet, "http://mf/v1/entries/1/fetch-content", nil, req)
  1171. return jsonResponseFrom(t, http.StatusOK, http.Header{}, struct {
  1172. Content string `json:"content"`
  1173. }{
  1174. Content: expected,
  1175. })
  1176. })))
  1177. res, err := client.FetchEntryOriginalContentContext(t.Context(), 1)
  1178. if err != nil {
  1179. t.Fatalf("Expected no error, got %v", err)
  1180. }
  1181. if res != expected {
  1182. t.Fatalf("Expected %s, got %s", expected, res)
  1183. }
  1184. }
  1185. func TestFetchCounters(t *testing.T) {
  1186. expected := &FeedCounters{
  1187. ReadCounters: map[int64]int{
  1188. 2: 1,
  1189. },
  1190. UnreadCounters: map[int64]int{
  1191. 3: 1,
  1192. },
  1193. }
  1194. client := NewClientWithOptions(
  1195. "http://mf",
  1196. WithHTTPClient(
  1197. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1198. expectRequest(t, http.MethodGet, "http://mf/v1/feeds/counters", nil, req)
  1199. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1200. })))
  1201. res, err := client.FetchCountersContext(t.Context())
  1202. if err != nil {
  1203. t.Fatalf("Expected no error, got %v", err)
  1204. }
  1205. if !reflect.DeepEqual(res, expected) {
  1206. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1207. }
  1208. }
  1209. func TestFlushHistory(t *testing.T) {
  1210. client := NewClientWithOptions(
  1211. "http://mf",
  1212. WithHTTPClient(
  1213. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1214. expectRequest(t, http.MethodPut, "http://mf/v1/flush-history", nil, req)
  1215. return jsonResponseFrom(t, http.StatusOK, http.Header{}, nil)
  1216. })))
  1217. if err := client.FlushHistoryContext(t.Context()); err != nil {
  1218. t.Fatalf("Expected no error, got %v", err)
  1219. }
  1220. }
  1221. func TestIcon(t *testing.T) {
  1222. expected := &FeedIcon{
  1223. ID: 1,
  1224. MimeType: "text/plain",
  1225. Data: "data",
  1226. }
  1227. client := NewClientWithOptions(
  1228. "http://mf",
  1229. WithHTTPClient(
  1230. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1231. expectRequest(t, http.MethodGet, "http://mf/v1/icons/1", nil, req)
  1232. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1233. })))
  1234. res, err := client.IconContext(t.Context(), 1)
  1235. if err != nil {
  1236. t.Fatalf("Expected no error, got %v", err)
  1237. }
  1238. if !reflect.DeepEqual(res, expected) {
  1239. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1240. }
  1241. }
  1242. func TestEnclosure(t *testing.T) {
  1243. expected := &Enclosure{
  1244. ID: 1,
  1245. URL: "http://example.com",
  1246. MimeType: "text/plain",
  1247. }
  1248. client := NewClientWithOptions(
  1249. "http://mf",
  1250. WithHTTPClient(
  1251. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1252. expectRequest(t, http.MethodGet, "http://mf/v1/enclosures/1", nil, req)
  1253. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1254. })))
  1255. res, err := client.EnclosureContext(t.Context(), 1)
  1256. if err != nil {
  1257. t.Fatalf("Expected no error, got %v", err)
  1258. }
  1259. if !reflect.DeepEqual(res, expected) {
  1260. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1261. }
  1262. }
  1263. func TestUpdateEnclosure(t *testing.T) {
  1264. expected := &Enclosure{
  1265. ID: 1,
  1266. URL: "http://example.com",
  1267. MimeType: "text/plain",
  1268. }
  1269. client := NewClientWithOptions(
  1270. "http://mf",
  1271. WithHTTPClient(
  1272. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1273. expectRequest(t, http.MethodPut, "http://mf/v1/enclosures/1", nil, req)
  1274. expectFromJSON(t, req.Body, &EnclosureUpdateRequest{
  1275. MediaProgression: 10,
  1276. })
  1277. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1278. })))
  1279. if err := client.UpdateEnclosureContext(t.Context(), 1, &EnclosureUpdateRequest{
  1280. MediaProgression: 10,
  1281. }); err != nil {
  1282. t.Fatalf("Expected no error, got %v", err)
  1283. }
  1284. }
  1285. func TestEntryIDsFilterSerialization(t *testing.T) {
  1286. tests := []struct {
  1287. name string
  1288. filter *EntryIDsFilter
  1289. query string
  1290. }{
  1291. {
  1292. name: "empty_filter",
  1293. filter: &EntryIDsFilter{},
  1294. },
  1295. {
  1296. name: "non_positive_pagination",
  1297. filter: &EntryIDsFilter{Limit: -1, Offset: -1},
  1298. },
  1299. {
  1300. name: "starred_false",
  1301. filter: &EntryIDsFilter{Starred: new(false)},
  1302. query: "?starred=false",
  1303. },
  1304. {
  1305. name: "order",
  1306. filter: &EntryIDsFilter{Order: "published_at"},
  1307. query: "?order=published_at",
  1308. },
  1309. {
  1310. name: "direction",
  1311. filter: &EntryIDsFilter{Direction: "asc"},
  1312. query: "?direction=asc",
  1313. },
  1314. {
  1315. name: "feed_id",
  1316. filter: &EntryIDsFilter{FeedID: 4294967297},
  1317. query: "?feed_id=4294967297",
  1318. },
  1319. {
  1320. name: "category_id",
  1321. filter: &EntryIDsFilter{CategoryID: 4294967298},
  1322. query: "?category_id=4294967298",
  1323. },
  1324. {
  1325. name: "before",
  1326. filter: &EntryIDsFilter{Before: 2147483648},
  1327. query: "?before=2147483648",
  1328. },
  1329. {
  1330. name: "after",
  1331. filter: &EntryIDsFilter{After: 2147483649},
  1332. query: "?after=2147483649",
  1333. },
  1334. {
  1335. name: "published_before",
  1336. filter: &EntryIDsFilter{PublishedBefore: 2147483650},
  1337. query: "?published_before=2147483650",
  1338. },
  1339. {
  1340. name: "published_after",
  1341. filter: &EntryIDsFilter{PublishedAfter: 2147483651},
  1342. query: "?published_after=2147483651",
  1343. },
  1344. {
  1345. name: "changed_before",
  1346. filter: &EntryIDsFilter{ChangedBefore: 2147483652},
  1347. query: "?changed_before=2147483652",
  1348. },
  1349. {
  1350. name: "changed_after",
  1351. filter: &EntryIDsFilter{ChangedAfter: 2147483653},
  1352. query: "?changed_after=2147483653",
  1353. },
  1354. {
  1355. name: "before_entry_id",
  1356. filter: &EntryIDsFilter{BeforeEntryID: 4294967299},
  1357. query: "?before_entry_id=4294967299",
  1358. },
  1359. {
  1360. name: "after_entry_id",
  1361. filter: &EntryIDsFilter{AfterEntryID: 4294967300},
  1362. query: "?after_entry_id=4294967300",
  1363. },
  1364. {
  1365. name: "search_encoding",
  1366. filter: &EntryIDsFilter{Search: `go client & "feeds" + tags`},
  1367. query: "?search=go+client+%26+%22feeds%22+%2B+tags",
  1368. },
  1369. {
  1370. name: "repeated_statuses",
  1371. filter: &EntryIDsFilter{Statuses: []string{EntryStatusRead, EntryStatusUnread, EntryStatusRead}},
  1372. query: "?status=read&status=unread&status=read",
  1373. },
  1374. {
  1375. name: "statuses_override_status",
  1376. filter: &EntryIDsFilter{Status: "removed", Statuses: []string{EntryStatusRead, EntryStatusUnread}},
  1377. query: "?status=read&status=unread",
  1378. },
  1379. {
  1380. name: "empty_statuses_preserve_status",
  1381. filter: &EntryIDsFilter{Status: EntryStatusUnread, Statuses: []string{}},
  1382. query: "?status=unread",
  1383. },
  1384. {
  1385. name: "repeated_tags",
  1386. filter: &EntryIDsFilter{Tags: []string{"go", "release notes", "go"}},
  1387. query: "?tags=go&tags=release+notes&tags=go",
  1388. },
  1389. {
  1390. name: "globally_visible_true",
  1391. filter: &EntryIDsFilter{GloballyVisible: new(true)},
  1392. query: "?globally_visible=true",
  1393. },
  1394. {
  1395. name: "globally_visible_false",
  1396. filter: &EntryIDsFilter{GloballyVisible: new(false)},
  1397. query: "?globally_visible=false",
  1398. },
  1399. {
  1400. name: "zero_boundaries",
  1401. filter: &EntryIDsFilter{Status: EntryStatusUnread},
  1402. query: "?status=unread",
  1403. },
  1404. {
  1405. name: "negative_feed_id",
  1406. filter: &EntryIDsFilter{FeedID: -1},
  1407. query: "?feed_id=-1",
  1408. },
  1409. {
  1410. name: "negative_category_id",
  1411. filter: &EntryIDsFilter{CategoryID: -2},
  1412. query: "?category_id=-2",
  1413. },
  1414. {
  1415. name: "negative_before",
  1416. filter: &EntryIDsFilter{Before: -3},
  1417. query: "?before=-3",
  1418. },
  1419. {
  1420. name: "negative_after",
  1421. filter: &EntryIDsFilter{After: -4},
  1422. query: "?after=-4",
  1423. },
  1424. {
  1425. name: "negative_published_before",
  1426. filter: &EntryIDsFilter{PublishedBefore: -5},
  1427. query: "?published_before=-5",
  1428. },
  1429. {
  1430. name: "negative_published_after",
  1431. filter: &EntryIDsFilter{PublishedAfter: -6},
  1432. query: "?published_after=-6",
  1433. },
  1434. {
  1435. name: "negative_changed_before",
  1436. filter: &EntryIDsFilter{ChangedBefore: -7},
  1437. query: "?changed_before=-7",
  1438. },
  1439. {
  1440. name: "negative_changed_after",
  1441. filter: &EntryIDsFilter{ChangedAfter: -8},
  1442. query: "?changed_after=-8",
  1443. },
  1444. {
  1445. name: "negative_before_entry_id",
  1446. filter: &EntryIDsFilter{BeforeEntryID: -9},
  1447. query: "?before_entry_id=-9",
  1448. },
  1449. {
  1450. name: "negative_after_entry_id",
  1451. filter: &EntryIDsFilter{AfterEntryID: -10},
  1452. query: "?after_entry_id=-10",
  1453. },
  1454. }
  1455. expected := &EntryIDsResultSet{Total: 1, EntryIDs: []int64{42}}
  1456. for _, tt := range tests {
  1457. t.Run(tt.name, func(t *testing.T) {
  1458. client := NewClientWithOptions(
  1459. "http://mf",
  1460. WithHTTPClient(
  1461. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1462. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids"+tt.query, nil, req)
  1463. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1464. })))
  1465. res, err := client.EntryIDsContext(t.Context(), tt.filter)
  1466. if err != nil {
  1467. t.Fatalf("Expected no error, got %v", err)
  1468. }
  1469. if !reflect.DeepEqual(res, expected) {
  1470. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1471. }
  1472. })
  1473. }
  1474. }
  1475. func TestEntryIDsContextBadRequest(t *testing.T) {
  1476. client := NewClientWithOptions(
  1477. "http://mf",
  1478. WithHTTPClient(
  1479. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1480. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids?feed_id=-1&status=unread", nil, req)
  1481. return jsonResponseFrom(t, http.StatusBadRequest, http.Header{}, errorResponse{
  1482. ErrorMessage: "Invalid feed_id query parameter",
  1483. })
  1484. })))
  1485. res, err := client.EntryIDsContext(t.Context(), &EntryIDsFilter{Status: EntryStatusUnread, FeedID: -1})
  1486. if !errors.Is(err, ErrBadRequest) {
  1487. t.Fatalf("Expected ErrBadRequest, got %v", err)
  1488. }
  1489. if res != nil {
  1490. t.Fatalf("Expected no result on error, got %s", asJSON(res))
  1491. }
  1492. }
  1493. func TestEntryIDsWithAllFilters(t *testing.T) {
  1494. filter := &EntryIDsFilter{
  1495. Limit: 2,
  1496. Offset: 5,
  1497. Starred: new(false),
  1498. Status: "removed",
  1499. Order: "published_at",
  1500. Direction: "asc",
  1501. FeedID: 42,
  1502. CategoryID: 7,
  1503. Before: 1800000000,
  1504. After: 1700000000,
  1505. PublishedBefore: 1790000000,
  1506. PublishedAfter: 1710000000,
  1507. ChangedBefore: 1780000000,
  1508. ChangedAfter: 1720000000,
  1509. BeforeEntryID: 100,
  1510. AfterEntryID: 10,
  1511. Search: `go & "rss"`,
  1512. Statuses: []string{EntryStatusUnread, EntryStatusRead},
  1513. Tags: []string{"go", "release notes"},
  1514. GloballyVisible: new(false),
  1515. }
  1516. expected := &EntryIDsResultSet{Total: 1, EntryIDs: []int64{42}}
  1517. const expectedURL = "http://mf/v1/entries/ids?after=1700000000&after_entry_id=10" +
  1518. "&before=1800000000&before_entry_id=100&category_id=7" +
  1519. "&changed_after=1720000000&changed_before=1780000000&direction=asc" +
  1520. "&feed_id=42&globally_visible=false&limit=2&offset=5&order=published_at" +
  1521. "&published_after=1710000000&published_before=1790000000" +
  1522. "&search=go+%26+%22rss%22&starred=false&status=unread&status=read" +
  1523. "&tags=go&tags=release+notes"
  1524. client := NewClientWithOptions(
  1525. "http://mf",
  1526. WithHTTPClient(
  1527. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1528. expectRequest(t, http.MethodGet, expectedURL, nil, req)
  1529. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1530. })))
  1531. res, err := client.EntryIDs(filter)
  1532. if err != nil {
  1533. t.Fatalf("Expected no error, got %v", err)
  1534. }
  1535. if !reflect.DeepEqual(res, expected) {
  1536. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1537. }
  1538. }
  1539. func TestEntryIDsNoFilter(t *testing.T) {
  1540. expected := &EntryIDsResultSet{
  1541. Total: 2,
  1542. EntryIDs: []int64{1, 2},
  1543. }
  1544. client := NewClientWithOptions(
  1545. "http://mf",
  1546. WithHTTPClient(
  1547. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1548. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids", nil, req)
  1549. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1550. })))
  1551. res, err := client.EntryIDsContext(t.Context(), nil)
  1552. if err != nil {
  1553. t.Fatalf("Expected no error, got %v", err)
  1554. }
  1555. if !reflect.DeepEqual(res, expected) {
  1556. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1557. }
  1558. }
  1559. func TestEntryIDsWithPaginationFilter(t *testing.T) {
  1560. expected := &EntryIDsResultSet{
  1561. Total: 5,
  1562. EntryIDs: []int64{3},
  1563. }
  1564. client := NewClientWithOptions(
  1565. "http://mf",
  1566. WithHTTPClient(
  1567. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1568. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids?limit=1&offset=2", nil, req)
  1569. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1570. })))
  1571. res, err := client.EntryIDsContext(t.Context(), &EntryIDsFilter{Limit: 1, Offset: 2})
  1572. if err != nil {
  1573. t.Fatalf("Expected no error, got %v", err)
  1574. }
  1575. if !reflect.DeepEqual(res, expected) {
  1576. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1577. }
  1578. }
  1579. func TestEntryIDsWithStarredFilter(t *testing.T) {
  1580. expected := &EntryIDsResultSet{
  1581. Total: 1,
  1582. EntryIDs: []int64{42},
  1583. }
  1584. client := NewClientWithOptions(
  1585. "http://mf",
  1586. WithHTTPClient(
  1587. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1588. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids?starred=true", nil, req)
  1589. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1590. })))
  1591. res, err := client.EntryIDsContext(t.Context(), &EntryIDsFilter{Starred: new(true)})
  1592. if err != nil {
  1593. t.Fatalf("Expected no error, got %v", err)
  1594. }
  1595. if !reflect.DeepEqual(res, expected) {
  1596. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1597. }
  1598. }
  1599. func TestEntryIDsWithStatusFilter(t *testing.T) {
  1600. expected := &EntryIDsResultSet{
  1601. Total: 10,
  1602. EntryIDs: []int64{7, 8},
  1603. }
  1604. client := NewClientWithOptions(
  1605. "http://mf",
  1606. WithHTTPClient(
  1607. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1608. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids?status=unread", nil, req)
  1609. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1610. })))
  1611. res, err := client.EntryIDsContext(t.Context(), &EntryIDsFilter{Status: EntryStatusUnread})
  1612. if err != nil {
  1613. t.Fatalf("Expected no error, got %v", err)
  1614. }
  1615. if !reflect.DeepEqual(res, expected) {
  1616. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1617. }
  1618. }
  1619. func TestEntryIDsWithCombinedFilter(t *testing.T) {
  1620. expected := &EntryIDsResultSet{
  1621. Total: 3,
  1622. EntryIDs: []int64{5},
  1623. }
  1624. client := NewClientWithOptions(
  1625. "http://mf",
  1626. WithHTTPClient(
  1627. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1628. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids?limit=2&offset=5&starred=false&status=read", nil, req)
  1629. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1630. })))
  1631. res, err := client.EntryIDsContext(t.Context(), &EntryIDsFilter{Limit: 2, Offset: 5, Starred: new(false), Status: EntryStatusRead})
  1632. if err != nil {
  1633. t.Fatalf("Expected no error, got %v", err)
  1634. }
  1635. if !reflect.DeepEqual(res, expected) {
  1636. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1637. }
  1638. }