client_test.go 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  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 boolPtr(b bool) *bool { return &b }
  1286. func TestEntryIDsFilterSerialization(t *testing.T) {
  1287. tests := []struct {
  1288. name string
  1289. filter *EntryIDsFilter
  1290. query string
  1291. }{
  1292. {
  1293. name: "empty_filter",
  1294. filter: &EntryIDsFilter{},
  1295. },
  1296. {
  1297. name: "non_positive_pagination",
  1298. filter: &EntryIDsFilter{Limit: -1, Offset: -1},
  1299. },
  1300. {
  1301. name: "starred_false",
  1302. filter: &EntryIDsFilter{Starred: boolPtr(false)},
  1303. query: "?starred=false",
  1304. },
  1305. {
  1306. name: "order",
  1307. filter: &EntryIDsFilter{Order: "published_at"},
  1308. query: "?order=published_at",
  1309. },
  1310. {
  1311. name: "direction",
  1312. filter: &EntryIDsFilter{Direction: "asc"},
  1313. query: "?direction=asc",
  1314. },
  1315. {
  1316. name: "feed_id",
  1317. filter: &EntryIDsFilter{FeedID: 4294967297},
  1318. query: "?feed_id=4294967297",
  1319. },
  1320. {
  1321. name: "category_id",
  1322. filter: &EntryIDsFilter{CategoryID: 4294967298},
  1323. query: "?category_id=4294967298",
  1324. },
  1325. {
  1326. name: "before",
  1327. filter: &EntryIDsFilter{Before: 2147483648},
  1328. query: "?before=2147483648",
  1329. },
  1330. {
  1331. name: "after",
  1332. filter: &EntryIDsFilter{After: 2147483649},
  1333. query: "?after=2147483649",
  1334. },
  1335. {
  1336. name: "published_before",
  1337. filter: &EntryIDsFilter{PublishedBefore: 2147483650},
  1338. query: "?published_before=2147483650",
  1339. },
  1340. {
  1341. name: "published_after",
  1342. filter: &EntryIDsFilter{PublishedAfter: 2147483651},
  1343. query: "?published_after=2147483651",
  1344. },
  1345. {
  1346. name: "changed_before",
  1347. filter: &EntryIDsFilter{ChangedBefore: 2147483652},
  1348. query: "?changed_before=2147483652",
  1349. },
  1350. {
  1351. name: "changed_after",
  1352. filter: &EntryIDsFilter{ChangedAfter: 2147483653},
  1353. query: "?changed_after=2147483653",
  1354. },
  1355. {
  1356. name: "before_entry_id",
  1357. filter: &EntryIDsFilter{BeforeEntryID: 4294967299},
  1358. query: "?before_entry_id=4294967299",
  1359. },
  1360. {
  1361. name: "after_entry_id",
  1362. filter: &EntryIDsFilter{AfterEntryID: 4294967300},
  1363. query: "?after_entry_id=4294967300",
  1364. },
  1365. {
  1366. name: "search_encoding",
  1367. filter: &EntryIDsFilter{Search: `go client & "feeds" + tags`},
  1368. query: "?search=go+client+%26+%22feeds%22+%2B+tags",
  1369. },
  1370. {
  1371. name: "repeated_statuses",
  1372. filter: &EntryIDsFilter{Statuses: []string{EntryStatusRead, EntryStatusUnread, EntryStatusRead}},
  1373. query: "?status=read&status=unread&status=read",
  1374. },
  1375. {
  1376. name: "statuses_override_status",
  1377. filter: &EntryIDsFilter{Status: "removed", Statuses: []string{EntryStatusRead, EntryStatusUnread}},
  1378. query: "?status=read&status=unread",
  1379. },
  1380. {
  1381. name: "empty_statuses_preserve_status",
  1382. filter: &EntryIDsFilter{Status: EntryStatusUnread, Statuses: []string{}},
  1383. query: "?status=unread",
  1384. },
  1385. {
  1386. name: "repeated_tags",
  1387. filter: &EntryIDsFilter{Tags: []string{"go", "release notes", "go"}},
  1388. query: "?tags=go&tags=release+notes&tags=go",
  1389. },
  1390. {
  1391. name: "globally_visible_true",
  1392. filter: &EntryIDsFilter{GloballyVisible: boolPtr(true)},
  1393. query: "?globally_visible=true",
  1394. },
  1395. {
  1396. name: "globally_visible_false",
  1397. filter: &EntryIDsFilter{GloballyVisible: boolPtr(false)},
  1398. query: "?globally_visible=false",
  1399. },
  1400. {
  1401. name: "zero_boundaries",
  1402. filter: &EntryIDsFilter{Status: EntryStatusUnread},
  1403. query: "?status=unread",
  1404. },
  1405. {
  1406. name: "negative_feed_id",
  1407. filter: &EntryIDsFilter{FeedID: -1},
  1408. query: "?feed_id=-1",
  1409. },
  1410. {
  1411. name: "negative_category_id",
  1412. filter: &EntryIDsFilter{CategoryID: -2},
  1413. query: "?category_id=-2",
  1414. },
  1415. {
  1416. name: "negative_before",
  1417. filter: &EntryIDsFilter{Before: -3},
  1418. query: "?before=-3",
  1419. },
  1420. {
  1421. name: "negative_after",
  1422. filter: &EntryIDsFilter{After: -4},
  1423. query: "?after=-4",
  1424. },
  1425. {
  1426. name: "negative_published_before",
  1427. filter: &EntryIDsFilter{PublishedBefore: -5},
  1428. query: "?published_before=-5",
  1429. },
  1430. {
  1431. name: "negative_published_after",
  1432. filter: &EntryIDsFilter{PublishedAfter: -6},
  1433. query: "?published_after=-6",
  1434. },
  1435. {
  1436. name: "negative_changed_before",
  1437. filter: &EntryIDsFilter{ChangedBefore: -7},
  1438. query: "?changed_before=-7",
  1439. },
  1440. {
  1441. name: "negative_changed_after",
  1442. filter: &EntryIDsFilter{ChangedAfter: -8},
  1443. query: "?changed_after=-8",
  1444. },
  1445. {
  1446. name: "negative_before_entry_id",
  1447. filter: &EntryIDsFilter{BeforeEntryID: -9},
  1448. query: "?before_entry_id=-9",
  1449. },
  1450. {
  1451. name: "negative_after_entry_id",
  1452. filter: &EntryIDsFilter{AfterEntryID: -10},
  1453. query: "?after_entry_id=-10",
  1454. },
  1455. }
  1456. expected := &EntryIDsResultSet{Total: 1, EntryIDs: []int64{42}}
  1457. for _, tt := range tests {
  1458. t.Run(tt.name, func(t *testing.T) {
  1459. client := NewClientWithOptions(
  1460. "http://mf",
  1461. WithHTTPClient(
  1462. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1463. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids"+tt.query, nil, req)
  1464. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1465. })))
  1466. res, err := client.EntryIDsContext(t.Context(), tt.filter)
  1467. if err != nil {
  1468. t.Fatalf("Expected no error, got %v", err)
  1469. }
  1470. if !reflect.DeepEqual(res, expected) {
  1471. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1472. }
  1473. })
  1474. }
  1475. }
  1476. func TestEntryIDsContextBadRequest(t *testing.T) {
  1477. client := NewClientWithOptions(
  1478. "http://mf",
  1479. WithHTTPClient(
  1480. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1481. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids?feed_id=-1&status=unread", nil, req)
  1482. return jsonResponseFrom(t, http.StatusBadRequest, http.Header{}, errorResponse{
  1483. ErrorMessage: "Invalid feed_id query parameter",
  1484. })
  1485. })))
  1486. res, err := client.EntryIDsContext(t.Context(), &EntryIDsFilter{Status: EntryStatusUnread, FeedID: -1})
  1487. if !errors.Is(err, ErrBadRequest) {
  1488. t.Fatalf("Expected ErrBadRequest, got %v", err)
  1489. }
  1490. if res != nil {
  1491. t.Fatalf("Expected no result on error, got %s", asJSON(res))
  1492. }
  1493. }
  1494. func TestEntryIDsWithAllFilters(t *testing.T) {
  1495. filter := &EntryIDsFilter{
  1496. Limit: 2,
  1497. Offset: 5,
  1498. Starred: boolPtr(false),
  1499. Status: "removed",
  1500. Order: "published_at",
  1501. Direction: "asc",
  1502. FeedID: 42,
  1503. CategoryID: 7,
  1504. Before: 1800000000,
  1505. After: 1700000000,
  1506. PublishedBefore: 1790000000,
  1507. PublishedAfter: 1710000000,
  1508. ChangedBefore: 1780000000,
  1509. ChangedAfter: 1720000000,
  1510. BeforeEntryID: 100,
  1511. AfterEntryID: 10,
  1512. Search: `go & "rss"`,
  1513. Statuses: []string{EntryStatusUnread, EntryStatusRead},
  1514. Tags: []string{"go", "release notes"},
  1515. GloballyVisible: boolPtr(false),
  1516. }
  1517. expected := &EntryIDsResultSet{Total: 1, EntryIDs: []int64{42}}
  1518. const expectedURL = "http://mf/v1/entries/ids?after=1700000000&after_entry_id=10" +
  1519. "&before=1800000000&before_entry_id=100&category_id=7" +
  1520. "&changed_after=1720000000&changed_before=1780000000&direction=asc" +
  1521. "&feed_id=42&globally_visible=false&limit=2&offset=5&order=published_at" +
  1522. "&published_after=1710000000&published_before=1790000000" +
  1523. "&search=go+%26+%22rss%22&starred=false&status=unread&status=read" +
  1524. "&tags=go&tags=release+notes"
  1525. client := NewClientWithOptions(
  1526. "http://mf",
  1527. WithHTTPClient(
  1528. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1529. expectRequest(t, http.MethodGet, expectedURL, nil, req)
  1530. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1531. })))
  1532. res, err := client.EntryIDs(filter)
  1533. if err != nil {
  1534. t.Fatalf("Expected no error, got %v", err)
  1535. }
  1536. if !reflect.DeepEqual(res, expected) {
  1537. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1538. }
  1539. }
  1540. func TestEntryIDsNoFilter(t *testing.T) {
  1541. expected := &EntryIDsResultSet{
  1542. Total: 2,
  1543. EntryIDs: []int64{1, 2},
  1544. }
  1545. client := NewClientWithOptions(
  1546. "http://mf",
  1547. WithHTTPClient(
  1548. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1549. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids", nil, req)
  1550. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1551. })))
  1552. res, err := client.EntryIDsContext(t.Context(), nil)
  1553. if err != nil {
  1554. t.Fatalf("Expected no error, got %v", err)
  1555. }
  1556. if !reflect.DeepEqual(res, expected) {
  1557. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1558. }
  1559. }
  1560. func TestEntryIDsWithPaginationFilter(t *testing.T) {
  1561. expected := &EntryIDsResultSet{
  1562. Total: 5,
  1563. EntryIDs: []int64{3},
  1564. }
  1565. client := NewClientWithOptions(
  1566. "http://mf",
  1567. WithHTTPClient(
  1568. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1569. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids?limit=1&offset=2", nil, req)
  1570. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1571. })))
  1572. res, err := client.EntryIDsContext(t.Context(), &EntryIDsFilter{Limit: 1, Offset: 2})
  1573. if err != nil {
  1574. t.Fatalf("Expected no error, got %v", err)
  1575. }
  1576. if !reflect.DeepEqual(res, expected) {
  1577. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1578. }
  1579. }
  1580. func TestEntryIDsWithStarredFilter(t *testing.T) {
  1581. expected := &EntryIDsResultSet{
  1582. Total: 1,
  1583. EntryIDs: []int64{42},
  1584. }
  1585. client := NewClientWithOptions(
  1586. "http://mf",
  1587. WithHTTPClient(
  1588. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1589. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids?starred=true", nil, req)
  1590. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1591. })))
  1592. res, err := client.EntryIDsContext(t.Context(), &EntryIDsFilter{Starred: boolPtr(true)})
  1593. if err != nil {
  1594. t.Fatalf("Expected no error, got %v", err)
  1595. }
  1596. if !reflect.DeepEqual(res, expected) {
  1597. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1598. }
  1599. }
  1600. func TestEntryIDsWithStatusFilter(t *testing.T) {
  1601. expected := &EntryIDsResultSet{
  1602. Total: 10,
  1603. EntryIDs: []int64{7, 8},
  1604. }
  1605. client := NewClientWithOptions(
  1606. "http://mf",
  1607. WithHTTPClient(
  1608. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1609. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids?status=unread", nil, req)
  1610. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1611. })))
  1612. res, err := client.EntryIDsContext(t.Context(), &EntryIDsFilter{Status: EntryStatusUnread})
  1613. if err != nil {
  1614. t.Fatalf("Expected no error, got %v", err)
  1615. }
  1616. if !reflect.DeepEqual(res, expected) {
  1617. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1618. }
  1619. }
  1620. func TestEntryIDsWithCombinedFilter(t *testing.T) {
  1621. expected := &EntryIDsResultSet{
  1622. Total: 3,
  1623. EntryIDs: []int64{5},
  1624. }
  1625. client := NewClientWithOptions(
  1626. "http://mf",
  1627. WithHTTPClient(
  1628. newFakeHTTPClient(t, func(t *testing.T, req *http.Request) *http.Response {
  1629. expectRequest(t, http.MethodGet, "http://mf/v1/entries/ids?limit=2&offset=5&starred=false&status=read", nil, req)
  1630. return jsonResponseFrom(t, http.StatusOK, http.Header{}, expected)
  1631. })))
  1632. res, err := client.EntryIDsContext(t.Context(), &EntryIDsFilter{Limit: 2, Offset: 5, Starred: boolPtr(false), Status: EntryStatusRead})
  1633. if err != nil {
  1634. t.Fatalf("Expected no error, got %v", err)
  1635. }
  1636. if !reflect.DeepEqual(res, expected) {
  1637. t.Fatalf("Expected %s, got %s", asJSON(expected), asJSON(res))
  1638. }
  1639. }