client_test.go 50 KB

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