mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
client: simplify test with mock-responses
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user