@@ -0,0 +1,256 @@ |
| | 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| | 2 | + |
| | 3 | +package apipage_test |
| | 4 | + |
| | 5 | +import ( |
| | 6 | + "net/http/httptest" |
| | 7 | + "net/url" |
| | 8 | + "strings" |
| | 9 | + "testing" |
| | 10 | + |
| | 11 | + "github.com/tenseleyFlow/shithub/internal/web/handlers/api/apipage" |
| | 12 | +) |
| | 13 | + |
| | 14 | +func TestParseQuery_Defaults(t *testing.T) { |
| | 15 | + t.Parallel() |
| | 16 | + r := httptest.NewRequest("GET", "/api/v1/things", nil) |
| | 17 | + page, perPage := apipage.ParseQuery(r, 0, 0) |
| | 18 | + if page != 1 || perPage != apipage.DefaultPerPage { |
| | 19 | + t.Fatalf("got page=%d, perPage=%d; want 1/%d", page, perPage, apipage.DefaultPerPage) |
| | 20 | + } |
| | 21 | +} |
| | 22 | + |
| | 23 | +func TestParseQuery_ClampsPerPage(t *testing.T) { |
| | 24 | + t.Parallel() |
| | 25 | + r := httptest.NewRequest("GET", "/api/v1/things?page=2&per_page=500", nil) |
| | 26 | + page, perPage := apipage.ParseQuery(r, 30, 100) |
| | 27 | + if page != 2 { |
| | 28 | + t.Errorf("page: got %d, want 2", page) |
| | 29 | + } |
| | 30 | + if perPage != 100 { |
| | 31 | + t.Errorf("per_page: got %d, want 100", perPage) |
| | 32 | + } |
| | 33 | +} |
| | 34 | + |
| | 35 | +func TestParseQuery_NegativeFallback(t *testing.T) { |
| | 36 | + t.Parallel() |
| | 37 | + r := httptest.NewRequest("GET", "/api/v1/things?page=-3&per_page=-1", nil) |
| | 38 | + page, perPage := apipage.ParseQuery(r, 0, 0) |
| | 39 | + if page != 1 || perPage != apipage.DefaultPerPage { |
| | 40 | + t.Fatalf("got page=%d, perPage=%d; want 1/%d", page, perPage, apipage.DefaultPerPage) |
| | 41 | + } |
| | 42 | +} |
| | 43 | + |
| | 44 | +func TestParseQuery_NonInteger(t *testing.T) { |
| | 45 | + t.Parallel() |
| | 46 | + r := httptest.NewRequest("GET", "/api/v1/things?page=banana&per_page=cherry", nil) |
| | 47 | + page, perPage := apipage.ParseQuery(r, 25, 50) |
| | 48 | + if page != 1 || perPage != 25 { |
| | 49 | + t.Fatalf("got page=%d, perPage=%d; want 1/25", page, perPage) |
| | 50 | + } |
| | 51 | +} |
| | 52 | + |
| | 53 | +func TestLinkHeader_MiddlePage(t *testing.T) { |
| | 54 | + t.Parallel() |
| | 55 | + u, _ := url.Parse("/api/v1/user/starred?per_page=30&page=2") |
| | 56 | + p := apipage.Page{Current: 2, PerPage: 30, Total: 120} |
| | 57 | + h := p.LinkHeader("https://shithub.sh", u) |
| | 58 | + |
| | 59 | + links := parseLink(h) |
| | 60 | + wantRels := []string{"first", "prev", "next", "last"} |
| | 61 | + for _, rel := range wantRels { |
| | 62 | + if _, ok := links[rel]; !ok { |
| | 63 | + t.Errorf("missing rel=%q in %s", rel, h) |
| | 64 | + } |
| | 65 | + } |
| | 66 | + if got := pageFor(links["next"]); got != "3" { |
| | 67 | + t.Errorf("next page: got %s, want 3", got) |
| | 68 | + } |
| | 69 | + if got := pageFor(links["prev"]); got != "1" { |
| | 70 | + t.Errorf("prev page: got %s, want 1", got) |
| | 71 | + } |
| | 72 | + if got := pageFor(links["last"]); got != "4" { |
| | 73 | + t.Errorf("last page: got %s, want 4", got) |
| | 74 | + } |
| | 75 | + if got := pageFor(links["first"]); got != "1" { |
| | 76 | + t.Errorf("first page: got %s, want 1", got) |
| | 77 | + } |
| | 78 | + if !strings.HasPrefix(links["next"], "https://shithub.sh/api/v1/user/starred?") { |
| | 79 | + t.Errorf("next link not absolute: %s", links["next"]) |
| | 80 | + } |
| | 81 | +} |
| | 82 | + |
| | 83 | +func TestLinkHeader_FirstPage(t *testing.T) { |
| | 84 | + t.Parallel() |
| | 85 | + u, _ := url.Parse("/api/v1/repos?per_page=10&page=1") |
| | 86 | + p := apipage.Page{Current: 1, PerPage: 10, Total: 25} |
| | 87 | + links := parseLink(p.LinkHeader("https://shithub.sh", u)) |
| | 88 | + if _, ok := links["prev"]; ok { |
| | 89 | + t.Errorf("prev should be absent on first page; got %v", links) |
| | 90 | + } |
| | 91 | + if _, ok := links["next"]; !ok { |
| | 92 | + t.Error("next should be present on first page") |
| | 93 | + } |
| | 94 | + if got := pageFor(links["last"]); got != "3" { |
| | 95 | + t.Errorf("last page: got %s, want 3", got) |
| | 96 | + } |
| | 97 | +} |
| | 98 | + |
| | 99 | +func TestLinkHeader_LastPage(t *testing.T) { |
| | 100 | + t.Parallel() |
| | 101 | + u, _ := url.Parse("/api/v1/repos?per_page=10&page=3") |
| | 102 | + p := apipage.Page{Current: 3, PerPage: 10, Total: 25} |
| | 103 | + links := parseLink(p.LinkHeader("https://shithub.sh", u)) |
| | 104 | + if _, ok := links["next"]; ok { |
| | 105 | + t.Errorf("next should be absent on last page; got %v", links) |
| | 106 | + } |
| | 107 | + if _, ok := links["prev"]; !ok { |
| | 108 | + t.Error("prev should be present on last page") |
| | 109 | + } |
| | 110 | +} |
| | 111 | + |
| | 112 | +func TestLinkHeader_SinglePage(t *testing.T) { |
| | 113 | + t.Parallel() |
| | 114 | + u, _ := url.Parse("/api/v1/repos?per_page=30&page=1") |
| | 115 | + p := apipage.Page{Current: 1, PerPage: 30, Total: 5} |
| | 116 | + if got := p.LinkHeader("https://shithub.sh", u); got != "" { |
| | 117 | + t.Errorf("expected empty header for single-page result; got %q", got) |
| | 118 | + } |
| | 119 | +} |
| | 120 | + |
| | 121 | +func TestLinkHeader_StreamForm(t *testing.T) { |
| | 122 | + t.Parallel() |
| | 123 | + u, _ := url.Parse("/api/v1/feed?page=2") |
| | 124 | + p := apipage.Page{Current: 2, PerPage: 30, Total: -1, HasMore: true} |
| | 125 | + links := parseLink(p.LinkHeader("https://shithub.sh", u)) |
| | 126 | + if _, ok := links["first"]; ok { |
| | 127 | + t.Error("first should not appear when total unknown") |
| | 128 | + } |
| | 129 | + if _, ok := links["last"]; ok { |
| | 130 | + t.Error("last should not appear when total unknown") |
| | 131 | + } |
| | 132 | + if _, ok := links["next"]; !ok { |
| | 133 | + t.Error("next should appear when HasMore=true") |
| | 134 | + } |
| | 135 | + if _, ok := links["prev"]; !ok { |
| | 136 | + t.Error("prev should appear when Current > 1") |
| | 137 | + } |
| | 138 | +} |
| | 139 | + |
| | 140 | +func TestLinkHeader_StreamFormExhausted(t *testing.T) { |
| | 141 | + t.Parallel() |
| | 142 | + u, _ := url.Parse("/api/v1/feed?page=4") |
| | 143 | + p := apipage.Page{Current: 4, PerPage: 30, Total: -1, HasMore: false} |
| | 144 | + links := parseLink(p.LinkHeader("https://shithub.sh", u)) |
| | 145 | + if _, ok := links["next"]; ok { |
| | 146 | + t.Error("next should be absent when HasMore=false") |
| | 147 | + } |
| | 148 | + if _, ok := links["prev"]; !ok { |
| | 149 | + t.Error("prev should still appear when Current > 1") |
| | 150 | + } |
| | 151 | +} |
| | 152 | + |
| | 153 | +func TestLinkHeader_PreservesOtherQueryParams(t *testing.T) { |
| | 154 | + t.Parallel() |
| | 155 | + u, _ := url.Parse("/api/v1/issues?state=open&labels=bug,ux&per_page=30&page=2") |
| | 156 | + p := apipage.Page{Current: 2, PerPage: 30, Total: 120} |
| | 157 | + links := parseLink(p.LinkHeader("https://shithub.sh", u)) |
| | 158 | + for rel, link := range links { |
| | 159 | + if !strings.Contains(link, "state=open") { |
| | 160 | + t.Errorf("rel=%s lost state=open: %s", rel, link) |
| | 161 | + } |
| | 162 | + if !strings.Contains(link, "labels=") { |
| | 163 | + t.Errorf("rel=%s lost labels: %s", rel, link) |
| | 164 | + } |
| | 165 | + } |
| | 166 | +} |
| | 167 | + |
| | 168 | +func TestLinkHeader_RelativeWhenBaseURLEmpty(t *testing.T) { |
| | 169 | + t.Parallel() |
| | 170 | + u, _ := url.Parse("/api/v1/repos?page=1&per_page=10") |
| | 171 | + p := apipage.Page{Current: 1, PerPage: 10, Total: 25} |
| | 172 | + h := p.LinkHeader("", u) |
| | 173 | + if !strings.HasPrefix(h, "</api/v1/repos?") { |
| | 174 | + t.Errorf("expected path-relative link; got %s", h) |
| | 175 | + } |
| | 176 | +} |
| | 177 | + |
| | 178 | +// parseLink reimplements the gh-compatible parser used by |
| | 179 | +// shithub-cli/internal/api.ParseLinkHeader. Kept in-test so apipage has |
| | 180 | +// no cross-module dependency, but we exercise the same algorithm here. |
| | 181 | +func parseLink(header string) map[string]string { |
| | 182 | + out := map[string]string{} |
| | 183 | + if header == "" { |
| | 184 | + return out |
| | 185 | + } |
| | 186 | + for _, entry := range splitLinkEntries(header) { |
| | 187 | + u, rel, ok := parseLinkEntry(entry) |
| | 188 | + if !ok { |
| | 189 | + continue |
| | 190 | + } |
| | 191 | + out[rel] = u |
| | 192 | + } |
| | 193 | + return out |
| | 194 | +} |
| | 195 | + |
| | 196 | +func splitLinkEntries(header string) []string { |
| | 197 | + var ( |
| | 198 | + entries []string |
| | 199 | + buf strings.Builder |
| | 200 | + depth int |
| | 201 | + ) |
| | 202 | + for _, r := range header { |
| | 203 | + switch r { |
| | 204 | + case '<': |
| | 205 | + depth++ |
| | 206 | + case '>': |
| | 207 | + if depth > 0 { |
| | 208 | + depth-- |
| | 209 | + } |
| | 210 | + case ',': |
| | 211 | + if depth == 0 { |
| | 212 | + entries = append(entries, strings.TrimSpace(buf.String())) |
| | 213 | + buf.Reset() |
| | 214 | + continue |
| | 215 | + } |
| | 216 | + } |
| | 217 | + buf.WriteRune(r) |
| | 218 | + } |
| | 219 | + if buf.Len() > 0 { |
| | 220 | + entries = append(entries, strings.TrimSpace(buf.String())) |
| | 221 | + } |
| | 222 | + return entries |
| | 223 | +} |
| | 224 | + |
| | 225 | +func parseLinkEntry(entry string) (linkURL, rel string, ok bool) { |
| | 226 | + lt := strings.Index(entry, "<") |
| | 227 | + gt := strings.Index(entry, ">") |
| | 228 | + if lt < 0 || gt < 0 || gt < lt { |
| | 229 | + return "", "", false |
| | 230 | + } |
| | 231 | + linkURL = entry[lt+1 : gt] |
| | 232 | + rest := entry[gt+1:] |
| | 233 | + for _, attr := range strings.Split(rest, ";") { |
| | 234 | + attr = strings.TrimSpace(attr) |
| | 235 | + if !strings.HasPrefix(attr, "rel=") { |
| | 236 | + continue |
| | 237 | + } |
| | 238 | + rel = strings.Trim(attr[len("rel="):], `"`) |
| | 239 | + } |
| | 240 | + if rel == "" { |
| | 241 | + return "", "", false |
| | 242 | + } |
| | 243 | + return linkURL, rel, true |
| | 244 | +} |
| | 245 | + |
| | 246 | +func pageFor(link string) string { |
| | 247 | + idx := strings.Index(link, "?") |
| | 248 | + if idx < 0 { |
| | 249 | + return "" |
| | 250 | + } |
| | 251 | + q, err := url.ParseQuery(link[idx+1:]) |
| | 252 | + if err != nil { |
| | 253 | + return "" |
| | 254 | + } |
| | 255 | + return q.Get("page") |
| | 256 | +} |