1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
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)
}
}
|