Migration
This is my new public git source viewer. Source coming soon. Please contact with concerns/bugs.
Contact me
internal/site/request_test.go
79436f09f5539aac9b6f836b5b9049bbbece48aa
· 1.8 KB · 71 lines
raw
| 1 | package site |
| 2 | |
| 3 | import ( |
| 4 | "net/http/httptest" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestCompareRequest(t *testing.T) { |
| 9 | cases := []struct { |
| 10 | name string |
| 11 | rangeVal string |
| 12 | wantRef string |
| 13 | wantBase string |
| 14 | threeDot bool |
| 15 | wantErr bool |
| 16 | }{ |
| 17 | {name: "two dot", rangeVal: "main..feature", wantRef: "feature", wantBase: "main"}, |
| 18 | {name: "three dot", rangeVal: "main...feature", wantRef: "feature", wantBase: "main", threeDot: true}, |
| 19 | {name: "refs with dots", rangeVal: "v1.0..v2.0", wantRef: "v2.0", wantBase: "v1.0"}, |
| 20 | {name: "missing separator", rangeVal: "main", wantErr: true}, |
| 21 | } |
| 22 | |
| 23 | for _, tc := range cases { |
| 24 | t.Run(tc.name, func(t *testing.T) { |
| 25 | req := httptest.NewRequest("GET", "/demo/compare/"+tc.rangeVal, nil) |
| 26 | req.SetPathValue("range", tc.rangeVal) |
| 27 | got, err := CompareRequest(req) |
| 28 | if tc.wantErr { |
| 29 | if err == nil { |
| 30 | t.Fatal("expected an error") |
| 31 | } |
| 32 | return |
| 33 | } |
| 34 | if err != nil { |
| 35 | t.Fatalf("CompareRequest: %v", err) |
| 36 | } |
| 37 | if got.Ref != tc.wantRef || got.Range == nil || got.Range.Base != tc.wantBase || got.Range.ThreeDot != tc.threeDot { |
| 38 | t.Fatalf("CompareRequest = %+v, want ref=%q base=%q threeDot=%v", got, tc.wantRef, tc.wantBase, tc.threeDot) |
| 39 | } |
| 40 | }) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // The compare route must use the parser; the paginated routes must declare |
| 45 | // their page size. Otherwise buildCtx silently falls back to defaults. |
| 46 | func TestRoutesDeclareParsingMetadata(t *testing.T) { |
| 47 | routes := Routes() |
| 48 | byName := make(map[string]struct { |
| 49 | request bool |
| 50 | pageSize int |
| 51 | }) |
| 52 | for _, rt := range routes { |
| 53 | byName[rt.Name] = struct { |
| 54 | request bool |
| 55 | pageSize int |
| 56 | }{ |
| 57 | request: rt.Request != nil, |
| 58 | pageSize: rt.PageSize, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if !byName["compare"].request { |
| 63 | t.Error("compare route has no Request parser") |
| 64 | } |
| 65 | if byName["log"].pageSize == 0 { |
| 66 | t.Error("log route has no PageSize") |
| 67 | } |
| 68 | if byName["graph"].pageSize == 0 { |
| 69 | t.Error("graph route has no PageSize") |
| 70 | } |
| 71 | } |