client: improve mocking responses

Make the mocked responses match the API closer;

- Add headers as returned by the daemon's VersionMiddleware
- By default handle "/_ping" requests to allow the client to
  perform API-version negotiation as part of tests.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-11-13 01:25:15 +01:00
parent ef588715b6
commit 701f2fdade
4 changed files with 77 additions and 13 deletions

View File

@@ -258,7 +258,7 @@ func TestNegotiateAPIVersionEmpty(t *testing.T) {
client, err := New(FromEnv,
WithAPIVersionNegotiation(),
WithMockClient(mockPingResponse(http.StatusOK, PingResult{APIVersion: expected})),
WithBaseMockClient(mockPingResponse(http.StatusOK, PingResult{APIVersion: expected})),
)
assert.NilError(t, err)
@@ -331,7 +331,7 @@ func TestNegotiateAPIVersion(t *testing.T) {
opts := []Opt{
FromEnv,
WithAPIVersionNegotiation(),
WithMockClient(mockPingResponse(http.StatusOK, PingResult{APIVersion: tc.pingVersion})),
WithBaseMockClient(mockPingResponse(http.StatusOK, PingResult{APIVersion: tc.pingVersion})),
}
if tc.clientVersion != "" {
@@ -363,7 +363,7 @@ func TestNegotiateAPIVersionOverride(t *testing.T) {
client, err := New(
FromEnv,
WithMockClient(mockPingResponse(http.StatusOK, PingResult{APIVersion: "1.45"})),
WithBaseMockClient(mockPingResponse(http.StatusOK, PingResult{APIVersion: "1.45"})),
)
assert.NilError(t, err)
@@ -393,7 +393,7 @@ func TestNegotiateAPIVersionAutomatic(t *testing.T) {
ctx := t.Context()
client, err := New(
WithMockClient(func(req *http.Request) (*http.Response, error) {
WithBaseMockClient(func(req *http.Request) (*http.Response, error) {
return mockPingResponse(http.StatusOK, PingResult{APIVersion: pingVersion})(req)
}),
WithAPIVersionNegotiation(),
@@ -422,7 +422,7 @@ func TestNegotiateAPIVersionAutomatic(t *testing.T) {
func TestNegotiateAPIVersionWithEmptyVersion(t *testing.T) {
client, err := New(
WithAPIVersion(""),
WithMockClient(mockPingResponse(http.StatusOK, PingResult{APIVersion: "1.50"})),
WithBaseMockClient(mockPingResponse(http.StatusOK, PingResult{APIVersion: "1.50"})),
)
assert.NilError(t, err)
@@ -442,7 +442,7 @@ func TestNegotiateAPIVersionWithFixedVersion(t *testing.T) {
)
client, err := New(
WithAPIVersion(customVersion),
WithMockClient(mockPingResponse(http.StatusOK, PingResult{APIVersion: pingVersion})),
WithBaseMockClient(mockPingResponse(http.StatusOK, PingResult{APIVersion: pingVersion})),
)
assert.NilError(t, err)