-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
123 lines (105 loc) · 3.17 KB
/
Copy pathclient_test.go
File metadata and controls
123 lines (105 loc) · 3.17 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
package marketstack
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestNewClient(t *testing.T) {
t.Run("with API key", func(t *testing.T) {
client := NewClient("test-key", nil)
if client.apiKey != "test-key" {
t.Errorf("expected API key to be 'test-key', got '%s'", client.apiKey)
}
if client.httpClient == nil {
t.Error("expected httpClient to be set")
}
})
t.Run("from environment variable", func(t *testing.T) {
os.Setenv(envAPIKey, "env-test-key")
defer os.Unsetenv(envAPIKey)
client := NewClient("", nil)
if client.apiKey != "env-test-key" {
t.Errorf("expected API key from env to be 'env-test-key', got '%s'", client.apiKey)
}
})
t.Run("with custom HTTP client", func(t *testing.T) {
customClient := &http.Client{}
client := NewClient("test-key", customClient)
if client.httpClient != customClient {
t.Error("expected custom HTTP client to be used")
}
})
}
func TestSetBaseURL(t *testing.T) {
client := NewClient("test-key", nil)
client.SetBaseURL("https://custom.api.com/")
if client.baseURL != "https://custom.api.com" {
t.Errorf("expected base URL to be 'https://custom.api.com', got '%s'", client.baseURL)
}
}
func TestDoRequestMissingAPIKey(t *testing.T) {
client := NewClient("", nil)
os.Unsetenv(envAPIKey)
err := client.doRequest(context.Background(), "/test", nil, nil)
if err == nil {
t.Error("expected error for missing API key")
}
apiErr, ok := err.(*APIError)
if !ok {
t.Errorf("expected APIError, got %T", err)
}
if apiErr.Code != "missing_api_key" {
t.Errorf("expected error code 'missing_api_key', got '%s'", apiErr.Code)
}
}
func TestDoRequestAPIError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(ErrorResponse{
Error: &APIError{
Code: "invalid_access_key",
Message: "You have not supplied a valid API Access Key.",
},
})
}))
defer server.Close()
client := NewClient("invalid-key", nil)
client.SetBaseURL(server.URL)
var result map[string]interface{}
err := client.doRequest(context.Background(), "/test", nil, &result)
if err == nil {
t.Error("expected error for invalid API key")
}
apiErr, ok := err.(*APIError)
if !ok {
t.Errorf("expected APIError, got %T: %v", err, err)
}
if apiErr.Code != "invalid_access_key" {
t.Errorf("expected error code 'invalid_access_key', got '%s'", apiErr.Code)
}
}
func TestDoRequestSuccess(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("access_key") != "test-key" {
t.Error("API key not found in query parameters")
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{
"status": "success",
})
}))
defer server.Close()
client := NewClient("test-key", nil)
client.SetBaseURL(server.URL)
var result map[string]string
err := client.doRequest(context.Background(), "/test", nil, &result)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if result["status"] != "success" {
t.Errorf("expected status 'success', got '%s'", result["status"])
}
}