@@ -10,7 +10,7 @@ import (
1010)
1111
1212// newTestClient creates a RawClient that connects to an httptest.Server
13- func newTestClient (t * testing.T , handler http.Handler ) ( * RawClient , * httptest. Server ) {
13+ func newTestClient (t * testing.T , handler http.Handler ) * RawClient {
1414 t .Helper ()
1515 server := httptest .NewServer (handler )
1616 t .Cleanup (server .Close )
@@ -19,15 +19,23 @@ func newTestClient(t *testing.T, handler http.Handler) (*RawClient, *httptest.Se
1919 client : func () * http.Client {
2020 return & http.Client {
2121 Transport : & http.Transport {
22- DialContext : func (ctx context.Context , _ , _ string ) (net.Conn , error ) {
23- return net .Dial ("tcp" , server .Listener .Addr ().String ())
22+ DialContext : func (_ context.Context , _ , _ string ) (net.Conn , error ) {
23+ var d net.Dialer
24+ return d .DialContext (context .Background (), "tcp" , server .Listener .Addr ().String ())
2425 },
2526 },
2627 }
2728 },
2829 timeout : 10 * 1e9 , // 10s
2930 }
30- return client , server
31+ return client
32+ }
33+
34+ func writeJSON (t * testing.T , w http.ResponseWriter , v any ) {
35+ t .Helper ()
36+ if err := json .NewEncoder (w ).Encode (v ); err != nil {
37+ t .Fatalf ("failed to encode JSON response: %v" , err )
38+ }
3139}
3240
3341func TestGet_Success (t * testing.T ) {
@@ -36,10 +44,10 @@ func TestGet_Success(t *testing.T) {
3644 t .Errorf ("expected GET, got %s" , r .Method )
3745 }
3846 w .WriteHeader (http .StatusOK )
39- json . NewEncoder ( w ). Encode ( map [string ]string {"key" : "value" })
47+ writeJSON ( t , w , map [string ]string {"key" : "value" })
4048 })
4149
42- client , _ := newTestClient (t , handler )
50+ client := newTestClient (t , handler )
4351
4452 var result map [string ]string
4553 err := client .Get (context .Background (), "/test" , & result )
@@ -52,12 +60,12 @@ func TestGet_Success(t *testing.T) {
5260}
5361
5462func TestGet_ErrorStatus (t * testing.T ) {
55- handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
63+ handler := http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
5664 w .WriteHeader (http .StatusInternalServerError )
57- json . NewEncoder ( w ). Encode ( map [string ]string {"message" : "something broke" })
65+ writeJSON ( t , w , map [string ]string {"message" : "something broke" })
5866 })
5967
60- client , _ := newTestClient (t , handler )
68+ client := newTestClient (t , handler )
6169
6270 var result map [string ]string
6371 err := client .Get (context .Background (), "/test" , & result )
@@ -71,12 +79,14 @@ func TestGet_ErrorStatus(t *testing.T) {
7179}
7280
7381func TestGet_ErrorStatusPlainBody (t * testing.T ) {
74- handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
82+ handler := http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
7583 w .WriteHeader (http .StatusNotFound )
76- w .Write ([]byte ("404 Not Found" ))
84+ if _ , err := w .Write ([]byte ("404 Not Found" )); err != nil {
85+ t .Fatalf ("failed to write response: %v" , err )
86+ }
7787 })
7888
79- client , _ := newTestClient (t , handler )
89+ client := newTestClient (t , handler )
8090
8191 var result map [string ]string
8292 err := client .Get (context .Background (), "/test" , & result )
@@ -95,10 +105,10 @@ func TestPost_Success(t *testing.T) {
95105 t .Errorf ("expected POST, got %s" , r .Method )
96106 }
97107 w .WriteHeader (http .StatusOK )
98- json . NewEncoder ( w ). Encode ( map [string ]string {"status" : "ok" })
108+ writeJSON ( t , w , map [string ]string {"status" : "ok" })
99109 })
100110
101- client , _ := newTestClient (t , handler )
111+ client := newTestClient (t , handler )
102112
103113 var result map [string ]string
104114 err := client .Post (context .Background (), "/test" , map [string ]string {"input" : "data" }, & result )
@@ -111,12 +121,12 @@ func TestPost_Success(t *testing.T) {
111121}
112122
113123func TestPost_ErrorStatus (t * testing.T ) {
114- handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
124+ handler := http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
115125 w .WriteHeader (http .StatusBadRequest )
116- json . NewEncoder ( w ). Encode ( map [string ]string {"message" : "bad input" })
126+ writeJSON ( t , w , map [string ]string {"message" : "bad input" })
117127 })
118128
119- client , _ := newTestClient (t , handler )
129+ client := newTestClient (t , handler )
120130
121131 var result map [string ]string
122132 err := client .Post (context .Background (), "/test" , map [string ]string {"input" : "data" }, & result )
@@ -130,11 +140,11 @@ func TestPost_ErrorStatus(t *testing.T) {
130140}
131141
132142func TestPost_NilResult_Success (t * testing.T ) {
133- handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
143+ handler := http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
134144 w .WriteHeader (http .StatusOK )
135145 })
136146
137- client , _ := newTestClient (t , handler )
147+ client := newTestClient (t , handler )
138148
139149 err := client .Post (context .Background (), "/test" , nil , nil )
140150 if err != nil {
@@ -143,12 +153,12 @@ func TestPost_NilResult_Success(t *testing.T) {
143153}
144154
145155func TestPost_NilResult_ErrorStatus (t * testing.T ) {
146- handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
156+ handler := http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
147157 w .WriteHeader (http .StatusInternalServerError )
148- json . NewEncoder ( w ). Encode ( map [string ]string {"message" : "server error" })
158+ writeJSON ( t , w , map [string ]string {"message" : "server error" })
149159 })
150160
151- client , _ := newTestClient (t , handler )
161+ client := newTestClient (t , handler )
152162
153163 err := client .Post (context .Background (), "/test" , nil , nil )
154164 if err == nil {
@@ -171,7 +181,7 @@ func TestDelete_Success(t *testing.T) {
171181 w .WriteHeader (http .StatusOK )
172182 })
173183
174- client , _ := newTestClient (t , handler )
184+ client := newTestClient (t , handler )
175185
176186 err := client .Delete (context .Background (), "/apps/test-app" )
177187 if err != nil {
@@ -180,11 +190,11 @@ func TestDelete_Success(t *testing.T) {
180190}
181191
182192func TestDelete_204NoContent (t * testing.T ) {
183- handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
193+ handler := http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
184194 w .WriteHeader (http .StatusNoContent )
185195 })
186196
187- client , _ := newTestClient (t , handler )
197+ client := newTestClient (t , handler )
188198
189199 err := client .Delete (context .Background (), "/apps/test-app" )
190200 if err != nil {
@@ -193,14 +203,14 @@ func TestDelete_204NoContent(t *testing.T) {
193203}
194204
195205func TestDelete_ErrorStatusJSON (t * testing.T ) {
196- handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
206+ handler := http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
197207 w .WriteHeader (http .StatusInternalServerError )
198- json . NewEncoder ( w ). Encode ( map [string ]string {
208+ writeJSON ( t , w , map [string ]string {
199209 "message" : "provider `cloudflare-autorag`: could not revoke token" ,
200210 })
201211 })
202212
203- client , _ := newTestClient (t , handler )
213+ client := newTestClient (t , handler )
204214
205215 err := client .Delete (context .Background (), "/apps/cloudflare-autorag" )
206216 if err == nil {
@@ -213,12 +223,14 @@ func TestDelete_ErrorStatusJSON(t *testing.T) {
213223}
214224
215225func TestDelete_ErrorStatusPlainBody (t * testing.T ) {
216- handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
226+ handler := http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
217227 w .WriteHeader (http .StatusNotFound )
218- w .Write ([]byte ("404 Not Found" ))
228+ if _ , err := w .Write ([]byte ("404 Not Found" )); err != nil {
229+ t .Fatalf ("failed to write response: %v" , err )
230+ }
219231 })
220232
221- client , _ := newTestClient (t , handler )
233+ client := newTestClient (t , handler )
222234
223235 err := client .Delete (context .Background (), "/apps/nonexistent" )
224236 if err == nil {
0 commit comments