-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
213 lines (183 loc) · 5.8 KB
/
client_test.go
File metadata and controls
213 lines (183 loc) · 5.8 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package slicer
import (
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestNormalizeUnixSocketPath(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
tests := []struct {
name string
input string
want string
ok bool
}{
{name: "absolute path", input: "/tmp/slicer.sock", want: "/tmp/slicer.sock", ok: true},
{name: "relative path", input: "./slicer.sock", want: "./slicer.sock", ok: true},
{name: "parent relative path", input: "../slicer.sock", want: "../slicer.sock", ok: true},
{name: "bare socket path", input: "slicer.sock", want: "slicer.sock", ok: true},
{name: "tilde socket path", input: "~/slicer.sock", want: filepath.Join(home, "slicer.sock"), ok: true},
{name: "unix scheme path", input: "unix:///tmp/slicer.sock", want: "/tmp/slicer.sock", ok: true},
{name: "http url", input: "http://127.0.0.1:8080", want: "", ok: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := normalizeUnixSocketPath(tt.input)
if ok != tt.ok {
t.Fatalf("normalizeUnixSocketPath(%q) ok = %v, want %v", tt.input, ok, tt.ok)
}
if got != tt.want {
t.Fatalf("normalizeUnixSocketPath(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}
func TestNewSlicerClient_NormalizesUnixSocketPath(t *testing.T) {
home := t.TempDir()
originalHome := os.Getenv("HOME")
t.Setenv("HOME", home)
t.Cleanup(func() {
_ = os.Setenv("HOME", originalHome)
})
client := NewSlicerClient("unix://~/slicer.sock", "", "agent", nil)
want := filepath.Join(home, "slicer.sock")
if client.unixSocket != want {
t.Fatalf("client.unixSocket = %q, want %q", client.unixSocket, want)
}
if client.baseURL != "http://unix" {
t.Fatalf("client.baseURL = %q, want %q", client.baseURL, "http://unix")
}
}
func TestMakeRequest_AuthHeaderWithToken(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
want := "Bearer test-token"
if auth != want {
t.Errorf("Want '%s', got '%s'", want, auth)
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
client := NewSlicerClient(server.URL, "test-token", "test-agent", nil)
resp, err := client.makeJSONRequest(http.MethodGet, "/test", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if resp != nil {
resp.Body.Close()
}
}
func TestMakeRequest_NoAuthHeaderWhenTokenEmpty(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if auth != "" {
t.Errorf("Want no Authorization header, got '%s'", auth)
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
client := NewSlicerClient(server.URL, "", "test-agent", nil)
resp, err := client.makeJSONRequest(http.MethodGet, "/test", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if resp != nil {
resp.Body.Close()
}
}
func TestMakeRequest_WithBody(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify Content-Type header
ct := r.Header.Get("Content-Type")
want := "application/json"
if ct != want {
t.Errorf("Want '%s', got '%s'", want, ct)
}
// Verify body content
body, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("Failed to read request body: %v", err)
}
wantBody := `{"name":"test","value":"data"}`
if string(body) != wantBody {
t.Errorf("Want body '%s', got '%s'", wantBody, string(body))
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
client := NewSlicerClient(server.URL, "token", "agent", nil)
requestBody := map[string]string{"name": "test", "value": "data"}
resp, err := client.makeJSONRequest(http.MethodPost, "/test", requestBody)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if resp != nil {
resp.Body.Close()
}
}
func TestMakeRequest_WithoutBody(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify no Content-Type header for requests without body
ct := r.Header.Get("Content-Type")
if ct != "" {
t.Errorf("Want no Content-Type header, got '%s'", ct)
}
// Verify method and path
if r.Method != http.MethodGet {
t.Errorf("Want %s method, got %s", http.MethodGet, r.Method)
}
want := "/test"
if r.URL.Path != want {
t.Errorf("Want %s path, got %s", want, r.URL.Path)
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
client := NewSlicerClient(server.URL, "token", "agent", nil)
resp, err := client.makeJSONRequest(http.MethodGet, "/test", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if resp != nil {
resp.Body.Close()
}
}
func TestMakeRequest_InvalidJSON(t *testing.T) {
client := NewSlicerClient("http://localhost", "token", "agent", nil)
// Use a channel which can't be marshaled to JSON
invalidBody := make(chan int)
_, err := client.makeJSONRequest(http.MethodPost, "/test", invalidBody)
if err == nil {
t.Error("Want error, got nil")
}
}
func TestMakeRequest_CustomUserAgent(t *testing.T) {
customAgent := "custom-user-agent/1.0"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ua := r.Header.Get("User-Agent")
if ua != customAgent {
t.Errorf("Want '%s', got '%s'", customAgent, ua)
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
client := NewSlicerClient(server.URL, "token", customAgent, nil)
resp, err := client.makeJSONRequest(http.MethodGet, "/test", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if resp != nil {
resp.Body.Close()
}
}
func TestMakeRequest_InvalidBaseURL(t *testing.T) {
client := NewSlicerClient("://invalid-url", "token", "agent", nil)
_, err := client.makeJSONRequest(http.MethodGet, "/test", nil)
if err == nil {
t.Error("Want error, got nil")
}
}