-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathclientlists_test.go
87 lines (79 loc) · 1.94 KB
/
clientlists_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
package clientlists
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/edgegrid"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
"github.com/stretchr/testify/require"
"github.com/tj/assert"
)
// compactJSON converts a JSON-encoded byte slice to a compact form (so our JSON fixtures can be readable)
func compactJSON(encoded []byte) string {
buf := bytes.Buffer{}
if err := json.Compact(&buf, encoded); err != nil {
panic(fmt.Sprintf("%s: %s", err, string(encoded)))
}
return buf.String()
}
// loadFixtureBytes returns the entire contents of the given file as a byte slice
func loadFixtureBytes(path string) []byte {
contents, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
return contents
}
func mockAPIClient(t *testing.T, mockServer *httptest.Server) ClientLists {
serverURL, err := url.Parse(mockServer.URL)
require.NoError(t, err)
certPool := x509.NewCertPool()
certPool.AddCert(mockServer.Certificate())
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
},
},
}
s, err := session.New(session.WithClient(httpClient), session.WithSigner(&edgegrid.Config{Host: serverURL.Host}))
assert.NoError(t, err)
return Client(s)
}
func opt() Option {
return func(*clientlists) {}
}
func TestClient(t *testing.T) {
sess, err := session.New()
require.NoError(t, err)
tests := map[string]struct {
options []Option
expected *clientlists
}{
"no options provided, return default": {
options: nil,
expected: &clientlists{
Session: sess,
},
},
"dummy option": {
options: []Option{opt()},
expected: &clientlists{
Session: sess,
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
res := Client(sess, test.options...)
assert.Equal(t, res, test.expected)
})
}
}