-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
107 lines (94 loc) · 2.48 KB
/
client_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
106
107
package disk
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
func mockedHttpClient(h http.HandlerFunc) *Client {
httpClient, _ := testingHTTPClient(h)
client := *New("token")
client.HTTPClient = httpClient
return &client
}
func testingHTTPClient(handler http.Handler) (*http.Client, func()) {
s := httptest.NewTLSServer(handler)
client := &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, network, _ string) (net.Conn, error) {
return net.Dial(network, s.Listener.Addr().String())
},
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
return client, s.Close
}
func TestNew(t *testing.T) {
// Helper function to reset environment variable
resetEnv := func() {
os.Unsetenv("YANDEX_DISK_ACCESS_TOKEN")
}
t.Run("With provided token", func(t *testing.T) {
resetEnv()
client := New("test-token")
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.AccessToken != "test-token" {
t.Errorf("Expected AccessToken to be 'test-token', got '%s'", client.AccessToken)
}
if client.HTTPClient == nil {
t.Fatal("Expected non-nil HTTPClient")
}
if client.HTTPClient.Timeout != 10*time.Second {
t.Errorf("Expected Timeout to be 10 seconds, got %v", client.HTTPClient.Timeout)
}
})
t.Run("With environment variable", func(t *testing.T) {
resetEnv()
os.Setenv("YANDEX_DISK_ACCESS_TOKEN", "env-token")
client := New()
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.AccessToken != "env-token" {
t.Errorf("Expected AccessToken to be 'env-token', got '%s'", client.AccessToken)
}
})
t.Run("Without token and empty environment variable", func(t *testing.T) {
resetEnv()
client := New()
if client != nil {
t.Fatal("Expected nil client")
}
})
t.Run("With multiple tokens", func(t *testing.T) {
resetEnv()
client := New("token1", "token2")
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.AccessToken != "token1" {
t.Errorf("Expected AccessToken to be 'token1', got '%s'", client.AccessToken)
}
})
t.Run("HTTPClient configuration", func(t *testing.T) {
resetEnv()
client := New("test-token")
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.HTTPClient == nil {
t.Fatal("Expected non-nil HTTPClient")
}
if client.HTTPClient.Timeout != 10*time.Second {
t.Errorf("Expected Timeout to be 10 seconds, got %v", client.HTTPClient.Timeout)
}
})
}