forked from gophish/gophish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook_test.go
103 lines (90 loc) · 2.42 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package webhook
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
type mockSender struct {
client *http.Client
}
func newMockSender() *mockSender {
ms := &mockSender{
client: &http.Client{},
}
return ms
}
func (ms mockSender) Send(endPoint EndPoint, data interface{}) error {
log.Println("[test] mocked 'Send' function")
return nil
}
func TestSendMocked(t *testing.T) {
ms := newMockSender()
endpoint := EndPoint{URL: "http://example.com/a1", Secret: "s1"}
data := map[string]string{
"a1": "a11",
"a2": "a22",
"a3": "a33",
}
err := ms.Send(endpoint, data)
if err != nil {
t.Fatalf("error sending data to webhook endpoint: %v", err)
}
}
func TestSendReal(t *testing.T) {
expectedSig := "004b36ca3fcbc01a08b17bf5d4a7e1aa0b10e14f55f3f8bd9acac0c7e8d2635d"
secret := "secret456"
data := map[string]interface{}{
"key1": "val1",
"key2": "val2",
"key3": "val3",
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("[test] running the server...")
signStartIdx := len(Sha256Prefix) + 1
sigHeader := r.Header.Get(SignatureHeader)
gotSig := sigHeader[signStartIdx:]
if expectedSig != gotSig {
t.Fatalf("invalid signature received. expected %s got %s", expectedSig, gotSig)
}
ct := r.Header.Get("Content-Type")
expectedCT := "application/json"
if ct != expectedCT {
t.Fatalf("invalid content type. expected %s got %s", ct, expectedCT)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatalf("error reading JSON body from webhook request: %v", err)
}
var payload map[string]interface{}
err = json.Unmarshal(body, &payload)
if err != nil {
t.Fatalf("error unmarshaling webhook payload: %v", err)
}
if !reflect.DeepEqual(data, payload) {
t.Fatalf("invalid payload received. expected %#v got %#v", data, payload)
}
}))
defer ts.Close()
endp1 := EndPoint{URL: ts.URL, Secret: secret}
err := Send(endp1, data)
if err != nil {
t.Fatalf("error sending data to webhook endpoint: %v", err)
}
}
func TestSignature(t *testing.T) {
secret := "secret123"
payload := []byte("some payload456")
expected := "ab7844c1e9149f8dc976c4188a72163c005930f3c2266a163ffe434230bdf761"
got, err := sign(secret, payload)
if err != nil {
t.Fatalf("error signing payload: %v", err)
}
if expected != got {
t.Fatalf("invalid signature received. expected %s got %s", expected, got)
}
}