|
9 | 9 | > Http client module based on [net/http](https://pkg.go.dev/net/http). |
10 | 10 |
|
11 | 11 | <!-- TOC --> |
12 | | - |
13 | 12 | * [Installation](#installation) |
14 | 13 | * [Documentation](#documentation) |
15 | | - * [Requests](#requests) |
16 | | - * [Transports](#transports) |
17 | | - * [BaseTransport](#basetransport) |
18 | | - * [LoggerTransport](#loggertransport) |
19 | | - * [MetricsTransport](#metricstransport) |
20 | | - |
| 14 | + * [Requests](#requests) |
| 15 | + * [Transports](#transports) |
| 16 | + * [BaseTransport](#basetransport) |
| 17 | + * [LoggerTransport](#loggertransport) |
| 18 | + * [MetricsTransport](#metricstransport) |
| 19 | + * [Testing](#testing) |
21 | 20 | <!-- TOC --> |
22 | 21 |
|
23 | 22 | ## Installation |
@@ -224,4 +223,96 @@ map[string]string{ |
224 | 223 |
|
225 | 224 | Then if the request path is `/foo/1/bar?page=2`, the metric path label will be masked with `/foo/{fooId}/bar?page={pageId}`. |
226 | 225 |
|
| 226 | +### Testing |
| 227 | + |
| 228 | +This module provides a [httpclienttest.NewTestHTTPServer()](httpclienttest/server.go) helper for testing your clients against a test server, that allows you: |
| 229 | + |
| 230 | +- to define test HTTP roundtrips: a couple of test aware functions to define the request and the response behavior |
| 231 | +- to configure several test HTTP roundtrips if you need to test successive calls |
| 232 | + |
| 233 | +To use it: |
| 234 | + |
| 235 | +```go |
| 236 | +package main_test |
| 237 | + |
| 238 | +import ( |
| 239 | + "errors" |
| 240 | + "net/http" |
| 241 | + "testing" |
| 242 | + |
| 243 | + "github.com/ankorstore/yokai/httpclient" |
| 244 | + "github.com/ankorstore/yokai/httpclient/httpclienttest" |
| 245 | + "github.com/stretchr/testify/assert" |
| 246 | +) |
| 247 | + |
| 248 | +func TestHTTPClient(t *testing.T) { |
| 249 | + t.Parallel() |
| 250 | + |
| 251 | + // client |
| 252 | + client, err := httpclient.NewDefaultHttpClientFactory().Create() |
| 253 | + assert.NoError(t, err) |
| 254 | + |
| 255 | + // test server preparation |
| 256 | + testServer := httpclienttest.NewTestHTTPServer( |
| 257 | + t, |
| 258 | + // configures a roundtrip for the 1st client call |
| 259 | + httpclienttest.WithTestHTTPRoundTrip( |
| 260 | + // func to configure / assert on the client request |
| 261 | + func(tb testing.TB, req *http.Request) error { |
| 262 | + tb.Helper() |
| 263 | + |
| 264 | + // performs some assertions |
| 265 | + assert.Equal(tb, "/foo", req.URL.Path) |
| 266 | + |
| 267 | + // returning an error here will make the test fail, if needed |
| 268 | + return nil |
| 269 | + }, |
| 270 | + // func to configure / assert on the response for the client |
| 271 | + func(tb testing.TB, w http.ResponseWriter) error { |
| 272 | + tb.Helper() |
| 273 | + |
| 274 | + // prepares the response for the client |
| 275 | + w.Header.Set("foo", "bar") |
| 276 | + |
| 277 | + // performs some assertions |
| 278 | + assert.Equal(tb, "bar", w.Header.Get("foo")) |
| 279 | + |
| 280 | + // returning an error here will make the test fail, if needed |
| 281 | + return nil |
| 282 | + }, |
| 283 | + ), |
| 284 | + // configures a roundtrip for the 2nd client call |
| 285 | + httpclienttest.WithTestHTTPRoundTrip( |
| 286 | + // func to configure / assert on the client request |
| 287 | + func(tb testing.TB, req *http.Request) error { |
| 288 | + tb.Helper() |
| 289 | + |
| 290 | + assert.Equal(tb, "/bar", req.URL.Path) |
| 291 | + |
| 292 | + return nil |
| 293 | + }, |
| 294 | + // func to configure / assert on the response for the client |
| 295 | + func(tb testing.TB, w http.ResponseWriter) error { |
| 296 | + tb.Helper() |
| 297 | + |
| 298 | + w.WriteHeader(http.StatusInternalServerError) |
| 299 | + |
| 300 | + return nil |
| 301 | + }, |
| 302 | + ), |
| 303 | + ) |
| 304 | + |
| 305 | + // 1st client call |
| 306 | + resp, err := client.Get(testServer.URL + "/foo") |
| 307 | + assert.NoError(t, err) |
| 308 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 309 | + assert.Equal(t, "bar", resp.Header.Get("foo")) |
| 310 | + |
| 311 | + // 2nd client call |
| 312 | + resp, err = client.Get(testServer.URL + "/bar") |
| 313 | + assert.NoError(t, err) |
| 314 | + assert.Equal(t, http.StatusInternalServerError, resp.StatusCode) |
| 315 | +} |
| 316 | +``` |
227 | 317 |
|
| 318 | +You can find more complete examples in the [tests](httpclienttest/server_test.go). |
0 commit comments