plugin_push_test.go 1.3 KB

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