forked from 0xch4z/dockerhub-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhook_test.go
77 lines (63 loc) · 1.75 KB
/
webhook_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package dockerhub
import (
"context"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestWebhookService_CreateWebhook(t *testing.T) {
client, mux, teardown := makeMockClient()
defer teardown()
namespace := "namespace"
registry := "registry-1.docker.io"
repo := "repo"
name := "name"
url := "https://www.ngrok.com/re78rt/hook"
hook := &WebhookResponse{}
uri := fmt.Sprintf("/repositories/%s/%s/", namespace, repo)
mux.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, r, http.MethodPost)
assertNoHeader(t, r, "Authorization")
assertBody(t, r, string(mustJSONMarshal(&WebhookRequest{
Name: name,
Registry: registry,
Webhooks: []Webhooks{
{
Name: name,
HookURL: url,
},
},
},
)))
w.WriteHeader(http.StatusCreated)
w.Write(mustJSONMarshal(&WebhookResponse{}))
})
res, err := client.Webhook.CreateWebhook(context.Background(), namespace, repo, name, url)
if err != nil {
t.Errorf("Webhook.CreateWebhook returned error: %v", err)
}
if !reflect.DeepEqual(res, hook) {
t.Errorf("webhook is %v; want %v", res, hook)
}
}
func TestWebhookService_GetWebhooks(t *testing.T) {
client, mux, teardown := makeMockClient()
defer teardown()
namespace := "namespace"
repo := "repo"
hook := &WebhookResponse{}
uri := fmt.Sprintf("/repositories/%s/%s/", namespace, repo)
mux.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
assertMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusOK)
w.Write(mustJSONMarshal(hook))
})
res, err := client.Webhook.GetWebhooks(context.Background(), namespace, repo)
if err != nil {
t.Errorf("Webhook.GetWebhooks returned error: %v", err)
}
if !reflect.DeepEqual(res, hook) {
t.Errorf("webhook is %v; want %v", res, hook)
}
}