forked from airbnb/k8s-webhook-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
63 lines (52 loc) · 1.44 KB
/
handler_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
package handler
import (
"context"
"fmt"
"io/ioutil"
"net/http/httptest"
"os"
"testing"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/metrics/statsd"
"k8s.io/apimachinery/pkg/runtime"
)
type mockKubernetesClient struct {
obj runtime.Object
namespace string
}
func (k *mockKubernetesClient) Apply(obj runtime.Object, namespace string) error {
k.obj = obj
k.namespace = namespace
return nil
}
type mockLoader struct {
obj runtime.Object
}
func (l *mockLoader) Load(ctx context.Context, repo, path, ref string) (runtime.Object, error) {
return l.obj, nil
}
func TestHandle(t *testing.T) {
var (
config = &Config{Namespace: "namespace", ResourcePath: "foo/bar.yaml", Secret: []byte("foobar")}
)
logger := log.With(log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)), "caller", log.Caller(5))
handler := NewGithubHookHandler(
logger,
config,
&mockKubernetesClient{},
&mockLoader{},
statsd.New("k8s-ci-purger.", logger),
)
req := httptest.NewRequest("POST", "http://example.com/", nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-GitHub-Event", "DeleteEvent")
req.Header.Set("X-GitHub-Delivery", "4636fc67-b693-4a27-87a4-18d4021ae789")
req.Header.Set("X-Hub-Signature", "sha1=1234")
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header.Get("Content-Type"))
fmt.Println(string(body))
}