-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
355 lines (320 loc) · 10.1 KB
/
client_test.go
File metadata and controls
355 lines (320 loc) · 10.1 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package forge
import (
"context"
"fmt"
json "github.com/goccy/go-json"
"net/http"
"net/http/httptest"
"testing"
core "dappco.re/go/core"
)
func TestClient_Get_Good(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("expected GET, got %s", r.Method)
}
if r.Header.Get("Authorization") != "token test-token" {
t.Errorf("missing auth header")
}
if r.URL.Path != "/api/v1/user" {
t.Errorf("wrong path: %s", r.URL.Path)
}
json.NewEncoder(w).Encode(map[string]string{"login": "virgil"})
}))
defer srv.Close()
c := NewClient(srv.URL, "test-token")
var out map[string]string
err := c.Get(context.Background(), "/api/v1/user", &out)
if err != nil {
t.Fatal(err)
}
if out["login"] != "virgil" {
t.Errorf("got login=%q", out["login"])
}
}
func TestClient_Post_Good(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
var body map[string]string
json.NewDecoder(r.Body).Decode(&body)
if body["name"] != "test-repo" {
t.Errorf("wrong body: %v", body)
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]any{"id": 1, "name": "test-repo"})
}))
defer srv.Close()
c := NewClient(srv.URL, "test-token")
body := map[string]string{"name": "test-repo"}
var out map[string]any
err := c.Post(context.Background(), "/api/v1/orgs/core/repos", body, &out)
if err != nil {
t.Fatal(err)
}
if out["name"] != "test-repo" {
t.Errorf("got name=%v", out["name"])
}
}
func TestClient_PostRaw_Good(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if got := r.URL.Path; got != "/api/v1/markdown" {
t.Errorf("wrong path: %s", got)
}
w.Header().Set("X-RateLimit-Limit", "100")
w.Header().Set("X-RateLimit-Remaining", "98")
w.Header().Set("X-RateLimit-Reset", "1700000001")
w.Write([]byte("<p>Hello</p>"))
}))
defer srv.Close()
c := NewClient(srv.URL, "test-token")
body := map[string]string{"text": "Hello"}
got, err := c.PostRaw(context.Background(), "/api/v1/markdown", body)
if err != nil {
t.Fatal(err)
}
if string(got) != "<p>Hello</p>" {
t.Errorf("got body=%q", string(got))
}
rl := c.RateLimit()
if rl.Limit != 100 || rl.Remaining != 98 || rl.Reset != 1700000001 {
t.Fatalf("unexpected rate limit: %+v", rl)
}
}
func TestClient_Delete_Good(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
t.Errorf("expected DELETE, got %s", r.Method)
}
w.WriteHeader(http.StatusNoContent)
}))
defer srv.Close()
c := NewClient(srv.URL, "test-token")
err := c.Delete(context.Background(), "/api/v1/repos/core/test")
if err != nil {
t.Fatal(err)
}
}
func TestClient_GetRaw_Good(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("expected GET, got %s", r.Method)
}
if got := r.URL.Path; got != "/api/v1/signing-key.gpg" {
t.Errorf("wrong path: %s", got)
}
w.Header().Set("X-RateLimit-Limit", "60")
w.Header().Set("X-RateLimit-Remaining", "59")
w.Header().Set("X-RateLimit-Reset", "1700000002")
w.Write([]byte("key-data"))
}))
defer srv.Close()
c := NewClient(srv.URL, "test-token")
got, err := c.GetRaw(context.Background(), "/api/v1/signing-key.gpg")
if err != nil {
t.Fatal(err)
}
if string(got) != "key-data" {
t.Errorf("got body=%q", string(got))
}
rl := c.RateLimit()
if rl.Limit != 60 || rl.Remaining != 59 || rl.Reset != 1700000002 {
t.Fatalf("unexpected rate limit: %+v", rl)
}
}
func TestClient_ServerError_Bad(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"message": "internal error"})
}))
defer srv.Close()
c := NewClient(srv.URL, "test-token")
err := c.Get(context.Background(), "/api/v1/user", nil)
if err == nil {
t.Fatal("expected error")
}
var apiErr *APIError
if !core.As(err, &apiErr) {
t.Fatalf("expected APIError, got %T", err)
}
if apiErr.StatusCode != 500 {
t.Errorf("got status=%d", apiErr.StatusCode)
}
}
func TestClient_NotFound_Bad(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"message": "not found"})
}))
defer srv.Close()
c := NewClient(srv.URL, "test-token")
err := c.Get(context.Background(), "/api/v1/repos/x/y", nil)
if !IsNotFound(err) {
t.Fatalf("expected not found, got %v", err)
}
}
func TestClient_ContextCancellation_Good(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-r.Context().Done()
}))
defer srv.Close()
c := NewClient(srv.URL, "test-token")
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancel immediately
err := c.Get(ctx, "/api/v1/user", nil)
if err == nil {
t.Fatal("expected error from cancelled context")
}
}
func TestClient_Options_Good(t *testing.T) {
c := NewClient("https://forge.lthn.ai", "tok",
WithUserAgent("go-forge/1.0"),
)
if c.userAgent != "go-forge/1.0" {
t.Errorf("got user agent=%q", c.userAgent)
}
if got := c.UserAgent(); got != "go-forge/1.0" {
t.Errorf("got UserAgent()=%q", got)
}
}
func TestClient_HasToken_Good(t *testing.T) {
c := NewClient("https://forge.lthn.ai", "tok")
if !c.HasToken() {
t.Fatal("expected HasToken to report configured token")
}
}
func TestClient_HasToken_Bad(t *testing.T) {
c := NewClient("https://forge.lthn.ai", "")
if c.HasToken() {
t.Fatal("expected HasToken to report missing token")
}
}
func TestClient_NilSafeAccessors(t *testing.T) {
var c *Client
if got := c.BaseURL(); got != "" {
t.Fatalf("got BaseURL()=%q, want empty string", got)
}
if got := c.RateLimit(); got != (RateLimit{}) {
t.Fatalf("got RateLimit()=%#v, want zero value", got)
}
if got := c.UserAgent(); got != "" {
t.Fatalf("got UserAgent()=%q, want empty string", got)
}
if got := c.HTTPClient(); got != nil {
t.Fatal("expected HTTPClient() to return nil")
}
if got := c.HasToken(); got {
t.Fatal("expected HasToken() to report false")
}
}
func TestClient_WithHTTPClient_Good(t *testing.T) {
custom := &http.Client{}
c := NewClient("https://forge.lthn.ai", "tok", WithHTTPClient(custom))
if c.httpClient != custom {
t.Error("expected custom HTTP client to be set")
}
if got := c.HTTPClient(); got != custom {
t.Error("expected HTTPClient() to return the configured HTTP client")
}
}
func TestClient_String_Good(t *testing.T) {
c := NewClient("https://forge.lthn.ai", "tok", WithUserAgent("go-forge/1.0"))
got := fmt.Sprint(c)
want := `forge.Client{baseURL="https://forge.lthn.ai", token=set, userAgent="go-forge/1.0"}`
if got != want {
t.Fatalf("got %q, want %q", got, want)
}
if got := c.String(); got != want {
t.Fatalf("got String()=%q, want %q", got, want)
}
if got := fmt.Sprintf("%#v", c); got != want {
t.Fatalf("got GoString=%q, want %q", got, want)
}
}
func TestAPIError_Error_Good(t *testing.T) {
e := &APIError{StatusCode: 404, Message: "not found", URL: "/api/v1/repos/x/y"}
got := e.Error()
want := "forge: /api/v1/repos/x/y 404: not found"
if got != want {
t.Errorf("got %q, want %q", got, want)
}
if got := e.String(); got != want {
t.Errorf("got String()=%q, want %q", got, want)
}
if got := fmt.Sprint(e); got != want {
t.Errorf("got fmt.Sprint=%q, want %q", got, want)
}
if got := fmt.Sprintf("%#v", e); got != want {
t.Errorf("got GoString=%q, want %q", got, want)
}
}
func TestIsConflict_Match_Good(t *testing.T) {
err := &APIError{StatusCode: http.StatusConflict, Message: "conflict", URL: "/test"}
if !IsConflict(err) {
t.Error("expected IsConflict to return true for 409")
}
}
func TestIsConflict_NotConflict_Bad(t *testing.T) {
err := &APIError{StatusCode: http.StatusNotFound, Message: "not found", URL: "/test"}
if IsConflict(err) {
t.Error("expected IsConflict to return false for 404")
}
}
func TestIsForbidden_NotForbidden_Bad(t *testing.T) {
err := &APIError{StatusCode: http.StatusNotFound, Message: "not found", URL: "/test"}
if IsForbidden(err) {
t.Error("expected IsForbidden to return false for 404")
}
}
func TestClient_RateLimit_Good(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-RateLimit-Limit", "100")
w.Header().Set("X-RateLimit-Remaining", "99")
w.Header().Set("X-RateLimit-Reset", "1700000000")
json.NewEncoder(w).Encode(map[string]string{"login": "test"})
}))
defer srv.Close()
c := NewClient(srv.URL, "tok")
var out map[string]string
if err := c.Get(context.Background(), "/api/v1/user", &out); err != nil {
t.Fatal(err)
}
rl := c.RateLimit()
if rl.Limit != 100 {
t.Errorf("got limit=%d, want 100", rl.Limit)
}
if rl.Remaining != 99 {
t.Errorf("got remaining=%d, want 99", rl.Remaining)
}
if rl.Reset != 1700000000 {
t.Errorf("got reset=%d, want 1700000000", rl.Reset)
}
}
func TestClient_Forbidden_Bad(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode(map[string]string{"message": "forbidden"})
}))
defer srv.Close()
c := NewClient(srv.URL, "tok")
err := c.Get(context.Background(), "/api/v1/admin", nil)
if !IsForbidden(err) {
t.Fatalf("expected forbidden, got %v", err)
}
}
func TestClient_Conflict_Bad(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusConflict)
json.NewEncoder(w).Encode(map[string]string{"message": "already exists"})
}))
defer srv.Close()
c := NewClient(srv.URL, "tok")
err := c.Post(context.Background(), "/api/v1/repos", map[string]string{"name": "dup"}, nil)
if !IsConflict(err) {
t.Fatalf("expected conflict, got %v", err)
}
}