You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

170 lines
3.9 KiB

  1. // Copyright 2017 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. "net/http"
  20. "net/http/httptest"
  21. "net/url"
  22. "strconv"
  23. "testing"
  24. crm "google.golang.org/api/cloudresourcemanager/v1"
  25. )
  26. //go:generate -command api go run gen.go docurls.go replacements.go -install -api
  27. //go:generate api cloudresourcemanager:v1
  28. // A handler that mimics paging behavior.
  29. type pageHandler struct {
  30. param bool // is page token in a query param, or body?
  31. err error
  32. }
  33. const nPages = 3
  34. func (h *pageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  35. v, err := url.ParseRequestURI(r.URL.RequestURI())
  36. if err != nil {
  37. h.err = err
  38. return
  39. }
  40. var pageToken string
  41. if h.param {
  42. pts := v.Query()["pageToken"]
  43. if len(pts) > 0 {
  44. pageToken = pts[0]
  45. }
  46. } else {
  47. d := json.NewDecoder(r.Body)
  48. req := struct{ PageToken *string }{&pageToken}
  49. if err := d.Decode(&req); err != nil {
  50. h.err = err
  51. return
  52. }
  53. }
  54. var start int
  55. if pageToken != "" {
  56. start, err = strconv.Atoi(pageToken)
  57. if err != nil {
  58. h.err = err
  59. return
  60. }
  61. }
  62. nextPageToken := ""
  63. if start+1 < nPages {
  64. nextPageToken = strconv.Itoa(start + 1)
  65. }
  66. fmt.Fprintf(w, `{"nextPageToken": %q}`, nextPageToken)
  67. }
  68. func TestPagesParam(t *testing.T) {
  69. handler := &pageHandler{param: true}
  70. server := httptest.NewServer(handler)
  71. defer server.Close()
  72. client := &http.Client{}
  73. s, err := crm.New(client)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. s.BasePath = server.URL
  78. ctx := context.Background()
  79. c := s.Projects.List()
  80. countPages := func() int {
  81. n := 0
  82. err = c.Pages(ctx, func(*crm.ListProjectsResponse) error {
  83. n++
  84. return nil
  85. })
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. return n
  90. }
  91. // Pages traverses through all the pages.
  92. if got, want := countPages(), nPages; got != want {
  93. t.Errorf("got %d pages, want %d", got, want)
  94. }
  95. // Pages starts wherever the current page token is.
  96. c.PageToken("1")
  97. if got, want := countPages(), nPages-1; got != want {
  98. t.Errorf("got %d pages, want %d", got, want)
  99. }
  100. // Pages restores the initial state: we will again visit one fewer
  101. // page, because the initial page token was reset to "1".
  102. if got, want := countPages(), nPages-1; got != want {
  103. t.Errorf("got %d pages, want %d", got, want)
  104. }
  105. if handler.err != nil {
  106. t.Fatal(handler.err)
  107. }
  108. }
  109. func TestPagesRequestField(t *testing.T) {
  110. handler := &pageHandler{param: false}
  111. server := httptest.NewServer(handler)
  112. defer server.Close()
  113. client := &http.Client{}
  114. s, err := crm.New(client)
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. s.BasePath = server.URL
  119. ctx := context.Background()
  120. c := s.Organizations.Search(&crm.SearchOrganizationsRequest{})
  121. countPages := func() int {
  122. n := 0
  123. err = c.Pages(ctx, func(*crm.SearchOrganizationsResponse) error {
  124. n++
  125. return nil
  126. })
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. return n
  131. }
  132. // Pages traverses through all the pages.
  133. if got, want := countPages(), nPages; got != want {
  134. t.Errorf("got %d pages, want %d", got, want)
  135. }
  136. // Pages starts wherever the current page token is.
  137. c = s.Organizations.Search(&crm.SearchOrganizationsRequest{PageToken: "1"})
  138. if got, want := countPages(), nPages-1; got != want {
  139. t.Errorf("got %d pages, want %d", got, want)
  140. }
  141. // Pages restores the initial state: we will again visit one fewer
  142. // page, because the initial page token was reset to "1".
  143. if got, want := countPages(), nPages-1; got != want {
  144. t.Errorf("got %d pages, want %d", got, want)
  145. }
  146. }