-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
50 lines (42 loc) · 1.12 KB
/
Copy pathclient_test.go
File metadata and controls
50 lines (42 loc) · 1.12 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
package api
import (
"net/http"
"testing"
)
func TestNewRequest(t *testing.T) {
client := NewClient("https://example.com/api/v1", "test-key")
tests := []struct {
name string
method string
path string
}{
{"GET", http.MethodGet, "/api-key"},
{"POST", http.MethodPost, "/some-resource"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := client.newRequest(tt.method, tt.path)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if req.Method != tt.method {
t.Errorf("method = %q, want %q", req.Method, tt.method)
}
wantURL := "https://example.com/api/v1" + tt.path
if req.URL.String() != wantURL {
t.Errorf("url = %q, want %q", req.URL.String(), wantURL)
}
wantAuth := "Bearer test-key"
if got := req.Header.Get("Authorization"); got != wantAuth {
t.Errorf("Authorization = %q, want %q", got, wantAuth)
}
})
}
}
func TestNewRequest_InvalidURL(t *testing.T) {
client := NewClient("://bad-url", "test-key")
_, err := client.newRequest(http.MethodGet, "/path")
if err == nil {
t.Error("expected error for invalid URL, got nil")
}
}