github.com/brandon-bethke-neudesic/moby@v1.13.1/client/secret_create_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types" 13 "github.com/docker/docker/api/types/swarm" 14 "golang.org/x/net/context" 15 ) 16 17 func TestSecretCreateError(t *testing.T) { 18 client := &Client{ 19 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 20 } 21 _, err := client.SecretCreate(context.Background(), swarm.SecretSpec{}) 22 if err == nil || err.Error() != "Error response from daemon: Server error" { 23 t.Fatalf("expected a Server Error, got %v", err) 24 } 25 } 26 27 func TestSecretCreate(t *testing.T) { 28 expectedURL := "/secrets/create" 29 client := &Client{ 30 client: newMockClient(func(req *http.Request) (*http.Response, error) { 31 if !strings.HasPrefix(req.URL.Path, expectedURL) { 32 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 33 } 34 if req.Method != "POST" { 35 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 36 } 37 b, err := json.Marshal(types.SecretCreateResponse{ 38 ID: "test_secret", 39 }) 40 if err != nil { 41 return nil, err 42 } 43 return &http.Response{ 44 StatusCode: http.StatusCreated, 45 Body: ioutil.NopCloser(bytes.NewReader(b)), 46 }, nil 47 }), 48 } 49 50 r, err := client.SecretCreate(context.Background(), swarm.SecretSpec{}) 51 if err != nil { 52 t.Fatal(err) 53 } 54 if r.ID != "test_secret" { 55 t.Fatalf("expected `test_secret`, got %s", r.ID) 56 } 57 }