integration_client_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlereader_test
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "testing"
  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 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. for key, list := range form {
  54. values[key] = list
  55. }
  56. if client.token != "" {
  57. values.Set("T", client.token)
  58. }
  59. request, err := http.NewRequest(http.MethodPost, client.baseURL+path, strings.NewReader(values.Encode()))
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  64. return send(t, request)
  65. }
  66. // getJSON sends a GET request with output=json and decodes the successful response into result.
  67. func (client *googleReaderClient) getJSON(t *testing.T, path string, query url.Values, result any) {
  68. t.Helper()
  69. values := url.Values{"output": {"json"}}
  70. for key, list := range query {
  71. values[key] = list
  72. }
  73. status, body := client.get(t, path, values)
  74. decodeJSON(t, status, body, result)
  75. }
  76. // postJSON sends a form with output=json and decodes the successful response into result.
  77. func (client *googleReaderClient) postJSON(t *testing.T, path string, form url.Values, result any) {
  78. t.Helper()
  79. values := url.Values{"output": {"json"}}
  80. for key, list := range form {
  81. values[key] = list
  82. }
  83. status, body := client.post(t, path, values)
  84. decodeJSON(t, status, body, result)
  85. }
  86. // postOK sends a form and requires the plain text OK response.
  87. func (client *googleReaderClient) postOK(t *testing.T, path string, form url.Values) {
  88. t.Helper()
  89. status, body := client.post(t, path, form)
  90. if status != http.StatusOK || string(body) != "OK" {
  91. t.Fatalf(`POST %s %v: expected status 200 with body "OK", got %d: %s`, path, form, status, body)
  92. }
  93. }
  94. func send(t *testing.T, request *http.Request) (int, []byte) {
  95. t.Helper()
  96. response, err := http.DefaultClient.Do(request)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. defer response.Body.Close()
  101. body, err := io.ReadAll(response.Body)
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. return response.StatusCode, body
  106. }
  107. func decodeJSON(t *testing.T, status int, body []byte, result any) {
  108. t.Helper()
  109. if status != http.StatusOK {
  110. t.Fatalf(`Expected status 200, got %d: %s`, status, body)
  111. }
  112. if err := json.Unmarshal(body, result); err != nil {
  113. t.Fatalf(`Unable to decode %s: %v`, body, err)
  114. }
  115. }
  116. // requireStatus fails the test when the response status is not the expected one.
  117. func requireStatus(t *testing.T, status int, body []byte, expected int) {
  118. t.Helper()
  119. if status != expected {
  120. t.Fatalf(`Expected status %d, got %d: %s`, expected, status, body)
  121. }
  122. }
  123. // The response types below mirror the JSON documents sent to clients. They are not the
  124. // package's own types, so renaming a JSON field fails the tests as it would break clients.
  125. type loginResult struct {
  126. SID string `json:"SID"`
  127. LSID string `json:"LSID"`
  128. Auth string `json:"Auth"`
  129. }
  130. type userInfoResult struct {
  131. UserID string `json:"userId"`
  132. UserName string `json:"userName"`
  133. UserProfileID string `json:"userProfileId"`
  134. UserEmail string `json:"userEmail"`
  135. }
  136. type tagResult struct {
  137. ID string `json:"id"`
  138. Label string `json:"label"`
  139. Type string `json:"type"`
  140. }
  141. type tagListResult struct {
  142. Tags []tagResult `json:"tags"`
  143. }
  144. type subscriptionResult struct {
  145. ID string `json:"id"`
  146. Title string `json:"title"`
  147. Categories []tagResult `json:"categories"`
  148. URL string `json:"url"`
  149. HTMLURL string `json:"htmlUrl"`
  150. IconURL string `json:"iconUrl"`
  151. }
  152. type subscriptionListResult struct {
  153. Subscriptions []subscriptionResult `json:"subscriptions"`
  154. }
  155. type quickAddResult struct {
  156. NumResults int64 `json:"numResults"`
  157. Query string `json:"query"`
  158. StreamID string `json:"streamId"`
  159. StreamName string `json:"streamName"`
  160. }
  161. type itemIDsResult struct {
  162. ItemRefs []struct {
  163. ID string `json:"id"`
  164. } `json:"itemRefs"`
  165. Continuation string `json:"continuation"`
  166. }
  167. // ids returns the item IDs in the order of the response.
  168. func (result itemIDsResult) ids() []string {
  169. ids := make([]string, 0, len(result.ItemRefs))
  170. for _, itemRef := range result.ItemRefs {
  171. ids = append(ids, itemRef.ID)
  172. }
  173. return ids
  174. }
  175. type hrefResult struct {
  176. HREF string `json:"href"`
  177. Type string `json:"type"`
  178. }
  179. type itemContentResult struct {
  180. Content string `json:"content"`
  181. }
  182. type itemOriginResult struct {
  183. StreamID string `json:"streamId"`
  184. Title string `json:"title"`
  185. HTMLURL string `json:"htmlUrl"`
  186. }
  187. type itemEnclosureResult struct {
  188. URL string `json:"url"`
  189. Type string `json:"type"`
  190. }
  191. type itemResult struct {
  192. ID string `json:"id"`
  193. Title string `json:"title"`
  194. Categories []string `json:"categories"`
  195. Published int64 `json:"published"`
  196. Canonical []hrefResult `json:"canonical"`
  197. Alternate []hrefResult `json:"alternate"`
  198. Content itemContentResult `json:"content"`
  199. Origin itemOriginResult `json:"origin"`
  200. Enclosure []itemEnclosureResult `json:"enclosure"`
  201. }
  202. type itemContentsResult struct {
  203. ID string `json:"id"`
  204. Author string `json:"author"`
  205. Items []itemResult `json:"items"`
  206. }