(t *testing.T)
| 85 | } |
| 86 | |
| 87 | func TestListSubscriptions(t *testing.T) { |
| 88 | page := func(link string, ids ...string) httpmock.Responder { |
| 89 | subs := make([]map[string]string, 0, len(ids)) |
| 90 | for _, id := range ids { |
| 91 | subs = append(subs, map[string]string{"id": id}) |
| 92 | } |
| 93 | responder := httpmock.JSONResponse(map[string]any{"subscriptions": subs}) |
| 94 | if link == "" { |
| 95 | return responder |
| 96 | } |
| 97 | return httpmock.WithHeader(responder, "Link", link) |
| 98 | } |
| 99 | pageMatcher := func(perPage, page string) httpmock.Matcher { |
| 100 | query := url.Values{"per_page": {perPage}} |
| 101 | if page != "" { |
| 102 | query.Set("page", page) |
| 103 | } |
| 104 | rest := httpmock.REST("GET", "event-subscriptions") |
| 105 | paged := httpmock.QueryMatcher("GET", "event-subscriptions", query) |
| 106 | return func(req *http.Request) bool { |
| 107 | return rest(req) && paged(req) && req.URL.Query().Has("page") == (page != "") |
| 108 | } |
| 109 | } |
| 110 | nextLink := func(page string) string { |
| 111 | return `<https://api.github.com/event-subscriptions?page=` + page + `&per_page=3>; rel="next", ` + |
| 112 | `<https://api.github.com/event-subscriptions?page=9&per_page=3>; rel="last"` |
| 113 | } |
| 114 | |
| 115 | tests := []struct { |
| 116 | name string |
| 117 | limit int |
| 118 | stubs func(*httpmock.Registry) |
| 119 | wantIDs []string |
| 120 | }{ |
| 121 | { |
| 122 | name: "follows next links and truncates to the limit", |
| 123 | limit: 3, |
| 124 | stubs: func(reg *httpmock.Registry) { |
| 125 | reg.Register(pageMatcher("3", ""), page(nextLink("2"), "sub_1", "sub_2")) |
| 126 | reg.Register(pageMatcher("3", "2"), page(nextLink("3"), "sub_3", "sub_4")) |
| 127 | }, |
| 128 | wantIDs: []string{"sub_1", "sub_2", "sub_3"}, |
| 129 | }, |
| 130 | { |
| 131 | name: "stops at the last page", |
| 132 | limit: 30, |
| 133 | stubs: func(reg *httpmock.Registry) { |
| 134 | reg.Register(pageMatcher("30", ""), page("", "sub_1")) |
| 135 | }, |
| 136 | wantIDs: []string{"sub_1"}, |
| 137 | }, |
| 138 | { |
| 139 | name: "stops at an empty page despite a next link", |
| 140 | limit: 30, |
| 141 | stubs: func(reg *httpmock.Registry) { |
| 142 | reg.Register(pageMatcher("30", ""), page(nextLink("2"))) |
| 143 | }, |
| 144 | wantIDs: []string{}, |
nothing calls this directly
no test coverage detected