forked from opus-domini/fast-shot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
126 lines (116 loc) · 3.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package fastshot
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/opus-domini/fast-shot/constant/method"
)
func TestNewClient(t *testing.T) {
tests := []struct {
name string
baseURL string
expectError bool
}{
{
name: "Successful Client Creation",
baseURL: "https://example.com",
expectError: false,
},
{
name: "Error Parsing URL",
baseURL: ":%^:",
expectError: true,
},
{
name: "Empty URL",
baseURL: "",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
clientBuilder := NewClient(tt.baseURL)
if !clientBuilder.client.Validations().IsEmpty() != tt.expectError {
t.Errorf("NewClient() error = %v, expectError %v", clientBuilder.client.Validations().Count() > 0, tt.expectError)
}
})
}
}
func TestNewClientLoadBalancer(t *testing.T) {
tests := []struct {
name string
baseURLs []string
expectError bool
}{
{
name: "Successful Client Load Balancer Creation",
baseURLs: []string{"https://example1.com", "https://example2.com"},
expectError: false,
},
{
name: "Error Parsing URL",
baseURLs: []string{"https://example1.com", ":%^:"},
expectError: true,
},
{
name: "Empty URL",
baseURLs: []string{"https://example1.com", ""},
expectError: true,
},
{
name: "Empty URL List",
baseURLs: []string{},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
clientBuilder := NewClientLoadBalancer(tt.baseURLs)
if (clientBuilder.client.Validations().Count() > 0) != tt.expectError {
t.Errorf("NewClientLoadBalancer() error = %v, expectError %v", clientBuilder.client.Validations().Count() > 0, tt.expectError)
}
})
}
}
func TestClientMethods(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("OK"))
if err != nil {
return
}
}))
defer server.Close()
clients := []struct {
name string
client ClientHttpMethods
}{
{"clientDefault", DefaultClient(server.URL)},
{"clientLoadBalancer", DefaultClientLoadBalancer([]string{server.URL})},
}
for _, client := range clients {
tests := []struct {
methodType method.Type
methodFunc func(string) *RequestBuilder
}{
{method.CONNECT, func(url string) *RequestBuilder { return client.client.CONNECT(url) }},
{method.DELETE, func(url string) *RequestBuilder { return client.client.DELETE(url) }},
{method.GET, func(url string) *RequestBuilder { return client.client.GET(url) }},
{method.HEAD, func(url string) *RequestBuilder { return client.client.HEAD(url) }},
{method.OPTIONS, func(url string) *RequestBuilder { return client.client.OPTIONS(url) }},
{method.PATCH, func(url string) *RequestBuilder { return client.client.PATCH(url) }},
{method.POST, func(url string) *RequestBuilder { return client.client.POST(url) }},
{method.PUT, func(url string) *RequestBuilder { return client.client.PUT(url) }},
{method.TRACE, func(url string) *RequestBuilder { return client.client.TRACE(url) }},
{method.Parse("TRACE"), func(url string) *RequestBuilder { return client.client.TRACE(url) }},
}
for _, tt := range tests {
t.Run(client.name+" "+tt.methodType.String(), func(t *testing.T) {
req := tt.methodFunc("/")
resp, _ := req.Send()
if resp.Status().IsError() {
t.Errorf("Expected 200, got %d", resp.Status().Code())
}
})
}
}
}