github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/plugin_push_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "strings" 9 "testing" 10 11 "golang.org/x/net/context" 12 ) 13 14 func TestPluginPushError(t *testing.T) { 15 client := &Client{ 16 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 17 } 18 19 _, err := client.PluginPush(context.Background(), "plugin_name", "") 20 if err == nil || err.Error() != "Error response from daemon: Server error" { 21 t.Fatalf("expected a Server Error, got %v", err) 22 } 23 } 24 25 func TestPluginPush(t *testing.T) { 26 expectedURL := "/plugins/plugin_name" 27 28 client := &Client{ 29 client: newMockClient(func(req *http.Request) (*http.Response, error) { 30 if !strings.HasPrefix(req.URL.Path, expectedURL) { 31 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 32 } 33 if req.Method != "POST" { 34 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 35 } 36 auth := req.Header.Get("X-Registry-Auth") 37 if auth != "authtoken" { 38 return nil, fmt.Errorf("Invalid auth header : expected 'authtoken', got %s", auth) 39 } 40 return &http.Response{ 41 StatusCode: http.StatusOK, 42 Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), 43 }, nil 44 }), 45 } 46 47 _, err := client.PluginPush(context.Background(), "plugin_name", "authtoken") 48 if err != nil { 49 t.Fatal(err) 50 } 51 }