2019-12-16 02:27:21 +00:00
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-12-16 04:07:55 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-02-02 03:44:50 +00:00
|
|
|
"reflect"
|
2019-12-16 04:07:55 +00:00
|
|
|
"testing"
|
2019-12-16 02:27:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockSender struct {
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockSender() *mockSender {
|
2019-12-16 04:07:55 +00:00
|
|
|
ms := &mockSender{
|
2019-12-16 02:27:21 +00:00
|
|
|
client: &http.Client{},
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms mockSender) Send(endPoint EndPoint, data interface{}) error {
|
|
|
|
log.Println("[test] mocked 'Send' function")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-02 03:44:50 +00:00
|
|
|
func TestSendMocked(t *testing.T) {
|
|
|
|
ms := newMockSender()
|
|
|
|
endpoint := EndPoint{URL: "http://example.com/a1", Secret: "s1"}
|
|
|
|
data := map[string]string{
|
2019-12-16 02:27:21 +00:00
|
|
|
"a1": "a11",
|
|
|
|
"a2": "a22",
|
|
|
|
"a3": "a33",
|
|
|
|
}
|
2020-02-02 03:44:50 +00:00
|
|
|
err := ms.Send(endpoint, data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error sending data to webhook endpoint: %v", err)
|
|
|
|
}
|
2019-12-16 02:27:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 03:44:50 +00:00
|
|
|
func TestSendReal(t *testing.T) {
|
|
|
|
expectedSig := "004b36ca3fcbc01a08b17bf5d4a7e1aa0b10e14f55f3f8bd9acac0c7e8d2635d"
|
2019-12-16 02:27:21 +00:00
|
|
|
secret := "secret456"
|
2020-02-02 03:44:50 +00:00
|
|
|
data := map[string]interface{}{
|
2019-12-16 02:27:21 +00:00
|
|
|
"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
|
2020-02-02 03:44:50 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-12-16 02:27:21 +00:00
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
2020-02-02 03:44:50 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-12-16 02:27:21 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
defer ts.Close()
|
|
|
|
endp1 := EndPoint{URL: ts.URL, Secret: secret}
|
2020-02-02 03:44:50 +00:00
|
|
|
err := Send(endp1, data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error sending data to webhook endpoint: %v", err)
|
|
|
|
}
|
2019-12-16 02:27:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 03:44:50 +00:00
|
|
|
func TestSignature(t *testing.T) {
|
2019-12-16 02:27:21 +00:00
|
|
|
secret := "secret123"
|
|
|
|
payload := []byte("some payload456")
|
2020-02-02 03:44:50 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-12-16 02:27:21 +00:00
|
|
|
}
|