diff --git a/circleci_test.go b/circleci_test.go index 4b91030..56b4e9e 100644 --- a/circleci_test.go +++ b/circleci_test.go @@ -1,12 +1,14 @@ package circleci import ( + "encoding/json" "fmt" - "io/ioutil" "net/http" "net/http/httptest" "net/url" "testing" + + "github.com/google/go-cmp/cmp" ) func setup() (client *Client, mux *http.ServeMux, serverURL string, teardown func()) { @@ -45,12 +47,20 @@ func testHeader(t *testing.T, r *http.Request, header string, want string) { func testBody(t *testing.T, r *http.Request, want string) { t.Helper() - b, err := ioutil.ReadAll(r.Body) + var mgot map[string]interface{} + err := json.NewDecoder(r.Body).Decode(&mgot) if err != nil { t.Errorf("Error reading request body: %v", err) } - if got := string(b); got != want { - t.Errorf("request Body is %s, want %s", got, want) + + var mwant map[string]interface{} + err = json.Unmarshal([]byte(want), &mwant) + if err != nil { + t.Errorf("Error Unmarshalling want string: %v", err) + } + + if !cmp.Equal(mgot, mwant) { + t.Errorf("request Body is %s, want %s", mgot, mwant) } } diff --git a/errors.go b/errors.go index 5d47beb..8502325 100644 --- a/errors.go +++ b/errors.go @@ -27,6 +27,7 @@ var ( ErrRequiredJobName = errors.New("job name is required") ErrRequiredWebhookEvents = errors.New("webhook events is required") ErrRequiredWebhookName = errors.New("webhook name is required") + ErrRequiredWebhookID = errors.New("webhook ID is required") ErrRequiredWebhookURL = errors.New("webhook URL is required") ErrRequiredWebhookVerifyTLS = errors.New("webhook verifyTLS is required") ErrRequiredWebhookSigningSecret = errors.New("webhook signingSecret is required") diff --git a/webhook.go b/webhook.go index d5d41a1..a1b64bc 100644 --- a/webhook.go +++ b/webhook.go @@ -2,9 +2,11 @@ package circleci import ( "context" + "fmt" ) type Webhooks interface { + Get(ctx context.Context, id string) (*Webhook, error) List(ctx context.Context, options WebhookListOptions) (*WebhookList, error) Create(ctx context.Context, options WebhookCreateOptions) (*Webhook, error) } @@ -33,6 +35,26 @@ type Scope struct { Type string `json:"type"` } +func (s *webhooks) Get(ctx context.Context, id string) (*Webhook, error) { + if !validString(&id) { + return nil, ErrRequiredWebhookID + } + + u := fmt.Sprintf("webhook/%s", id) + req, err := s.client.newRequest("GET", u, nil) + if err != nil { + return nil, err + } + + w := &Webhook{} + err = s.client.do(ctx, req, w) + if err != nil { + return nil, err + } + + return w, nil +} + type WebhookListOptions struct { ScopeID *string `url:"scope-id,omitempty"` ScopeType *string `url:"scope-type,omitempty"` diff --git a/webhook_test.go b/webhook_test.go index e05b082..5948ea1 100644 --- a/webhook_test.go +++ b/webhook_test.go @@ -9,6 +9,31 @@ import ( "github.com/google/go-cmp/cmp" ) +func Test_webhooks_Get(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + webhookID := "webhook1" + mux.HandleFunc(fmt.Sprintf("/webhook/%s", webhookID), func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testHeader(t, r, "Accept", "application/json") + testHeader(t, r, "Circle-Token", client.token) + fmt.Fprint(w, `{"id": "1"}`) + }) + + ctx := context.Background() + w, err := client.Webhooks.Get(ctx, webhookID) + if err != nil { + t.Errorf("Webhooks.Get got error: %v", err) + } + + want := &Webhook{ID: "1"} + + if !cmp.Equal(w, want) { + t.Errorf("Webhooks.Get got %+v, want %+v", w, want) + } +} + func Test_webhooks_List(t *testing.T) { client, mux, _, teardown := setup() defer teardown()