marker_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package testing
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
  8. "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper"
  9. )
  10. // MarkerPager sample and test cases.
  11. type MarkerPageResult struct {
  12. pagination.MarkerPageBase
  13. }
  14. func (r MarkerPageResult) IsEmpty() (bool, error) {
  15. results, err := ExtractMarkerStrings(r)
  16. if err != nil {
  17. return true, err
  18. }
  19. return len(results) == 0, err
  20. }
  21. func (r MarkerPageResult) LastMarker() (string, error) {
  22. results, err := ExtractMarkerStrings(r)
  23. if err != nil {
  24. return "", err
  25. }
  26. if len(results) == 0 {
  27. return "", nil
  28. }
  29. return results[len(results)-1], nil
  30. }
  31. func createMarkerPaged(t *testing.T) pagination.Pager {
  32. testhelper.SetupHTTP()
  33. testhelper.Mux.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
  34. r.ParseForm()
  35. ms := r.Form["marker"]
  36. switch {
  37. case len(ms) == 0:
  38. fmt.Fprintf(w, "aaa\nbbb\nccc")
  39. case len(ms) == 1 && ms[0] == "ccc":
  40. fmt.Fprintf(w, "ddd\neee\nfff")
  41. case len(ms) == 1 && ms[0] == "fff":
  42. fmt.Fprintf(w, "ggg\nhhh\niii")
  43. case len(ms) == 1 && ms[0] == "iii":
  44. w.WriteHeader(http.StatusNoContent)
  45. default:
  46. t.Errorf("Request with unexpected marker: [%v]", ms)
  47. }
  48. })
  49. client := createClient()
  50. createPage := func(r pagination.PageResult) pagination.Page {
  51. p := MarkerPageResult{pagination.MarkerPageBase{PageResult: r}}
  52. p.MarkerPageBase.Owner = p
  53. return p
  54. }
  55. return pagination.NewPager(client, testhelper.Server.URL+"/page", createPage)
  56. }
  57. func ExtractMarkerStrings(page pagination.Page) ([]string, error) {
  58. content := page.(MarkerPageResult).Body.([]uint8)
  59. parts := strings.Split(string(content), "\n")
  60. results := make([]string, 0, len(parts))
  61. for _, part := range parts {
  62. if len(part) > 0 {
  63. results = append(results, part)
  64. }
  65. }
  66. return results, nil
  67. }
  68. func TestEnumerateMarker(t *testing.T) {
  69. pager := createMarkerPaged(t)
  70. defer testhelper.TeardownHTTP()
  71. callCount := 0
  72. err := pager.EachPage(func(page pagination.Page) (bool, error) {
  73. actual, err := ExtractMarkerStrings(page)
  74. if err != nil {
  75. return false, err
  76. }
  77. t.Logf("Handler invoked with %v", actual)
  78. var expected []string
  79. switch callCount {
  80. case 0:
  81. expected = []string{"aaa", "bbb", "ccc"}
  82. case 1:
  83. expected = []string{"ddd", "eee", "fff"}
  84. case 2:
  85. expected = []string{"ggg", "hhh", "iii"}
  86. default:
  87. t.Fatalf("Unexpected call count: %d", callCount)
  88. return false, nil
  89. }
  90. testhelper.CheckDeepEquals(t, expected, actual)
  91. callCount++
  92. return true, nil
  93. })
  94. testhelper.AssertNoErr(t, err)
  95. testhelper.AssertEquals(t, callCount, 3)
  96. }
  97. func TestAllPagesMarker(t *testing.T) {
  98. pager := createMarkerPaged(t)
  99. defer testhelper.TeardownHTTP()
  100. page, err := pager.AllPages()
  101. testhelper.AssertNoErr(t, err)
  102. expected := []string{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii"}
  103. actual, err := ExtractMarkerStrings(page)
  104. testhelper.AssertNoErr(t, err)
  105. testhelper.CheckDeepEquals(t, expected, actual)
  106. }