integration_client_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. "io"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "testing"
  12. )
  13. const starredStream = "user/-/state/com.google/starred"
  14. func feedStream(feedID int64) string {
  15. return "feed/" + strconv.FormatInt(feedID, 10)
  16. }
  17. // labelStream returns the stream ID of a label in the "user/-/" form, which clients send for the current user.
  18. func labelStream(title string) string {
  19. return "user/-/label/" + title
  20. }
  21. // googleReaderClient calls the Google Reader API. It sends no credentials when token is empty.
  22. type googleReaderClient struct {
  23. baseURL string
  24. token string
  25. }
  26. // get sends a GET request with the token in the Authorization header.
  27. func (client *googleReaderClient) get(t *testing.T, path string, query url.Values) (int, []byte) {
  28. t.Helper()
  29. request, err := http.NewRequest(http.MethodGet, client.baseURL+path+"?"+query.Encode(), nil)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. if client.token != "" {
  34. request.Header.Set("Authorization", "GoogleLogin auth="+client.token)
  35. }
  36. return send(t, request)
  37. }
  38. // post sends a form with the token in the T field.
  39. func (client *googleReaderClient) post(t *testing.T, path string, form url.Values) (int, []byte) {
  40. t.Helper()
  41. values := url.Values{}
  42. for key, list := range form {
  43. values[key] = list
  44. }
  45. if client.token != "" {
  46. values.Set("T", client.token)
  47. }
  48. request, err := http.NewRequest(http.MethodPost, client.baseURL+path, strings.NewReader(values.Encode()))
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  53. return send(t, request)
  54. }
  55. // getJSON sends a GET request with output=json and decodes the successful response into result.
  56. func (client *googleReaderClient) getJSON(t *testing.T, path string, query url.Values, result any) {
  57. t.Helper()
  58. values := url.Values{"output": {"json"}}
  59. for key, list := range query {
  60. values[key] = list
  61. }
  62. status, body := client.get(t, path, values)
  63. decodeJSON(t, status, body, result)
  64. }
  65. // postJSON sends a form with output=json and decodes the successful response into result.
  66. func (client *googleReaderClient) postJSON(t *testing.T, path string, form url.Values, result any) {
  67. t.Helper()
  68. values := url.Values{"output": {"json"}}
  69. for key, list := range form {
  70. values[key] = list
  71. }
  72. status, body := client.post(t, path, values)
  73. decodeJSON(t, status, body, result)
  74. }
  75. // postOK sends a form and requires the plain text OK response.
  76. func (client *googleReaderClient) postOK(t *testing.T, path string, form url.Values) {
  77. t.Helper()
  78. status, body := client.post(t, path, form)
  79. if status != http.StatusOK || string(body) != "OK" {
  80. t.Fatalf(`POST %s %v: expected status 200 with body "OK", got %d: %s`, path, form, status, body)
  81. }
  82. }
  83. func send(t *testing.T, request *http.Request) (int, []byte) {
  84. t.Helper()
  85. response, err := http.DefaultClient.Do(request)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. defer response.Body.Close()
  90. body, err := io.ReadAll(response.Body)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. return response.StatusCode, body
  95. }
  96. func decodeJSON(t *testing.T, status int, body []byte, result any) {
  97. t.Helper()
  98. if status != http.StatusOK {
  99. t.Fatalf(`Expected status 200, got %d: %s`, status, body)
  100. }
  101. if err := json.Unmarshal(body, result); err != nil {
  102. t.Fatalf(`Unable to decode %s: %v`, body, err)
  103. }
  104. }
  105. // requireStatus fails the test when the response status is not the expected one.
  106. func requireStatus(t *testing.T, status int, body []byte, expected int) {
  107. t.Helper()
  108. if status != expected {
  109. t.Fatalf(`Expected status %d, got %d: %s`, expected, status, body)
  110. }
  111. }
  112. // The response types below mirror the JSON documents sent to clients. They are not the
  113. // package's own types, so renaming a JSON field fails the tests as it would break clients.
  114. type loginResult struct {
  115. SID string `json:"SID"`
  116. LSID string `json:"LSID"`
  117. Auth string `json:"Auth"`
  118. }
  119. type userInfoResult struct {
  120. UserID string `json:"userId"`
  121. UserName string `json:"userName"`
  122. UserProfileID string `json:"userProfileId"`
  123. UserEmail string `json:"userEmail"`
  124. }
  125. type tagResult struct {
  126. ID string `json:"id"`
  127. Label string `json:"label"`
  128. Type string `json:"type"`
  129. }
  130. type tagListResult struct {
  131. Tags []tagResult `json:"tags"`
  132. }
  133. type subscriptionResult struct {
  134. ID string `json:"id"`
  135. Title string `json:"title"`
  136. Categories []tagResult `json:"categories"`
  137. URL string `json:"url"`
  138. HTMLURL string `json:"htmlUrl"`
  139. IconURL string `json:"iconUrl"`
  140. }
  141. type subscriptionListResult struct {
  142. Subscriptions []subscriptionResult `json:"subscriptions"`
  143. }
  144. type quickAddResult struct {
  145. NumResults int64 `json:"numResults"`
  146. Query string `json:"query"`
  147. StreamID string `json:"streamId"`
  148. StreamName string `json:"streamName"`
  149. }