From 73fabd5a2182f6a543efaa90dccf5ff110c90e4e Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 5 Nov 2024 13:01:56 +0100 Subject: [PATCH] client: TestImageHistory: use fixture for JSON response Use a fixture instead of encoding with the current definition of the type, to make sure we don't regress if any changes are made in the type. Signed-off-by: Sebastiaan van Stijn --- client/image_history_test.go | 47 +++++++++++++++--------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/client/image_history_test.go b/client/image_history_test.go index 1d2d558eaf..dd7d45a06a 100644 --- a/client/image_history_test.go +++ b/client/image_history_test.go @@ -1,10 +1,7 @@ package client // import "github.com/docker/docker/client" import ( - "bytes" "context" - "encoding/json" - "fmt" "io" "net/http" "strings" @@ -25,37 +22,31 @@ func TestImageHistoryError(t *testing.T) { } func TestImageHistory(t *testing.T) { - expectedURL := "/images/image_id/history" + const ( + expectedURL = "/images/image_id/history" + 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"]}]` + ) client := &Client{ client: newMockClient(func(r *http.Request) (*http.Response, error) { - if !strings.HasPrefix(r.URL.Path, expectedURL) { - return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL) - } - b, err := json.Marshal([]image.HistoryResponseItem{ - { - ID: "image_id1", - Tags: []string{"tag1", "tag2"}, - }, - { - ID: "image_id2", - Tags: []string{"tag1", "tag2"}, - }, - }) - if err != nil { - return nil, err - } - + assert.Check(t, is.Equal(r.URL.Path, expectedURL)) return &http.Response{ StatusCode: http.StatusOK, - Body: io.NopCloser(bytes.NewReader(b)), + Body: io.NopCloser(strings.NewReader(historyResponse)), }, nil }), } + expected := []image.HistoryResponseItem{ + { + ID: "image_id1", + Tags: []string{"tag1", "tag2"}, + }, + { + ID: "image_id2", + Tags: []string{"tag1", "tag2"}, + }, + } + imageHistories, err := client.ImageHistory(context.Background(), "image_id", image.HistoryOptions{}) - if err != nil { - t.Fatal(err) - } - if len(imageHistories) != 2 { - t.Fatalf("expected 2 containers, got %v", imageHistories) - } + assert.NilError(t, err) + assert.Check(t, is.DeepEqual(imageHistories, expected)) }