| Initial import of the rickub CLI as a standalone public project 1a1d430 Olivier Girardot 9h ago | 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestNormalizeAPIPath(t *testing.T) { |
| 12 | cases := map[string]string{ |
| 13 | "/user": "/user", |
| 14 | "user": "/user", |
| 15 | "/api/v1/user": "/user", |
| 16 | "search/repos": "/search/repos", |
| 17 | "/api/v1/repos": "/repos", |
| 18 | } |
| 19 | for in, want := range cases { |
| 20 | if got := normalizeAPIPath(in); got != want { |
| 21 | t.Errorf("normalizeAPIPath(%q) = %q, want %q", in, got, want) |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func TestInferType(t *testing.T) { |
| 27 | if inferType("true") != true { |
| 28 | t.Error("true") |
| 29 | } |
| 30 | if inferType("false") != false { |
| 31 | t.Error("false") |
| 32 | } |
| 33 | if inferType("null") != nil { |
| 34 | t.Error("null") |
| 35 | } |
| 36 | if inferType("42") != 42 { |
| 37 | t.Error("42") |
| 38 | } |
| 39 | if inferType("hi") != "hi" { |
| 40 | t.Error("hi") |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestAPICommandGET(t *testing.T) { |
| 45 | var gotPath, gotQuery string |
| 46 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 47 | gotPath = r.URL.Path |
| 48 | gotQuery = r.URL.RawQuery |
| 49 | json.NewEncoder(w).Encode(map[string]string{"handle": "ricktester"}) |
| 50 | })) |
| 51 | defer srv.Close() |
| 52 | t.Setenv("XDG_CONFIG_HOME", t.TempDir()) |
| 53 | t.Setenv("RICKUB_HOST", srv.URL) |
| 54 | t.Setenv("RICKUB_TOKEN", "rickub_pat_test") |
| 55 | // reset escape-hatch field flags |
| 56 | apiFields = nil |
| 57 | apiRawFields = nil |
| 58 | |
| 59 | out, _, err := execute(t, "api", "GET", "search/repos", "-f", "q=api") |
| 60 | if err != nil { |
| 61 | t.Fatalf("execute: %v", err) |
| 62 | } |
| 63 | if gotPath != "/api/v1/search/repos" { |
| 64 | t.Errorf("path = %q", gotPath) |
| 65 | } |
| 66 | if gotQuery != "q=api" { |
| 67 | t.Errorf("query = %q", gotQuery) |
| 68 | } |
| 69 | if !strings.Contains(out, "ricktester") { |
| 70 | t.Errorf("output = %q", out) |
| 71 | } |
| 72 | } |