-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathigcclient_test.go
More file actions
161 lines (126 loc) · 3.63 KB
/
igcclient_test.go
File metadata and controls
161 lines (126 loc) · 3.63 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
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
150
151
152
153
154
155
156
157
158
159
160
161
package igcclient
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux
// client is the IGC client being tested.
client *IGCClient
// server is a test HTTP server used to provide mock API responses.
server *httptest.Server
xAPIKey = "xApiKey"
authToken = "authToken"
)
// setup sets up a test HTTP server along with a igcclient.IGCClient that is
// configured to talk to that test server. Tests should register handlers on
// mux which provide mock responses for the API method being tested.
func setup() func() {
// test server
mux = http.NewServeMux()
server = httptest.NewServer(mux)
parsed, _ := url.Parse(server.URL)
// IGCClient client configured to use test server
c, err := NewClient(parsed.String())
if err != nil {
log.Panic(err)
}
client = c
return func() {
server.Close()
}
}
func NewClient(serverHost string) (*IGCClient, error) {
logRequestBlacklist := []string{
"/user",
"/user/closeaccount",
"/v2/authentication/login",
"/authentication/change/password",
"/authentication/change/email",
"/authentication/change/securityquestion",
"/authentication/forgotpassword/change/sms",
"/authentication/forgotpassword/change",
"/v2/authentication/register",
}
logResponseBlacklist := []string{
"/consent/getconsents",
"/consent/userconsents",
"/countries",
"/user",
}
logBlacklist := []string{
"/authentication/isloggedin",
}
invalidAuthCallback := tokenInvalid
igcClient, err := NewIGCClient(Config{
BaseURL: fmt.Sprintf("%s", serverHost),
LogRequestBody: true,
LogResponseData: true,
LogRequestBlacklist: logRequestBlacklist,
LogResponseBlacklist: logResponseBlacklist,
LogBlacklist: logBlacklist,
Debug: true,
InvalidAuthCallback: &invalidAuthCallback,
})
if err != nil {
return nil, err
}
return igcClient, nil
}
func tokenInvalid(token string) {
}
func TestTimeoutError(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/devices", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(31 * time.Second)
w.WriteHeader(200)
w.Write([]byte("{\"Data\":[{\"DeviceTypeId\":1},{\"DeviceTypeId\":2}],\"Success\":true,\"Errors\":[]}"))
})
logger := testLogger{}
_, err := client.Devices.Devices(make(map[string]string), logger)
if err != nil && err.Error() == ErrorClientTimeout {
fmt.Println("Yey, we got timeout error as we wanted!")
} else if err != nil {
t.Errorf("Expected ErrorClientTimeout but got: %s", err)
} else {
t.Error("Expected ErrorClientTimeout")
}
}
type testLogger struct{}
func (l testLogger) UseTextFormatter() {}
func (l testLogger) SetLevel(level int) {}
func (l testLogger) SetContext(context map[string]interface{}) {}
func (l testLogger) GetContext() map[string]interface{} {
return nil
}
func (l testLogger) Println(args ...interface{}) {
log.Println(args...)
}
func (l testLogger) Printf(format string, args ...interface{}) {
log.Printf(format, args...)
}
func (l testLogger) Trace(msg string, fields map[string]interface{}) {
log.Printf(msg, fields)
}
func (l testLogger) Debug(msg string, fields map[string]interface{}) {
log.Printf(msg, fields)
}
func (l testLogger) Info(msg string, fields map[string]interface{}) {
log.Printf(msg, fields)
}
func (l testLogger) Warning(msg string, fields map[string]interface{}) {
log.Printf(msg, fields)
}
func (l testLogger) Error(msg string, fields map[string]interface{}) {
log.Printf(msg, fields)
}
func (l testLogger) Fatal(msg string, fields map[string]interface{}) {
log.Fatal(msg, fields)
}