package main import ( "net/http/httptest" "testing" ) func TestParsePageRequest(t *testing.T) { tests := []struct { name string query string wantLimit int wantCursor *pageCursor wantErr bool }{ {name: "defaults", wantLimit: defaultPageSize}, {name: "custom limit", query: "limit=17", wantLimit: 17}, {name: "maximum limit", query: "limit=500", wantLimit: 500}, {name: "zero limit", query: "limit=0", wantErr: true}, {name: "oversized limit", query: "limit=501", wantErr: true}, {name: "invalid limit", query: "limit=large", wantErr: true}, { name: "cursor", query: "cursor=" + encodePageCursor(pageCursor{Kind: "lists", Key: 123, ID: "list-1"}), wantLimit: defaultPageSize, wantCursor: &pageCursor{Kind: "lists", Key: 123, ID: "list-1"}, }, {name: "malformed cursor", query: "cursor=not-a-cursor", wantErr: true}, { name: "cursor for another collection", query: "cursor=" + encodePageCursor(pageCursor{Kind: "items", Key: 123, ID: "item-1"}), wantErr: true, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { request := httptest.NewRequest("GET", "/api/lists?"+test.query, nil) got, err := parsePageRequest(request, "lists") if (err != nil) != test.wantErr { t.Fatalf("parsePageRequest() error = %v, wantErr %v", err, test.wantErr) } if err != nil { return } if got.Limit != test.wantLimit { t.Fatalf("limit = %d, want %d", got.Limit, test.wantLimit) } if test.wantCursor == nil { if got.Cursor != nil { t.Fatalf("cursor = %+v, want none", got.Cursor) } return } if got.Cursor == nil || *got.Cursor != *test.wantCursor { t.Fatalf("cursor = %+v, want %+v", got.Cursor, test.wantCursor) } }) } }