-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_internal_test.go
105 lines (90 loc) · 2.09 KB
/
client_internal_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
package bunnystorage
import (
"bytes"
"context"
"io"
"net/http"
"testing"
)
func TestRequest(t *testing.T) {
t.Parallel()
cfg := &Config{
StorageZone: "my-storage-zone",
Key: "my-key",
ReadOnlyKey: "my-read-only-key",
Endpoint: EndpointFalkenstein,
}
client, err := NewClient(cfg)
if err != nil {
t.Fatalf("failed to create test client: %v", err)
}
tests := []struct {
name string
ctx context.Context
method string
uri string
headers map[string]string
body io.Reader
expectErr bool
}{
{
name: "GET request",
ctx: context.Background(),
method: http.MethodGet,
uri: "https://example.com",
headers: map[string]string{
"Accept": "application/json",
},
body: http.NoBody,
},
{
name: "POST request",
ctx: context.Background(),
method: http.MethodPost,
uri: "https://example.com",
headers: map[string]string{
"Content-Type": "application/json",
},
body: bytes.NewBuffer([]byte(`{"key": "value"}`)),
},
{
name: "Invalid context",
ctx: nil,
method: http.MethodGet,
uri: "https://example.com",
headers: nil,
body: http.NoBody,
expectErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
req, err := client.request(tt.ctx, tt.method, tt.uri, tt.headers, tt.body)
if tt.expectErr {
if err == nil {
t.Fatal("expected an error, got nil")
}
return
}
if err != nil {
t.Fatalf("request() failed: %v", err)
}
if req.Method != tt.method {
t.Errorf("expected method %s, got %s", tt.method, req.Method)
}
if req.URL.String() != tt.uri {
t.Errorf("expected uri %s, got %s", tt.uri, req.URL.String())
}
for k, v := range tt.headers {
if req.Header.Get(k) != v {
t.Errorf("expected header %s with value %s, got %s", k, v, req.Header.Get(k))
}
}
if req.Header.Get("User-Agent") != cfg.UserAgent {
t.Errorf("expected User-Agent header %s, got %s", cfg.UserAgent, req.Header.Get("User-Agent"))
}
})
}
}