-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_test.go
190 lines (167 loc) · 4.92 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package client
import (
"testing"
_ "github.com/joho/godotenv/autoload"
)
func TestGetClient(t *testing.T) {
// Generate a new client with the actual token
client, err := GetClient()
if err != nil {
t.Errorf("Expected no error with token, got %v", err)
}
if client == nil {
t.Errorf("Expected a client, got nil")
}
client_err := newClient()
err = client_err.Token("invalid_token")
if err == nil {
t.Errorf("Expected an error, got nil")
}
}
// TestGetEnvironment tests the getEnvironment method for various host URLs.
func TestGetEnvironment(t *testing.T) {
// Define test cases
tests := []struct {
name string
hostURL string
expected Environment
}{
{
name: "Production Environment",
hostURL: "https://api.marketdata.app",
expected: Production,
},
{
name: "Testing Environment",
hostURL: "https://tst.marketdata.app",
expected: Test,
},
{
name: "Development Environment",
hostURL: "http://localhost",
expected: Development,
},
{
name: "Unknown Environment",
hostURL: "https://unknown.environment",
expected: "Unknown",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Create a new MarketDataClient instance
client := newClient()
// Set the HostURL to the test case's host URL
client.Client.SetBaseURL(tc.hostURL)
// Call getEnvironment and check the result
result := client.getEnvironment()
if result != tc.expected {
t.Errorf("Expected environment %s, got %s", tc.expected, result)
}
})
}
}
// TestEnvironmentMethod tests the Environment method for setting the client's environment.
func TestEnvironmentMethod(t *testing.T) {
// Define test cases
tests := []struct {
name string
environment Environment
expectedURL string
expectError bool
}{
{
name: "Set Production Environment",
environment: Production,
expectedURL: prodProtocol + "://" + prodHost,
expectError: false,
},
{
name: "Set Testing Environment",
environment: Test,
expectedURL: testProtocol + "://" + testHost,
expectError: false,
},
{
name: "Set Development Environment",
environment: Development,
expectedURL: devProtocol + "://" + devHost,
expectError: false,
},
{
name: "Set Invalid Environment",
environment: "invalidEnv",
expectedURL: "",
expectError: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Get the MarketDataClient instance
client := newClient()
// Set the environment using the Environment method
err := client.Environment(tc.environment)
// Check if an error was expected
if tc.expectError {
if err == nil {
t.Errorf("Expected an error for environment %s, but got none", tc.environment)
}
} else {
if err != nil {
t.Errorf("Did not expect an error for environment %s, but got: %v", tc.environment, err)
}
// Verify that the baseURL was set correctly
if client.Client.HostURL != tc.expectedURL {
t.Errorf("Expected baseURL %s, got %s", tc.expectedURL, client.Client.HostURL)
}
// Additionally, verify that getEnvironment returns the correct environment
result := client.getEnvironment()
if result != tc.environment {
t.Errorf("Expected environment %s, got %s", tc.environment, result)
}
}
})
}
}
/*
func TestRateLimit(t *testing.T) {
client, err := GetClient(os.Getenv("MARKETDATA_TOKEN"))
if err != nil {
t.Fatal(err)
}
// Check initial rate limit limit and reset time
initialLimit := client.RateLimitLimit
if initialLimit <= 0 {
t.Errorf("Expected positive rate limit limit, but got %d", initialLimit)
}
resetTime := client.RateLimitReset
if time.Now().After(resetTime) {
t.Errorf("Expected reset time in the future, but got %v", resetTime)
}
// Request to https://api.marketdata.app/stocks/quotes/AAPL/
resp, err := client.Get("https://api.marketdata.app/v1/stocks/quotes/AAPL/")
if err != nil {
t.Fatal(err)
}
fmt.Println("Headers after AAPL request:", resp.Header())
body := resp.Body()
fmt.Println("Status code after AAPL request:", resp.StatusCode())
fmt.Println("Body after AAPL request:", string(body))
initialRemaining := client.RateLimitRemaining
//fmt.Println("Rate limit consumed after AAPL request:", resp.RateLimitConsumed)
// Request to https://api.marketdata.app/stocks/quotes/SPY/
resp, err = client.Get("https://api.marketdata.app/v1/stocks/quotes/SPY/")
if err != nil {
t.Fatal(err)
}
fmt.Println("Headers after SPY request:", resp.Header())
body = resp.Body()
fmt.Println("Status code after SPY request:", resp.StatusCode())
fmt.Println("Body after SPY request:", string(body))
afterRequestRemaining := client.RateLimitRemaining
//fmt.Println("Rate limit consumed after SPY request:", resp.RateLimitConsumed)
if afterRequestRemaining != initialRemaining-1 {
t.Errorf("Expected remaining rate limit to decrease by 1, but got %d", initialRemaining-afterRequestRemaining)
}
}
*/