package cmd import ( "encoding/json" "net/http" "net/http/httptest" "strings" "testing" ) func TestNormalizeAPIPath(t *testing.T) { cases := map[string]string{ "/user": "/user", "user": "/user", "/api/v1/user": "/user", "search/repos": "/search/repos", "/api/v1/repos": "/repos", } for in, want := range cases { if got := normalizeAPIPath(in); got != want { t.Errorf("normalizeAPIPath(%q) = %q, want %q", in, got, want) } } } func TestInferType(t *testing.T) { if inferType("true") != true { t.Error("true") } if inferType("false") != false { t.Error("false") } if inferType("null") != nil { t.Error("null") } if inferType("42") != 42 { t.Error("42") } if inferType("hi") != "hi" { t.Error("hi") } } func TestAPICommandGET(t *testing.T) { var gotPath, gotQuery string srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotPath = r.URL.Path gotQuery = r.URL.RawQuery json.NewEncoder(w).Encode(map[string]string{"handle": "ricktester"}) })) defer srv.Close() t.Setenv("XDG_CONFIG_HOME", t.TempDir()) t.Setenv("RICKUB_HOST", srv.URL) t.Setenv("RICKUB_TOKEN", "rickub_pat_test") // reset escape-hatch field flags apiFields = nil apiRawFields = nil out, _, err := execute(t, "api", "GET", "search/repos", "-f", "q=api") if err != nil { t.Fatalf("execute: %v", err) } if gotPath != "/api/v1/search/repos" { t.Errorf("path = %q", gotPath) } if gotQuery != "q=api" { t.Errorf("query = %q", gotQuery) } if !strings.Contains(out, "ricktester") { t.Errorf("output = %q", out) } }