-
Notifications
You must be signed in to change notification settings - Fork 4
/
client_test.go
149 lines (120 loc) · 2.96 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package gomo
import (
"bytes"
"log"
"net/http"
"strings"
"testing"
)
func testClient() Client {
return Client{
credentials: NewClientCredentials("abc", "def"),
APIVersion: defaultAPIVersion,
Endpoint: defaultEndpoint,
Debug: false,
httpClient: &http.Client{},
Logger: defaultLogger,
}
}
func TestClientDefaults(t *testing.T) {
c := testClient()
// test the default endpoint
expectedEndpoint := "https://api.moltin.com"
if c.Endpoint != expectedEndpoint {
t.Errorf("Incorrect default endpoint: %s (expected %s)", c.Endpoint, expectedEndpoint)
}
// test the API version
expectedAPIVersion := "v2"
if c.APIVersion != expectedAPIVersion {
t.Errorf("Incorrect API version: %s (expected %s)", c.APIVersion, expectedAPIVersion)
}
// test the defaul debug status
if c.Debug != false {
t.Errorf("Incorrect debug val: %t (expected %t)", c.Debug, false)
}
}
func TestClientDebug(t *testing.T) {
c := testClient()
// test enabling debug
c.EnableDebug()
if c.Debug != true {
t.Errorf("Incorrect debug val: %t (expected %t)", c.Debug, true)
}
// test disabling debug
c.DisableDebug()
if c.Debug != false {
t.Errorf("Incorrect debug val: %t (expected %t)", c.Debug, false)
}
}
func TestCustomEndpoint(t *testing.T) {
endpoint := "https://yourdomain.com"
c := testClient()
c.CustomEndpoint(endpoint)
if c.Endpoint != endpoint {
t.Errorf("Incorrect debug val: %s (expected %s)", c.Endpoint, endpoint)
}
}
func TestClientGrantTypeImplicit(t *testing.T) {
client := NewClient(
NewImplicitCredentials(
"abc",
),
)
if client.GrantType() != "implicit" {
t.Error("Implicit Credentials do not return implicit grant type")
}
}
func TestClientGrantTypeClientCredentials(t *testing.T) {
client := NewClient(
NewClientCredentials(
"abc",
"def",
),
)
if client.GrantType() != "client_credentials" {
t.Error("Client Credentials do not return client_credentials grant type")
}
}
func TestCustomLogger(t *testing.T) {
client := NewClient(
NewClientCredentials(
"abc",
"def",
),
)
client.EnableDebug()
logHit := false
client.CustomLogger(func(c *Client, msg interface{}) {
logHit = true
})
err := client.Authenticate()
client.Log(err)
if logHit == false {
t.Errorf("log not hit")
}
}
func TestDefaultLogger(t *testing.T) {
client := NewClient(
NewClientCredentials(
"abc",
"def",
),
)
client.EnableDebug()
var debugOnOut bytes.Buffer
log.SetOutput(&debugOnOut)
client.Log("a test")
// the log time is included eg "2019/03/29 23:44:59 a test"
// remove the date and new line then test the rest of the string
o := strings.TrimSuffix(debugOnOut.String()[20:], "\n")
if o != "a test" {
t.Errorf("Logger did not work: `%s` (expected `%s`)", o, "a test")
}
client.DisableDebug()
var debugOffOut bytes.Buffer
log.SetOutput(&debugOffOut)
client.Log("a test")
if debugOffOut.String() != "" {
t.Errorf("Logger did not work - did not expect any output: `%s`", debugOffOut.String())
}
}