mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
client/image_test: Use functional option to create mock client
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user