Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 4 | "bytes" |
| 5 | "errors" |
| 6 | "io" |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 7 | "net/http" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 11 | "github.com/mitchellh/mapstructure" |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 12 | ) |
| 13 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 14 | type nopCloser struct { |
| 15 | io.Reader |
| 16 | } |
| 17 | |
| 18 | func (nopCloser) Close() error { return nil } |
| 19 | |
| 20 | func responseWithBody(body string) (http.Response, error) { |
| 21 | return http.Response{ |
| 22 | Body: nopCloser{bytes.NewReader([]byte(body))}, |
| 23 | }, nil |
| 24 | } |
| 25 | |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 26 | // SinglePage sample and test cases. |
| 27 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 28 | type SingleIntList struct { |
| 29 | SinglePage |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 32 | func ExtractSingleInts(page Page) ([]int, error) { |
| 33 | var response struct { |
| 34 | Ints []int `mapstructure:"ints"` |
| 35 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 36 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 37 | err := mapstructure.Decode(page.(SingleIntList).Body, &response) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 41 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 42 | return response.Ints, nil |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 43 | } |
| 44 | |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 45 | func setupSinglePaged() Pager { |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 46 | return NewSinglePager(func() (http.Response, error) { |
| 47 | return responseWithBody(`{ "ints": [1, 2, 3] }`) |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 48 | }) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | func TestEnumerateSinglePaged(t *testing.T) { |
| 52 | callCount := 0 |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 53 | err := setupSinglePaged().EachPage(func(page Page) bool { |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 54 | callCount++ |
| 55 | |
| 56 | expected := []int{1, 2, 3} |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 57 | actual, err := ExtractSingleInts(page) |
| 58 | if err != nil { |
| 59 | t.Fatalf("Unexpected error extracting ints: %v", err) |
| 60 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 61 | if !reflect.DeepEqual(expected, actual) { |
| 62 | t.Errorf("Expected %v, but was %v", expected, actual) |
| 63 | } |
| 64 | return true |
| 65 | }) |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 66 | if err != nil { |
| 67 | t.Fatalf("Unexpected error calling EachPage: %v", err) |
| 68 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 69 | |
| 70 | if callCount != 1 { |
| 71 | t.Errorf("Callback was invoked %d times", callCount) |
| 72 | } |
| 73 | } |
| 74 | |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 75 | // LinkedPager sample and test cases. |
| 76 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 77 | type LinkedIntPage struct { |
| 78 | PaginatedLinksPage |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 81 | func ExtractLinkedInts(page Page) ([]int, error) { |
| 82 | var response struct { |
| 83 | Ints []int `mapstructure:"ints"` |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 84 | } |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 85 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 86 | err := mapstructure.Decode(page.(LinkedIntPage).Body, &response) |
| 87 | if err != nil { |
| 88 | return nil, err |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 89 | } |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 90 | |
| 91 | return response.Ints, nil |
Ash Wilson | b110fc9 | 2014-09-08 13:54:59 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 94 | func createLinked(t *testing.T) Pager { |
| 95 | return NewLinkedPager("page1", func(url string) (http.Response, error) { |
| 96 | switch url { |
| 97 | case "page1": |
| 98 | return responseWithBody(`{ "ints": [1, 2, 3], "links": { "next": "page2" } }`) |
| 99 | case "page2": |
| 100 | return responseWithBody(`{ "ints": [4, 5, 6], "links": { "next": "page3" } }`) |
| 101 | case "page3": |
| 102 | return responseWithBody(`{ "ints": [7, 8, 9], "links": { "next": null } }`) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 103 | default: |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 104 | t.Fatalf("LinkedPager called with unexpected URL: %v", url) |
| 105 | return http.Response{}, errors.New("Wat") |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 106 | } |
| 107 | }) |
| 108 | } |
| 109 | |
| 110 | func TestEnumerateLinked(t *testing.T) { |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 111 | pager := createLinked(t) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 112 | |
| 113 | callCount := 0 |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame^] | 114 | err := pager.EachPage(func(page Page) bool { |
| 115 | actual, err := ExtractLinkedInts(page) |
| 116 | if err != nil { |
| 117 | t.Errorf("Unable to extract ints from page: %v", err) |
| 118 | return false |
| 119 | } |
| 120 | |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 121 | t.Logf("Handler invoked with %v", actual) |
| 122 | |
| 123 | var expected []int |
| 124 | switch callCount { |
| 125 | case 0: |
| 126 | expected = []int{1, 2, 3} |
| 127 | case 1: |
| 128 | expected = []int{4, 5, 6} |
| 129 | case 2: |
| 130 | expected = []int{7, 8, 9} |
| 131 | default: |
| 132 | t.Fatalf("Unexpected call count: %d", callCount) |
| 133 | return false |
| 134 | } |
| 135 | |
| 136 | if !reflect.DeepEqual(expected, actual) { |
| 137 | t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual) |
| 138 | } |
| 139 | |
| 140 | callCount++ |
| 141 | return true |
| 142 | }) |
| 143 | if err != nil { |
| 144 | t.Errorf("Unexpected error for page iteration: %v", err) |
| 145 | } |
| 146 | |
| 147 | if callCount != 3 { |
| 148 | t.Errorf("Expected 3 calls, but was %d", callCount) |
| 149 | } |
| 150 | } |