client: add mockPingResponse utility

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-11-13 00:43:27 +01:00
parent acb5c5a390
commit ef588715b6
2 changed files with 29 additions and 7 deletions

View File

@@ -5,9 +5,11 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/moby/moby/api/types/common"
"github.com/moby/moby/api/types/swarm"
)
// defaultAPIPath is the API path prefix for the default API version used.
@@ -84,6 +86,27 @@ func mockJSONResponse[T any](statusCode int, headers http.Header, resp T) func(r
return mockResponse(statusCode, hdr, string(respBody))
}
// mockPingResponse mocks the headers set for a "/_ping" response.
func mockPingResponse(statusCode int, ping PingResult) func(req *http.Request) (*http.Response, error) {
headers := http.Header{}
if s := ping.SwarmStatus; s != nil {
role := "worker"
if s.ControlAvailable {
role = "manager"
}
headers.Set("Swarm", fmt.Sprintf("%s/%s", string(swarm.LocalNodeStateActive), role))
}
headers.Set("Api-Version", ping.APIVersion)
headers.Set("Ostype", ping.OSType)
headers.Set("Docker-Experimental", strconv.FormatBool(ping.Experimental))
headers.Set("Builder-Version", string(ping.BuilderVersion))
headers.Set("Content-Type", "text/plain; charset=utf-8")
headers.Set("Cache-Control", "no-cache, no-store, must-revalidate")
headers.Set("Pragma", "no-cache")
return mockResponse(statusCode, headers, "OK")
}
func mockResponse(statusCode int, headers http.Header, respBody string) func(req *http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {
var body io.ReadCloser

View File

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