integration_client_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlereader_test
  4. import (
  5. "fmt"
  6. "maps"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "testing"
  12. "miniflux.app/v2/internal/http/server/servertest"
  13. )
  14. // The state stream IDs use the "user/-/" form, which clients send for the current user.
  15. const (
  16. readingListStream = "user/-/state/com.google/reading-list"
  17. readStream = "user/-/state/com.google/read"
  18. starredStream = "user/-/state/com.google/starred"
  19. keptUnreadStream = "user/-/state/com.google/kept-unread"
  20. )
  21. func feedStream(feedID int64) string {
  22. return "feed/" + strconv.FormatInt(feedID, 10)
  23. }
  24. // labelStream returns the stream ID of a label in the "user/-/" form, which clients send for the current user.
  25. func labelStream(title string) string {
  26. return "user/-/label/" + title
  27. }
  28. // longItemID returns the item ID in the long form used in stream/items/contents responses.
  29. func longItemID(entryID int64) string {
  30. return fmt.Sprintf("tag:google.com,2005:reader/item/%016x", entryID)
  31. }
  32. // googleReaderClient calls the Google Reader API. It sends no credentials when token is empty.
  33. type googleReaderClient struct {
  34. baseURL string
  35. token string
  36. }
  37. // get sends a GET request with the token in the Authorization header.
  38. func (client *googleReaderClient) get(t *testing.T, path string, query url.Values) (int, []byte) {
  39. t.Helper()
  40. request, err := http.NewRequest(http.MethodGet, client.baseURL+path+"?"+query.Encode(), nil)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if client.token != "" {
  45. request.Header.Set("Authorization", "GoogleLogin auth="+client.token)
  46. }
  47. return servertest.Send(t, request)
  48. }
  49. // post sends a form with the token in the T field.
  50. func (client *googleReaderClient) post(t *testing.T, path string, form url.Values) (int, []byte) {
  51. t.Helper()
  52. values := url.Values{}
  53. maps.Copy(values, form)
  54. if client.token != "" {
  55. values.Set("T", client.token)
  56. }
  57. request, err := http.NewRequest(http.MethodPost, client.baseURL+path, strings.NewReader(values.Encode()))
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  62. return servertest.Send(t, request)
  63. }
  64. // getJSON sends a GET request with output=json and decodes the successful response into result.
  65. func (client *googleReaderClient) getJSON(t *testing.T, path string, query url.Values, result any) {
  66. t.Helper()
  67. values := url.Values{"output": {"json"}}
  68. maps.Copy(values, query)
  69. status, body := client.get(t, path, values)
  70. servertest.DecodeJSON(t, status, body, result)
  71. }
  72. // postJSON sends a form with output=json and decodes the successful response into result.
  73. func (client *googleReaderClient) postJSON(t *testing.T, path string, form url.Values, result any) {
  74. t.Helper()
  75. values := url.Values{"output": {"json"}}
  76. maps.Copy(values, form)
  77. status, body := client.post(t, path, values)
  78. servertest.DecodeJSON(t, status, body, result)
  79. }
  80. // postOK sends a form and requires the plain text OK response.
  81. func (client *googleReaderClient) postOK(t *testing.T, path string, form url.Values) {
  82. t.Helper()
  83. status, body := client.post(t, path, form)
  84. if status != http.StatusOK || string(body) != "OK" {
  85. t.Fatalf(`POST %s %v: expected status 200 with body "OK", got %d: %s`, path, form, status, body)
  86. }
  87. }
  88. // requireStatus fails the test when the response status is not the expected one.
  89. func requireStatus(t *testing.T, status int, body []byte, expected int) {
  90. t.Helper()
  91. if status != expected {
  92. t.Fatalf(`Expected status %d, got %d: %s`, expected, status, body)
  93. }
  94. }
  95. // The response types below mirror the JSON documents sent to clients. They are not the
  96. // package's own types, so renaming a JSON field fails the tests as it would break clients.
  97. type loginResult struct {
  98. SID string `json:"SID"`
  99. LSID string `json:"LSID"`
  100. Auth string `json:"Auth"`
  101. }
  102. type userInfoResult struct {
  103. UserID string `json:"userId"`
  104. UserName string `json:"userName"`
  105. UserProfileID string `json:"userProfileId"`
  106. UserEmail string `json:"userEmail"`
  107. }
  108. type tagResult struct {
  109. ID string `json:"id"`
  110. Label string `json:"label"`
  111. Type string `json:"type"`
  112. }
  113. type tagListResult struct {
  114. Tags []tagResult `json:"tags"`
  115. }
  116. type subscriptionResult struct {
  117. ID string `json:"id"`
  118. Title string `json:"title"`
  119. Categories []tagResult `json:"categories"`
  120. URL string `json:"url"`
  121. HTMLURL string `json:"htmlUrl"`
  122. IconURL string `json:"iconUrl"`
  123. }
  124. type subscriptionListResult struct {
  125. Subscriptions []subscriptionResult `json:"subscriptions"`
  126. }
  127. type quickAddResult struct {
  128. NumResults int64 `json:"numResults"`
  129. Query string `json:"query"`
  130. StreamID string `json:"streamId"`
  131. StreamName string `json:"streamName"`
  132. }
  133. type itemIDsResult struct {
  134. ItemRefs []struct {
  135. ID string `json:"id"`
  136. } `json:"itemRefs"`
  137. Continuation string `json:"continuation"`
  138. }
  139. // ids returns the item IDs in the order of the response.
  140. func (result itemIDsResult) ids() []string {
  141. ids := make([]string, 0, len(result.ItemRefs))
  142. for _, itemRef := range result.ItemRefs {
  143. ids = append(ids, itemRef.ID)
  144. }
  145. return ids
  146. }
  147. type hrefResult struct {
  148. HREF string `json:"href"`
  149. Type string `json:"type"`
  150. }
  151. type itemContentResult struct {
  152. Content string `json:"content"`
  153. }
  154. type itemOriginResult struct {
  155. StreamID string `json:"streamId"`
  156. Title string `json:"title"`
  157. HTMLURL string `json:"htmlUrl"`
  158. }
  159. type itemEnclosureResult struct {
  160. URL string `json:"url"`
  161. Type string `json:"type"`
  162. }
  163. type itemResult struct {
  164. ID string `json:"id"`
  165. Title string `json:"title"`
  166. Categories []string `json:"categories"`
  167. Published int64 `json:"published"`
  168. Canonical []hrefResult `json:"canonical"`
  169. Alternate []hrefResult `json:"alternate"`
  170. Content itemContentResult `json:"content"`
  171. Origin itemOriginResult `json:"origin"`
  172. Enclosure []itemEnclosureResult `json:"enclosure"`
  173. }
  174. type itemContentsResult struct {
  175. ID string `json:"id"`
  176. Author string `json:"author"`
  177. Items []itemResult `json:"items"`
  178. }