client: WithMockClient: match version behavior of actual client

The WithMockClient option was explicitly resetting the client's API
version (see [1]), which differs from the regular client, which is
initialized with the current API version used by the client (see [2]).

This patch:

- reduces the `WithMockClient` to only set the custom HTTP client, leaving
  other fields un-touched.
- adds a test utility and updates tests to handle the API-version prefix
- removes redundant uses of `WithVersion()` in tests; for most test-cases
  it was used to make sure a current API version is used that supports the
  feature being tested, but there was no test to verify the behavior for
  lower API versions, so we may as well test against "latest".

[1]: 5a582729d8/client/client_mock_test.go (L22-L36)
[2]: 5a582729d8/client/client.go (L167-L190)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-09-17 23:12:40 +02:00
parent 64419958cc
commit 839c2709af
93 changed files with 389 additions and 626 deletions

View File

@@ -27,9 +27,9 @@ func TestImageHistory(t *testing.T) {
historyResponse = `[{"Comment":"","Created":0,"CreatedBy":"","Id":"image_id1","Size":0,"Tags":["tag1","tag2"]},{"Comment":"","Created":0,"CreatedBy":"","Id":"image_id2","Size":0,"Tags":["tag1","tag2"]}]`
expectedPlatform = `{"architecture":"arm64","os":"linux","variant":"v8"}`
)
client, err := NewClientWithOpts(WithMockClient(func(r *http.Request) (*http.Response, error) {
assert.Check(t, is.Equal(r.URL.Path, expectedURL))
assert.Check(t, is.Equal(r.URL.Query().Get("platform"), expectedPlatform))
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
assert.Check(t, assertRequest(req, http.MethodGet, expectedURL))
assert.Check(t, is.Equal(req.URL.Query().Get("platform"), expectedPlatform))
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(historyResponse)),