-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvix_test.go
More file actions
35 lines (28 loc) 路 763 Bytes
/
Copy pathsvix_test.go
File metadata and controls
35 lines (28 loc) 路 763 Bytes
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
package trusthook
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"net/http"
"strconv"
"testing"
"time"
)
func TestSvixValidSignature(t *testing.T) {
rawKey := []byte("supersecretkey")
secret := "whsec_" + base64.StdEncoding.EncodeToString(rawKey)
id := "msg_123"
ts := strconv.FormatInt(time.Now().Unix(), 10)
body := []byte(`{"id":"evt_123"}`)
mac := hmac.New(sha256.New, rawKey)
mac.Write([]byte(id + "." + ts + "." + string(body)))
sig := base64.StdEncoding.EncodeToString(mac.Sum(nil))
headers := http.Header{}
headers.Set("svix-id", id)
headers.Set("svix-timestamp", ts)
headers.Set("svix-signature", "v1,"+sig)
err := Verify(Svix, body, headers, secret)
if err != nil {
t.Errorf("got %v, expected %v", err, nil)
}
}