-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
200 lines (163 loc) · 5.16 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
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
package webs
import (
"github.com/h2non/gock"
"net/http"
"testing"
)
// TestClient_Build verifies that the ClientBuilder successfully creates a non-nil Client instance when Build is invoked.
func TestClient_Build(t *testing.T) {
client := NewClientBuilder().Build()
if client == nil {
t.Errorf("expected client, got nil")
}
}
// TestClient_Get validates that the Get method of the custom HTTP client returns an expected response without errors.
func TestClient_Get(t *testing.T) {
defer gock.Off()
gock.New("https://server.com").
Get("/").
Reply(200).
JSON(map[string]string{"hello": "world"})
client := NewClientBuilder().Build()
gock.InterceptClient(client.client)
res, err := client.Get("https://server.com", nil)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if res == nil {
t.Fatal("expected response, got nil")
}
if res.statusCode != 200 {
t.Errorf("expected status code 200, got %d", res.statusCode)
}
}
// TestClient_Post is a test function that verifies the behavior of the Client's Post method.
// It uses the gock library to mock HTTP responses and ensures that the client correctly sends a POST request,
// processes the response, and unmarshals JSON content.
func TestClient_Post(t *testing.T) {
defer gock.Off()
gock.New("https://server.com").
Post("/").
MatchType("json").
JSON(map[string]string{"foo": "bar"}).
Reply(201).
JSON(map[string]string{"foo": "bar"})
client := NewClientBuilder().Build()
gock.InterceptClient(client.client)
body := map[string]string{"foo": "bar"}
header := http.Header{}
header.Add("Content-Type", "application/json")
res, err := client.Post("https://server.com", header, body)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if res == nil {
t.Fatal("expected response, got nil")
}
if res.statusCode != 201 {
t.Errorf("expected status code 201, got %d", res.statusCode)
}
var content = make(map[string]string)
err = res.UnmarshalJson(&content)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if content["foo"] != "bar" {
t.Errorf("expected foo to be bar, got %s", content["foo"])
}
}
// TestClient_Put tests the Put method of the Client by sending a JSON request to a mock server and verifying the response.
func TestClient_Put(t *testing.T) {
defer gock.Off()
gock.New("https://server.com").
Put("/").
MatchType("json").
JSON(map[string]string{"foo": "bar"}).
Reply(200).
JSON(map[string]string{"foo": "bar"})
client := NewClientBuilder().Build()
gock.InterceptClient(client.client)
body := map[string]string{"foo": "bar"}
header := http.Header{}
header.Add("Content-Type", "application/json")
res, err := client.Put("https://server.com", header, body)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if res == nil {
t.Fatal("expected response, got nil")
}
if res.statusCode != 200 {
t.Errorf("expected status code 200, got %d", res.statusCode)
}
}
// TestClient_Patch tests the Client's ability to send an HTTP PATCH request and handle the response correctly.
func TestClient_Patch(t *testing.T) {
defer gock.Off()
gock.New("https://server.com").
Patch("/").
MatchType("json").
JSON(map[string]string{"foo": "bar"}).
Reply(204).
JSON(map[string]string{"foo": "bar"})
client := NewClientBuilder().Build()
gock.InterceptClient(client.client)
body := map[string]string{"foo": "bar"}
header := http.Header{}
header.Add("Content-Type", "application/json")
res, err := client.Patch("https://server.com", header, body)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if res == nil {
t.Fatal("expected response, got nil")
}
if res.statusCode != 204 {
t.Errorf("expected status code 204, got %d", res.statusCode)
}
}
// TestClient_Delete validates the HTTP DELETE request functionality of the Client.
// It mocks a DELETE request to "https://server.com/bar" and expects a 204 No Content status in the response.
// The test ensures no errors occur during the request and the correct status code is returned.
func TestClient_Delete(t *testing.T) {
defer gock.Off()
gock.New("https://server.com").
Delete("/bar").
Reply(204)
client := NewClientBuilder().Build()
gock.InterceptClient(client.client)
res, err := client.Delete("https://server.com/bar", nil)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if res == nil {
t.Fatal("expected response, got nil")
}
if res.statusCode != 204 {
t.Errorf("expected status code 204, got %d", res.statusCode)
}
}
// TestClient_Do tests the Do function of the Client type to ensure it correctly sends a GET request and handles the response.
func TestClient_Do(t *testing.T) {
defer gock.Off()
gock.New("https://server.com").
Get("/").
Reply(200).
JSON(map[string]string{"hello": "world"})
client := NewClientBuilder().Build()
gock.InterceptClient(client.client)
req, err := http.NewRequest("GET", "https://server.com", nil)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
res, err := client.Do(req)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if res == nil {
t.Fatal("expected response, got nil")
}
if res.statusCode != 200 {
t.Errorf("expected status code 200, got %d", res.statusCode)
}
}