client: simplify test with mock-responses

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-23 14:20:03 +02:00
parent 1d8c8e192f
commit 47fd987af2
93 changed files with 228 additions and 1087 deletions

View File

@@ -1,12 +1,9 @@
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"testing"
@@ -62,10 +59,7 @@ func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) {
}
auth := req.Header.Get(registry.AuthHeader)
if auth == "NotValid" {
return &http.Response{
StatusCode: http.StatusUnauthorized,
Body: io.NopCloser(bytes.NewReader([]byte("Invalid credentials"))),
}, nil
return mockResponse(http.StatusUnauthorized, nil, "Invalid credentials")(req)
}
if auth != "IAmValid" {
return nil, fmt.Errorf("invalid auth header: expected 'IAmValid', got %s", auth)
@@ -75,18 +69,9 @@ func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) {
if term != "some-image" {
return nil, fmt.Errorf("term not set in URL query properly. Expected 'some-image', got %s", term)
}
content, err := json.Marshal([]registry.SearchResult{
{
Name: "anything",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []registry.SearchResult{
{Name: "anything"},
})(req)
}))
assert.NilError(t, err)
privilegeFunc := func(_ context.Context) (string, error) {
@@ -117,18 +102,9 @@ func TestImageSearchWithoutErrors(t *testing.T) {
if fltrs != expectedFilters {
return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", expectedFilters, fltrs)
}
content, err := json.Marshal([]registry.SearchResult{
{
Name: "anything",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
return mockJSONResponse(http.StatusOK, nil, []registry.SearchResult{
{Name: "anything"},
})(req)
}))
assert.NilError(t, err)
results, err := client.ImageSearch(context.Background(), "some-image", ImageSearchOptions{