-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathrestish_test.go
More file actions
85 lines (73 loc) · 2.35 KB
/
Copy pathrestish_test.go
File metadata and controls
85 lines (73 loc) · 2.35 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
package restish_test
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"github.com/rest-sh/restish/v2"
)
type testAuth struct{}
func (testAuth) Parameters() []restish.AuthParam {
return []restish.AuthParam{{Name: "token", Required: true, Secret: true}}
}
func (testAuth) Authenticate(_ context.Context, req *http.Request, ac restish.AuthContext) error {
req.Header.Set("Authorization", "Bearer "+ac.Params["token"])
return nil
}
type testFormatter struct{}
func (testFormatter) Format(w io.Writer, resp *restish.Response, _ bool) error {
_, err := fmt.Fprint(w, resp.Body)
return err
}
type testLinkParser struct{}
func (testLinkParser) ParseLinks(baseURL *url.URL, _ http.Header, _ any) []restish.Link {
return []restish.Link{{Rel: "self", URI: baseURL.String()}}
}
type testLoader struct{}
func (testLoader) Detect(string, []byte) bool { return false }
func (testLoader) LoadWithOptions([]byte, restish.LoadOptions) (*restish.APISpec, error) {
return nil, nil
}
func (testLoader) Priority() int { return 1000 }
func TestPublicAPIEmbeddableCLI(t *testing.T) {
configDir := t.TempDir()
t.Setenv("RSH_CONFIG_DIR", configDir)
c := restish.New()
c.Stdin = bytes.NewReader(nil)
c.Stdout = &bytes.Buffer{}
c.Stderr = &bytes.Buffer{}
c.SetCommandName("acme")
c.SetCommandDescription("Acme API CLI", "Acme API CLI long help.")
c.SetVersion("acme-dev")
c.SetDefaultConfig(&restish.Config{
APIs: map[string]*restish.APIConfig{
"acme": {BaseURL: "https://api.example.com"},
},
})
c.AddAuthHandler("test-auth", testAuth{})
c.AddContentType(&restish.ContentType{Name: "example"})
c.AddFormatter("test", testFormatter{})
c.AddLinkParser(testLinkParser{})
c.AddLoader(testLoader{})
if err := os.WriteFile(filepath.Join(configDir, "restish.json"), []byte(`{"apis":{"user":{"base_url":"https://user.example.com"}}}`), 0o600); err != nil {
t.Fatal(err)
}
if restish.Version() == "" {
t.Fatal("expected version")
}
if err := c.Run([]string{"acme", "version"}); err != nil {
t.Fatalf("run version: %v", err)
}
if got := c.Stdout.(*bytes.Buffer).String(); !strings.Contains(got, "acme-dev") {
t.Fatalf("version output = %q, want custom version", got)
}
if c.Config().APIs["acme"] == nil || c.Config().APIs["user"] == nil {
t.Fatalf("default and user APIs should both be present: %#v", c.Config().APIs)
}
}