Skip to content

Commit 9e3a6d0

Browse files
authored
doc(fxhttpclient): Updated documentation (ankorstore#325)
1 parent b818870 commit 9e3a6d0

File tree

1 file changed

+89
-1
lines changed

1 file changed

+89
-1
lines changed

docs/modules/fxhttpclient.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,92 @@ http_client_requests_total{method="GET",status="2xx",host="https://example.com",
206206

207207
## Testing
208208

209-
See [net/http/httptest](https://pkg.go.dev/net/http/httptest) documentation.
209+
This module provides a [httpclienttest.NewTestHTTPServer()](https://github.com/ankorstore/yokai/blob/main/httpclient/httpclienttest/server.go) helper for testing your clients against a test server, that allows you:
210+
211+
- to define test HTTP roundtrips: a couple of test aware functions to define the request and the response behavior
212+
- to configure several test HTTP roundtrips if you need to test successive calls
213+
214+
To use it:
215+
216+
```go title="internal/service/example_test.go"
217+
package service_test
218+
219+
import (
220+
"net/http"
221+
"testing"
222+
223+
"github.com/ankorstore/yokai/httpclient"
224+
"github.com/ankorstore/yokai/httpclient/httpclienttest"
225+
"github.com/stretchr/testify/assert"
226+
)
227+
228+
func TestHTTPClient(t *testing.T) {
229+
t.Parallel()
230+
231+
// retrieve your client
232+
var client *http.Client
233+
234+
// test server preparation
235+
testServer := httpclienttest.NewTestHTTPServer(
236+
t,
237+
// configures a roundtrip for the 1st client call (/foo)
238+
httpclienttest.WithTestHTTPRoundTrip(
239+
// func to configure / assert on the client request
240+
func(tb testing.TB, req *http.Request) error {
241+
tb.Helper()
242+
243+
// performs some assertions
244+
assert.Equal(tb, "/foo", req.URL.Path)
245+
246+
// returning an error here will make the test fail, if needed
247+
return nil
248+
},
249+
// func to configure / assert on the response for the client
250+
func(tb testing.TB, w http.ResponseWriter) error {
251+
tb.Helper()
252+
253+
// prepares the response for the client
254+
w.Header.Set("foo", "bar")
255+
256+
// performs some assertions
257+
assert.Equal(tb, "bar", w.Header.Get("foo"))
258+
259+
// returning an error here will make the test fail, if needed
260+
return nil
261+
},
262+
),
263+
// configures a roundtrip for the 2nd client call (/bar)
264+
httpclienttest.WithTestHTTPRoundTrip(
265+
// func to configure / assert on the client request
266+
func(tb testing.TB, req *http.Request) error {
267+
tb.Helper()
268+
269+
assert.Equal(tb, "/bar", req.URL.Path)
270+
271+
return nil
272+
},
273+
// func to configure / assert on the response for the client
274+
func(tb testing.TB, w http.ResponseWriter) error {
275+
tb.Helper()
276+
277+
w.WriteHeader(http.StatusInternalServerError)
278+
279+
return nil
280+
},
281+
),
282+
)
283+
284+
// 1st client call (/foo)
285+
resp, err := client.Get(testServer.URL + "/foo")
286+
assert.NoError(t, err)
287+
assert.Equal(t, http.StatusOK, resp.StatusCode)
288+
assert.Equal(t, "bar", resp.Header.Get("foo"))
289+
290+
// 2nd client call (/bar)
291+
resp, err = client.Get(testServer.URL + "/bar")
292+
assert.NoError(t, err)
293+
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
294+
}
295+
```
296+
297+
You can find more complete examples in the module [tests](https://github.com/ankorstore/yokai/blob/main/httpclient/httpclienttest/server_test.go).

0 commit comments

Comments
 (0)