-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
98 lines (86 loc) · 1.89 KB
/
client_test.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
package client
import (
"fmt"
"net/http"
"testing"
)
func TestNewClient(t *testing.T) {
testHost := "http://localhost:5050"
tt := []struct {
name, host string
auth Authenticator
transport http.RoundTripper
}{
{"basic auth", testHost, &BasicAuth{"user", "password"}, nil},
{"token auth", testHost, &TokenAuth{"token"}, nil},
{"custom transport", testHost, nil, &http.Transport{}},
}
for _, tc := range tt {
tf := func(t *testing.T) {
cfg := Config{
Host: tc.host,
Auth: tc.auth,
Transport: tc.transport,
}
c, err := New(cfg)
if err != nil {
t.Fatal(err)
}
NewDistributionAPI(c)
}
t.Run(tc.name, tf)
}
}
func TestAuthSchemes(t *testing.T) {
tt := []struct {
name string
auth Authenticator
}{
{"basic auth", &BasicAuth{"user", "password"}},
{"token auth", &TokenAuth{"token"}},
}
for _, tc := range tt {
tf := func(t *testing.T) {
req := new(http.Request)
req.Header = make(http.Header)
tc.auth.Set(req)
val := req.Header.Get("Authorization")
if val == "" {
t.Fatal("authorization header not set")
}
}
t.Run(tc.name, tf)
}
}
type badTransport struct{}
func (t *badTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("bad round trip")
}
func TestRoundTrip(t *testing.T) {
tt := []struct {
name string
transport http.RoundTripper
valid bool
}{
{"valid round trip", &DefaultTransport, true},
{"bad round trip", &badTransport{}, false},
}
for _, tc := range tt {
tf := func(t *testing.T) {
cfg := Config{
Host: "http://localhost",
Transport: tc.transport,
}
c, err := New(cfg)
if err != nil {
t.Fatal(err)
}
req, _ := http.NewRequest("GET", "http://localhost", nil)
_, err = c.Transport.RoundTrip(req)
if err == nil && !tc.valid {
t.Fatal("expected valid transport")
}
}
t.Run(tc.name, tf)
}
}