client/image_test: Use functional option to create mock client

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-08-29 15:17:22 +02:00
parent 005a289703
commit be76beee8f
14 changed files with 511 additions and 566 deletions

View File

@@ -15,11 +15,10 @@ import (
)
func TestImageLoadError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err := client.ImageLoad(context.Background(), nil, ImageLoadWithQuiet(true))
_, err = client.ImageLoad(context.Background(), nil, ImageLoadWithQuiet(true))
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
@@ -82,18 +81,17 @@ func TestImageLoad(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
assert.Check(t, is.Equal(req.URL.Path, expectedURL))
assert.Check(t, is.Equal(req.Header.Get("Content-Type"), expectedContentType))
assert.Check(t, is.DeepEqual(req.URL.Query(), tc.expectedQueryParams))
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
Header: http.Header{"Content-Type": []string{tc.responseContentType}},
}, nil
}),
}
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
assert.Check(t, is.Equal(req.URL.Path, expectedURL))
assert.Check(t, is.Equal(req.Header.Get("Content-Type"), expectedContentType))
assert.Check(t, is.DeepEqual(req.URL.Query(), tc.expectedQueryParams))
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(expectedOutput))),
Header: http.Header{"Content-Type": []string{tc.responseContentType}},
}, nil
}))
assert.NilError(t, err)
input := bytes.NewReader([]byte(expectedInput))
imageLoadResponse, err := client.ImageLoad(context.Background(), input,